如何创建一个包含Enumeration子类的Enumeration类

时间:2019-10-22 19:05:06

标签: c# asp.net enumeration

我正在使用.Net Architecture Guide中所述的Enumeration类的概念。 我已经为ParticipantType和ParticipantSubType创建了Enumeration类。

public abstract class ParticipantTypeEnumeration : IComparable
{
  public int ID { get; private set; }
  public string Name { get; private set; }
  public string Description { get; set; }

  public class ParticipantSubTypes : ParticipantSubTypeEnumeration
  {
    // I will substitute code to instantiate the actual subtypes once this is working.
    // I have tried every scenario I can think of to add the sub types to the parent type.
    public static ParticipantSubTypes SubType1 = new ParticipantSubTypes(1001, "SubType1", "This is a SubType1");
    public static ParticipantSubTypes SubType2 = new ParticipantSubTypes(1001, "SubType2", "This is a SubType2");
    public static ParticipantSubTypes SubType3 = new ParticipantSubTypes(1001, "SubType3", "This is a SubType3");

    public ParticipantSubTypes(int id, string name, string description) : base(id, name, description)
    {
    }
  }

  protected ParticipantTypeEnumeration(int id, string name, string description)
  {
    ID = id;
    Name = name;
    Description = description;
  }

  public override string ToString() => Name;

  public int CompareTo(object other) => ID.CompareTo(((ParticipantTypeEnumeration)other).ID);
}

public abstract class ParticipantSubTypeEnumeration : IComparable
{
  public int ID { get; private set; }
  public string Name { get; private set; }
  public string Description { get; set; }

  protected ParticipantSubTypeEnumeration() { }
  protected ParticipantSubTypeEnumeration(int id)
  {
  }
  protected ParticipantSubTypeEnumeration(int id, string name, string description)
  {
    ID = id;
    Name = name;
    Description = description;
  }

  public override string ToString() => Name;

  public static IEnumerable<T> GetAll<T>() where T : ParticipantSubTypeEnumeration
  {
    var fields = typeof(T).GetFields(BindingFlags.Public |
                                     BindingFlags.Static |
                                     BindingFlags.DeclaredOnly);

    return fields.Select(f => f.GetValue(null)).Cast<T>();
  }

  public int CompareTo(object other) => ID.CompareTo(((ParticipantSubTypeEnumeration)other).ID);
}

然后我像这样实现它。

public class ParticipantTypeTest : ParticipantTypeEnumeration
{
  public static ParticipantTypeTest Party = new ParticipantTypeTest(10002, "Party", "This is a party on a case.");
  public static ParticipantTypeTest Participant = new ParticipantTypeTest(10003, "Participant", "This is a participant on a case.");
  public static ParticipantTypeTest Auxiliary = new ParticipantTypeTest(10004, "Auxiliary", "Participant types that should not normally be displayed with Parties and Participants.");

  public ParticipantTypeTest(int id, string name, string description) : base(id, name, description)
  {
  }
}

我希望能够访问此类以获取参与者的值(现在可以正常使用)。

SMC.Classes.ParticipantTypeTest.Auxiliary.ID

并同样获取每种类型的子类型的值。

SMC.Classes.ParticipantTypeTest.Auxiliary.ParticipantSubTypes.SubType1.Name

我所有的尝试都没有成功。此处显示的代码将子类型显示为与类型相同的级别,而不是其下方。

关于以我想要的方式进行这项工作的秘诀是什么?

0 个答案:

没有答案