枚举"继承与QUOT; [英] Enum "Inheritance"

查看:158
本文介绍了枚举"继承与QUOT;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个较低水平的命名空间枚举。我想提供一个类或枚举在继承的低水平枚举一个中等水平的命名空间。

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#允许语法改变枚举值,看起来像继承的基础再presentation,但实际上他们仍然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
  • 在上面,因为,所有的枚举是值类型,因此密封

这篇关于枚举"继承与QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆