C#:通过继承构建 java 风格的枚举

发布于 2024-12-03 13:50:05 字数 2002 浏览 2 评论 0原文

我正在寻找为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

战皆罪 2024-12-10 13:50:05

使用

foreach (var b in BaseEnum.Values)
  yield return b;

您需要将 BaseEnum.Values 展开为单独的元素。

或者将其替换为 LINQ 表达式:-

return BaseEnum.Values.Concat(new[]{ D, E });

Use

foreach (var b in BaseEnum.Values)
  yield return b;

You need to unwrap BaseEnum.Values into individual elements.

Or replace it with a LINQ expression:-

return BaseEnum.Values.Concat(new[]{ D, E });
不甘平庸 2024-12-10 13:50:05
class Program
{
    static void Main(string[] args)
    {
        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.Where(d => d.GetType() == typeof(ChildEnum)))
        {
            Console.WriteLine(e.Name);
        }
    }
}

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 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
        {
            foreach (var baseEnum in BaseEnum.Values)
                yield return baseEnum;

            yield return D;
            yield return E;
        }
    }

    public ChildEnum(string name)
        : base(name)
    {
    }
} 
class Program
{
    static void Main(string[] args)
    {
        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.Where(d => d.GetType() == typeof(ChildEnum)))
        {
            Console.WriteLine(e.Name);
        }
    }
}

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 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
        {
            foreach (var baseEnum in BaseEnum.Values)
                yield return baseEnum;

            yield return D;
            yield return E;
        }
    }

    public ChildEnum(string name)
        : base(name)
    {
    }
} 
浅忆流年 2024-12-10 13:50:05

这是一个不需要令人讨厌的new public static 枚举器的版本。

它确实改变了继承结构以使其正常工作。我想我会提出想法作为替代方案。

public class BaseEnum<E> where E : BaseEnum<E>, new()
{
    public static readonly E A = new E() { Name = "A" };
    public static readonly E B = new E() { Name = "B" };
    public static readonly E C = new E() { Name = "C" };

    public string Name { get; protected set; }

    protected static IEnumerable<E> InternalValues
    {
        get
        {
            yield return A;
            yield return B;
            yield return C;
        }
    }
}

public class BaseEnum : BaseEnum<BaseEnum>
{
    public static IEnumerable<BaseEnum> Values
    {
        get { foreach (var x in InternalValues) yield return x; }
    }
}

public class ChildEnum : BaseEnum<ChildEnum>
{
    public static readonly ChildEnum D = new ChildEnum() { Name = "D" };
    public static readonly ChildEnum E = new ChildEnum() { Name = "E" };

    public static IEnumerable<ChildEnum> Values
    {
        get
        {
            foreach (var x in InternalValues) yield return x;
            yield return D;
            yield return E;
        }
    }
}

它确实还需要一个默认的公共构造函数,但由于 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.

public class BaseEnum<E> where E : BaseEnum<E>, new()
{
    public static readonly E A = new E() { Name = "A" };
    public static readonly E B = new E() { Name = "B" };
    public static readonly E C = new E() { Name = "C" };

    public string Name { get; protected set; }

    protected static IEnumerable<E> InternalValues
    {
        get
        {
            yield return A;
            yield return B;
            yield return C;
        }
    }
}

public class BaseEnum : BaseEnum<BaseEnum>
{
    public static IEnumerable<BaseEnum> Values
    {
        get { foreach (var x in InternalValues) yield return x; }
    }
}

public class ChildEnum : BaseEnum<ChildEnum>
{
    public static readonly ChildEnum D = new ChildEnum() { Name = "D" };
    public static readonly ChildEnum E = new ChildEnum() { Name = "E" };

    public static IEnumerable<ChildEnum> Values
    {
        get
        {
            foreach (var x in InternalValues) yield return x;
            yield return D;
            yield return E;
        }
    }
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文