【问题标题】:idea to create rich enum创建丰富枚举的想法
【发布时间】:2015-03-15 14:56:08
【问题描述】:

我为自己创建了几个具有相同结构的类 - “丰富的枚举”。所以它立即表明它可以通过我将继承的另一个类以某种方式简化。

我编写了一个示例类来向您展示这样的枚举包含什么:

class RichEnum
{
    // ???
}

class MyEnum : RichEnum
{
    // FIELDS AND CONSTRUCTOR(S)
    public readonly string A;
    public readonly int B;
    public readonly Object C;

    public MyEnum(string A, int B, Object C)
    {
        this.A = A;
        this.B = B;
        this.C = C;
    }

    // STATIC INSTANCES
    public static readonly MyEnum Example1 = new MyEnum("string1", 1, "object1");
    public static readonly MyEnum Example2 = new MyEnum("string2", 2, "object2");
    public static readonly MyEnum Example3 = new MyEnum("string3", 3, "object3");

    // SPECIAL INSTANCE
    public static readonly MyEnum Default = new MyEnum("default", 0, "default");

    // SPECIAL OBJECT FOR ITERATING OVER STATIC INSTANCES
    public static readonly MyEnum[] Values = { Example1, Example2, Example3 };

    // METHODS
    public int GetSomeNumber()
    {
        return B + 10;
    }
}

我有很多不同的“MyEnums”,但这些类都具有相同的结构。所以他们有:

  • 各种类型的一些公共只读字段(变量数)
  • 一个或多个构造函数
  • 公共静态只读实例
  • 如果这些实例都不能匹配条件,则用于返回值的特殊字段。例如,当我使用 MyEnum.Values.FirstOrDefault(...) 时,找不到任何内容时会返回默认值
  • 一些方法

有没有办法创建一个这种结构的类,然后从它继承,从而使过程更简化,更不容易出错?

【问题讨论】:

  • 您可以尝试为您的“各种类型”创建一个具有多个类型参数的泛型类。

标签: c# enums


【解决方案1】:

看来你正在重新发明轮子:Headspring.Enumeration

因此,如果您的目标只是为您提供这样一个“丰富”的枚举类,那么可以使用 Nuget 包;如果您的目标是编写自己的作品……那么,从现有艺术中寻找灵感永远不会有什么坏处。

【讨论】:

    【解决方案2】:

    如果你仔细看,这些类实际上没有任何可以抽象的共同点:

    1. 它们具有不同数量的静态和非静态字段
    2. 字段类型不同
    3. 每个字段的值不同
    4. 字段名称不同
    5. 方法不同

    除非您找到它们的共同点,否则您无法创建基类。可以帮助您的是包含此类的一般布局的代码 sn-p 以加快您的打字速度。

    【讨论】:

      【解决方案3】:

      看看我很久以前在 CodeProject 上发布的这段代码。

      http://www.codeproject.com/Articles/13293/Descriptive-Enumerations

      我不会把所有的代码都贴出来,虽然还不错,但这里有一个实际使用它的例子。在这种情况下,我试图定义一个乘客的航空公司座位偏好的枚举。

      public class SeatType : DescriptiveEnum<SeatType, int>
      {
          public static readonly SeatType Window = new SeatType("Window Seat", 1);
          public static readonly SeatType Aisle = new SeatType("Aisle Seat", 2);
          public static readonly SeatType AnythingExceptSeatNearBathroom = new SeatType("Anything Except Seat Near Bathroom", 3);
      
          private SeatType(string desc, int code)
              : base(desc, code)
          {
          }
      }
      

      所以,如您所见,这相当干净。当然,您当然可以扩展它以添加其他方法,或包含“默认”等。

      然后你可以这样使用它:

      class Example
      {
      
          public static void Main(string[] args)
          {
              SeatType c = SeatType.AnythingExceptSeatNearBathroom;
      
              Console.WriteLine(c.Description);
              Console.WriteLine(SeatType.AnythingExceptSeatNearBathroom == c);
              foreach (SeatType seat in SeatType.GetEnumMembers())
              {
                  Console.WriteLine(String.Format("Seat type code: {0} - description: {1}",seat.Code,seat.Description));
              }
      
      
              Console.ReadLine();
          }
      }
      

      基本的 DescriptiveEnum 类还内置了转换运算符,因此它的工作原理与枚举一样。也就是说,你可以说:

      SeatType c = (SeatType) SomeMethodThatReturnsAnInt();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-11-01
        • 1970-01-01
        • 2011-01-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多