c# enum covariance doesn't work

廉价感情. 提交于 2021-02-08 13:59:16

问题


I need to use enum as covariant type. Let's say i have this code:

public enum EnumColor
{
    Blue = 0,
    Red = 1,
}

public class Car : IColoredObject<EnumColor>
{
    private EnumColor m_Color;
    public EnumColor Color
    {
        get
        {
            return m_Color;
        }
        set
        {
            m_Color = value;
        }
    }

    public Car()
    {
    }
}

class Program
{
    static void Main()
    {
        Car car = new Car();
        IndependentClass.DoesItWork( car );
    }
}

and this code:

public interface IColoredObject<out EnumColorType>
{
    EnumColorType Color
    {
        get;
    }
}

public static class IndependentClass
{
    public static void DoesItWork( object car )
    {
        IColoredObject<object> coloredObject = car as IColoredObject<object>;

        if( coloredObject == null )
            Console.WriteLine( "It doesn't work." );
        else
        {
            Console.WriteLine( "It works." );

            int colorNumber = (int)( coloredObject.Color );

            Console.WriteLine( "Car has got color number " + colorNumber + "." );
        }
    }
}

I was trying to use Enum.

IColoredObject<Enum> coloredObject = car as IColoredObject<Enum>;

I was trying to use IConvertible which is interface of Enum.

IColoredObject<IConvertible> coloredObject = car as IColoredObject<IConvertible>;

But everytime it doesn't work (it was null).

What should i use ? Or how can i do it ?

(I don't want to use EnumColor in second part of code, because i want two independent codes joined only with interface.)


回答1:


Covariance is not supported with "Value types" Enum falls under this category and hence doesn't work.

Variance in generic interfaces is supported for reference types only. Value types do not support variance. For example, IEnumerable<int> (IEnumerable(Of Integer) in Visual Basic) cannot be implicitly converted to IEnumerable<object> (IEnumerable(Of Object) in Visual Basic), because integers are represented by a value type.

Source MSDN




回答2:


Finally i solved my problem with generic method. It's few chars more, but it works.

First code:

public enum EnumColor
{
    Blue = 0,
    Red = 1,
}

public class Car : IColoredObject<EnumColor>
{
    private EnumColor m_Color;
    public EnumColor Color
    {
        get
        {
            return m_Color;
        }
        set
        {
            m_Color = value;
        }
    }

    public Car()
    {
    }
}

class Program
{
    static void Main()
    {
        Car car = new Car();
        IndependentClass.DoesItWork<EnumColor>( car );
    }
}

Second code:

public interface IColoredObject<EnumColorType>
{
    EnumColorType Color
    {
        get;
    }
}

public static class IndependentClass
{
    public static void DoesItWork<EnumColorType>( object car )
    {
        IColoredObject<EnumColorType> coloredObject = car as IColoredObject<EnumColorType>;

        if( coloredObject == null )
            Console.WriteLine( "It doesn't work." );
        else
        {
            Console.WriteLine( "It works." );

            int colorNumber = (int)( coloredObject.Color as object );

            Console.WriteLine( "Car has got color number " + colorNumber + "." );
        }
    }
}


来源:https://stackoverflow.com/questions/21202661/c-sharp-enum-covariance-doesnt-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!