[英]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."
基类有一个抽象的System.Enum属性,我想使用我的自定义枚举类型实现,但是我不断收到一条错误消息,说我的派生属性“必须为System.Enum类型才能匹配被覆盖的成员”。
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? 谁能告诉我为什么我的枚举不是System.Enum类型? Thanks in advance.
提前致谢。
Your BaseClass
gives a "promise" that all of its subclasses will have the property Specification
of type System.Enum
. 您的
BaseClass
给出了一个“承诺”,即其所有子类都将具有类型为System.Enum
Specification
。 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
. 这意味着,如果我有任何对象
BaseClass b = ...
,则可以使用b.Specification = enumValue
分配给它的Specification
,其中enumValue
是任何 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
. 现在,如果您在
DerivedClass
派生,我将无法再将任何 System.Enum
分配给EnumClass.MyEnum Specification
,而只能分配EnumClass.MyEnum Specification
类型的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
. 显然,
DerivedClass.SetSpecification
不能覆盖BaseClass.SetSpecification
,因为参数类型EnumClass.MyEnum
是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. 不幸的是,
where T : enum
不可能的情况下,因此仍有可能将其他IConvertible
struct
传递给泛型类型参数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.