C#:通过继承构建 java 风格的枚举
我正在寻找为 C# 构建一个 java 风格的枚举模式,它也支持继承。我在收益回报方面遇到了麻烦。具体来说,从 ChildEnum 的 Values 属性返回 BaseEnum 的值。
public class BaseEnum {
public static readonly BaseEnum A = new BaseEnum("A");
public static readonly BaseEnum B = new BaseEnum("B");
public static readonly BaseEnum C = new BaseEnum("C");
public static IEnumerable<BaseEnum> Values {
get {
yield return A;
yield return B;
yield return C;
}
}
public readonly String Name;
protected BaseEnum(String name) {
this.Name = name;
}
public static void TestMain() {
Console.WriteLine("BaseEnum, should print (A,B,C):");
foreach(BaseEnum e in BaseEnum.Values) {
Console.WriteLine(e.Name);
}
Console.WriteLine("BaseEnum in ChildEnum, should print (A,B,C,D,E):");
foreach(BaseEnum e in ChildEnum.Values) {
Console.WriteLine(e.Name);
}
Console.WriteLine("ChildEnum in ChildEnum, should print (D,E):");
foreach(ChildEnum e in ChildEnum.Values) {
Console.WriteLine(e.Name);
}
}
}
class ChildEnum : BaseEnum {
public static readonly ChildEnum D = new ChildEnum("D");
public static readonly ChildEnum E = new ChildEnum("E");
new public static IEnumerable<BaseEnum> Values {
get {
// yield return BaseEnum.Values; // This is what I want to do. It gives the error message below:
// Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<Abra.Workshop.EnumABC>'
// to 'Abra.Workshop.EnumABC'. An explicit conversion exists (are you missing a cast?)
yield return D;
yield return E;
}
}
public ChildEnum(string name)
: base(name) {
}
}
/* Output!
BaseEnum, should print (A,B,C):
A
B
C
BaseEnum in ChildEnum, should print (A,B,C,D,E):
D
E
ChildEnum in ChildEnum, should print (D,E):
D
E
*/
I am looking to build a java-style enum pattern for C# that also supports inheritance. I'm having trouble with yield return. Specifically, returning the BaseEnum's Values from the ChildEnum's Values property.
public class BaseEnum {
public static readonly BaseEnum A = new BaseEnum("A");
public static readonly BaseEnum B = new BaseEnum("B");
public static readonly BaseEnum C = new BaseEnum("C");
public static IEnumerable<BaseEnum> Values {
get {
yield return A;
yield return B;
yield return C;
}
}
public readonly String Name;
protected BaseEnum(String name) {
this.Name = name;
}
public static void TestMain() {
Console.WriteLine("BaseEnum, should print (A,B,C):");
foreach(BaseEnum e in BaseEnum.Values) {
Console.WriteLine(e.Name);
}
Console.WriteLine("BaseEnum in ChildEnum, should print (A,B,C,D,E):");
foreach(BaseEnum e in ChildEnum.Values) {
Console.WriteLine(e.Name);
}
Console.WriteLine("ChildEnum in ChildEnum, should print (D,E):");
foreach(ChildEnum e in ChildEnum.Values) {
Console.WriteLine(e.Name);
}
}
}
class ChildEnum : BaseEnum {
public static readonly ChildEnum D = new ChildEnum("D");
public static readonly ChildEnum E = new ChildEnum("E");
new public static IEnumerable<BaseEnum> Values {
get {
// yield return BaseEnum.Values; // This is what I want to do. It gives the error message below:
// Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<Abra.Workshop.EnumABC>'
// to 'Abra.Workshop.EnumABC'. An explicit conversion exists (are you missing a cast?)
yield return D;
yield return E;
}
}
public ChildEnum(string name)
: base(name) {
}
}
/* Output!
BaseEnum, should print (A,B,C):
A
B
C
BaseEnum in ChildEnum, should print (A,B,C,D,E):
D
E
ChildEnum in ChildEnum, should print (D,E):
D
E
*/
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用
您需要将 BaseEnum.Values 展开为单独的元素。
或者将其替换为 LINQ 表达式:-
Use
You need to unwrap BaseEnum.Values into individual elements.
Or replace it with a LINQ expression:-
这是一个不需要令人讨厌的
new public static
枚举器的版本。它确实改变了继承结构以使其正常工作。我想我会提出想法作为替代方案。
它确实还需要一个默认的公共构造函数,但由于
Name
属性上的受保护设置器,只有在继承层次结构中运行的代码才能设置名称,因此,如果出现一个没有名称的实例,您就知道有人正在做错事。Here's a version that does away with the need to have the nasty
new public static
enumerator.It does change the inheritance structure to make it work though. Thought I'd put it forward thought as an alternative.
It does also require a default public constructor, but thanks to the protected setter on the
Name
property only code running in the inheritance hierarchy can set the name, so if an instance appeared without a name you know that someone is doing something wrong.