【问题标题】:Enum with object or a dictionary-like enum带有对象的枚举或类似字典的枚举
【发布时间】:2011-03-09 19:11:48
【问题描述】:

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

string foo = FileName.Hello; //Returns "Hello.txt"
string foo1 = FileName.Bye; //Returns "GoodBye.doc"

或者它可以是这样的对象:

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

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

【问题讨论】:

    标签: c#


    【解决方案1】:

    虽然这个问题很奇怪或者没有完全解释清楚,但这里是字面的解决方案:

    选项 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");
    }
    

    【讨论】:

    • 仅供参考,这仍然感觉“奇怪”(也许是因为我不知道你实际上在做什么)。我认为这只是一个测试应用程序或学校作业,但像这样的硬编码是不“正常的”。
    • 我基本上使用模板系统,而​​不是传递文件名的字符串,而是将文件名存储在其他位置并使用强类型属性访问它。
    【解决方案2】:

    只需使用 Dictionary<People, Person> 即可:

     enum People { Bill, Bob};
    
     var myDict = new Dictionary<People, Person>();
    
     myDict.Add(People.Bill, new Person() { FirstName = "Bill", LastName = "Jobs" });
    

    现在您可以使用以下语法找回 Bill:

    Person p = myDict[People.Bill];
    

    【讨论】:

      【解决方案3】:

      您可以在 Enum 对象上扩展方法以返回特定值。

      看看:

      http://pietschsoft.com/post/2008/07/C-Enhance-Enums-using-Extension-Methods.aspx

      【讨论】:

      • 这个答案看起来很不错......将元数据信息与.Net中的枚举相关联的正确方法。引用:“那么,你不能接受什么?决定两个选项中哪个更难,然后做另一个。这样一来,无论你的选择多么艰难,至少你可以在知道自己在避免更糟糕的事情中找到安慰。” ——约瑟芬·安吉里尼
      • @FaisalMq 同意。这是一个很棒的报价。
      • 链接已关闭。这就是仅链接答案的问题...
      • @testing 如果我能看到 10 年前当我发布这篇文章时堆栈溢出成为过时的文档存档的未来就好了。我会努力做得更好。
      【解决方案4】:

      Here's an article 在 CodeProject 上,它将向您展示如何创建一个属性,您可以将其应用于每个枚举成员以为其提供一些“额外”数据(如您的文件名,在这种情况下),您可以在代码的其他地方使用这些数据。

      【讨论】:

      • 链接已关闭。这就是仅链接答案的问题...
      【解决方案5】:

      【讨论】:

        【解决方案6】:

        你可以使用带值的静态类..

        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 或FileNames.Hello。它不会具有与枚举相同的属性,并且您的方法需要将字符串或 Person 作为参数。

        【讨论】:

          【解决方案7】:

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

          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
          }
          

          允许简单使用:

          Person steve = PeopleList.Steve;
          Console.WriteLine(steve.FirstName); //Steve
          

          【讨论】:

            【解决方案8】:

            我会使用 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;
                }
            

            【讨论】:

              猜你喜欢
              • 2013-09-02
              • 1970-01-01
              • 2010-11-14
              • 1970-01-01
              • 2020-03-14
              • 1970-01-01
              • 1970-01-01
              • 2021-12-22
              • 1970-01-01
              相关资源
              最近更新 更多