枚举“继承" [英] Enum "Inheritance"
问题描述
我在低级命名空间中有一个枚举.我想在继承"低级枚举的中级命名空间中提供一个类或枚举.
I have an enum in a low level namespace. I'd like to provide a class or enum in a mid level namespace that "inherits" the low level enum.
namespace low
{
public enum base
{
x, y, z
}
}
namespace mid
{
public enum consume : low.base
{
}
}
我希望这是可能的,或者某种可以代替枚举消耗的类,这将为枚举提供一个抽象层,但仍然让该类的实例访问枚举.
I'm hoping that this is possible, or perhaps some kind of class that can take the place of the enum consume which will provide a layer of abstraction for the enum, but still let an instance of that class access the enum.
想法?
我没有在类中将其切换为 consts 的原因之一是我必须使用的服务需要低级枚举.我得到了 WSDL 和 XSD,它们将结构定义为枚举.服务无法更改.
One of the reasons I haven't just switched this to consts in classes is that the low level enum is needed by a service that I must consume. I have been given the WSDLs and the XSDs, which define the structure as an enum. The service cannot be changed.
推荐答案
这是不可能的.枚举不能从其他枚举继承.事实上,所有的枚举实际上都必须从 System.Enum
继承.C# 允许语法更改枚举值的底层表示,看起来像继承,但实际上它们仍然继承自 System.enum.
This is not possible. Enums cannot inherit from other enums. In fact all enums must actually inherit from System.Enum
. C# allows syntax to change the underlying representation of the enum values which looks like inheritance, but in actuality they still inherit from System.enum.
请参阅 CLI 规范的第 8.5.2 节 了解全部细节.规范中的相关信息
See section 8.5.2 of the CLI spec for the full details. Relevant information from the spec
- 所有枚举必须派生自
System.Enum
- 由于上述原因,所有枚举都是值类型,因此是密封的
这篇关于枚举“继承"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!