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

问题描述 投票:0回答:1

我正在使用.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

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

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

c# asp.net enumeration
1个回答
0
投票

AuxiliaryParticipantTypeTest的实例,并且ParticipantTypeTest没有名为ParticipantSubTypes的静态成员。它具有该名称的子类,但是在C#中解析名称的方式不能从属性名称更改为该属性的声明类型的子类的名称。您可以做的就是这个,我不喜欢。希望您更喜欢它。

public class ParticipantSubType : ParticipantSubTypeEnumeration
{
    public ParticipantSubType(int id, string name, string description) : base(id, name, description)
    {
    }
}

public class ParticipantSubTypes
{
    // 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 readonly ParticipantSubType SubType1 = new ParticipantSubType(1001, "SubType1", "This is a SubType1");
    public readonly ParticipantSubType SubType2 = new ParticipantSubType(1001, "SubType2", "This is a SubType2");
    public readonly ParticipantSubType SubType3 = new ParticipantSubType(1001, "SubType3", "This is a SubType3");

    public static ParticipantSubTypes Instance { get; } = new ParticipantSubTypes();

    private ParticipantSubTypes()
    {
    }
}

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 ParticipantSubTypes ParticipantSubTypes { get; } = ParticipantSubTypes.Instance;

    public ParticipantTypeTest(int id, string name, string description) : base(id, name, description)
    {
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.