简体   繁体   中英

Overriding an abstract Enum property in C#

I have a base class and an enum type defined in a different class. The base class has an abstract System.Enum property that I would like to implement using my custom enum type, however I keep getting an error saying my derived property "must be type System.Enum to match overridden member."

My enum type looks like this:

public static class EnumClass
{
    public enum MyEnum {
        option1,
        option2
    };
}

My base class looks like this:

public abstract class BaseClass 
{
    public abstract Enum Specification { get; set; }
}

And my derived class looks like this:

public class DerivedClass : BaseClass 
{
    private EnumClass.MyEnum mSpecification;

    public override EnumClass.MyEnum Specification
    {
         get { return mSpecification; }
         set { mSpecification = value; }
    }
}

Can anyone tell me why my enum isn't a System.Enum type? Thanks in advance.

Your BaseClass gives a "promise" that all of its subclasses will have the property Specification of type System.Enum . That means, if I have any object BaseClass b = ... , I can assign to its Specification with b.Specification = enumValue where enumValue is any System.Enum .

Now, if you derive in DerivedClass , I can no longer assign any System.Enum to EnumClass.MyEnum Specification , but only values of type EnumClass.MyEnum . This is why this property does not satisfy the contracts of its base class.

Therefore, the compiler will tell you that this implementation violates the inheritance from the abstract base class and you have to use the same property type in the derived class as well.

For better understanding, split the property into two methods:

public abstract class BaseClass
{
    public abstract System.Enum GetSpecification();
    public abstract void SetSpecification(System.Enum value);
}

public class DerivedClass : BaseClass
{
    private EnumClass.MyEnum specification;
    public override EnumClass.MyEnum GetSpecification()
    {
        return specification;
    }
    public override void SetSpecification(EnumClass.MyEnum value)
    {
        specification = value;
    }
}

Obviously, BaseClass.SetSpecification cannot be overridden by DerivedClass.SetSpecification because the parameter type EnumClass.MyEnum is a subclass of System.Enum .

If you want to allow a specific type of enum for every subclass, you can use generics for that:

public abstract class BaseClass<T> where T : struct, IConvertible // almost only enums
{
    public abstract T Specification { get; set; }
}
public class DerivedClass : BaseClass<EnumClass.MyEnum>
{
    public EnumClass.MyEnum Specification { get; set; }
}

Unfortunately, where T : enum is not possible so there is still the possibility of passing other IConvertible struct s to the generic type parameter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM