C# 带有对象的枚举或类似字典的枚举

C# 带有对象的枚举或类似字典的枚举,c#,C#,我想创建一个名称列表,并将其作为强类型枚举访问。例如 string foo = FileName.Hello; //Returns "Hello.txt" string foo1 = FileName.Bye; //Returns "GoodBye.doc" 或者它可以是一个类似以下的对象: Person p = PeopleList.Bill; //p.FirstName = "Bill", p.LastName = "Jobs" 如何创建这样的数据类型?只需使用字典即可: enum P

我想创建一个名称列表,并将其作为强类型枚举访问。例如

string foo = FileName.Hello; //Returns "Hello.txt"
string foo1 = FileName.Bye; //Returns "GoodBye.doc"
或者它可以是一个类似以下的对象:

Person p = PeopleList.Bill; //p.FirstName = "Bill", p.LastName = "Jobs"

如何创建这样的数据类型?

只需使用
字典即可:

 enum People { Bill, Bob};

 var myDict = new Dictionary<People, Person>();

 myDict.Add(People.Bill, new Person() { FirstName = "Bill", LastName = "Jobs" });

可以在枚举对象上使用扩展方法来返回特定值

看看:


关于CodeProject,它将向您展示如何创建一个属性,您可以将该属性应用于每个枚举成员,从而为其提供一些“额外”数据(在本例中为您的文件名),您可以在代码中的其他地方使用这些数据。

您可以使用struct()

尽管这个问题很奇怪,或者没有完全解释,但下面是文字解决方案:

备选案文1:

public static class FileName
{
    public const string Hello = "Hello.txt";
    public const string GoodBye= "GoodBye.doc";
}
备选案文2:

public class Person
{
    public string FirstName {get; set; }
    public string LastName {get; set; }

    public Person(string firstName, string lastName)
    {
        this.FirstName = firstName;
        this.LastName = lastName;
    }
}

public static class PeopleList
{
    public static Person Bill = new Person("Bill", "Jobs");
}

您可以使用带有值的静态类

public static class PeopleList
{
   public static readonly Person Bill = new Person("Bill", "Jobs");
   public static readonly Person Joe = new Person("Joe", "Doe");
}

public static class FileNames
{
   public static readonly string Hello = "Hello.txt";
   public static readonly string Bye = "Byte.txt";
}

然后您可以将它们引用为
PeopleList.Bill
filename.Hello
。它不会具有与枚举相同的属性,并且您的方法需要使用字符串或Person作为参数

对于第二个示例,这是一个使用属性的顶级解决方案。注意这段代码有很多问题,只是一个例子

public static T GetValue<T>(this Enum e) where T:class
{
    FieldInfo fi = e.GetType().GetField(e.ToString());
    var valueAttribute = fi.GetCustomAttributes(typeof (ValueAttribute), 
        false).FirstOrDefault() as ValueAttribute;
    if (valueAttribute != null) return valueAttribute.Value as T;
    return null;
}

class PersonValueAttribute : ValueAttribute
{
    public PersonValueAttribute(string firstName, string lastName)
    {
        base.Value = new Person {FirstName = firstName, LastName = lastName};
    }
}

class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }

    public static implicit operator Person(Enum e)
    {
        return e.GetValue<Person>();
    }
}

enum PeopleList
{
    [PersonValue("Steve", "Jobs")]
    Steve
}

我将使用Description属性将自定义数据附加到枚举。然后可以使用此方法返回描述的值:

    public static string GetEnumDescription(Enum value)
    {
         FieldInfo fi = value.GetType().GetField(value.ToString());
         DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);
         string description = (attributes.Length > 0) ? attributes[0].Description : string.Empty;
         return description;
    }

仅供参考,这仍然感觉“奇怪”(可能是因为我不知道你实际上在做什么)。我假设这只是一个测试应用程序或学校作业,但像这样的硬编码不是“正常的”。我基本上使用一个模板系统,而不是传递一个字符串作为文件名,我将文件名存储在其他位置,并使用强类型属性访问它。这个答案看起来很好。。。将元数据信息与.Net中的枚举关联的正确方法。引用:“那么,你不能接受什么呢?决定这两个选项中哪一个更难,然后做另一个。这样,不管你的选择有多难,至少你可以在知道你正在避免更糟糕的事情时找到安慰。”―约瑟芬Angelini@FaisalMq同意。这是一句很棒的话,链接断开了。这就是链接唯一答案的问题…链接已关闭。这就是链接式答案的问题。。。
Person steve = PeopleList.Steve;
Console.WriteLine(steve.FirstName); //Steve
    public static string GetEnumDescription(Enum value)
    {
         FieldInfo fi = value.GetType().GetField(value.ToString());
         DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);
         string description = (attributes.Length > 0) ? attributes[0].Description : string.Empty;
         return description;
    }