【问题标题】:Creating a list from ENUM into Model从 ENUM 创建一个列表到模型中
【发布时间】:2011-12-13 17:40:21
【问题描述】:

我得到了以下模型代码:

public enum EnumTest
{
  [Description ("Enum Text 1")]
  Value_1 = 1,

  [Description ("Enum Text 2")]
  Value_2 = 2,
}

public List<Fields> listFields = new List<Fields>();

public class Fields
{
  public int Code { get; set;}
  public string Description { get; set;}
}

我有一个枚举,我想用枚举值填充我的变量 CODE,用相同的枚举描述填充变量 Description。我查了很长时间,但未能使用枚举 VALUE/DESCRIPTION 将我的“ListFields”初始化到它的构造函数中。

我已经得到了枚举和获取它的描述的方法..我发现它很有用,所以我把它留在这里,也许它对某人有用..

 public static string GetDescription(this Enum value)
 {
     return (from m in value.GetType().GetMember(value.ToString())
             let attr =(DescriptionAttribute)m.GetCustomAttributes(typeof(DescriptionAttribute), false).FirstOrDefault()
             select attr == null ? value.ToString() : attr.Description).FirstOrDefault();
  }

要使用它,您只需执行以下操作:

String xx = Enum.EnumName.GetDescription();

【问题讨论】:

    标签: asp.net-mvc model-view-controller asp.net-mvc-2 c#-3.0


    【解决方案1】:

    你必须使用反射。

        public static Fields[] GetEnumFields(Type enumType)
        {
            if (enumType == null)
                throw new ArgumentNullException("enumType");
            if (!enumType.IsEnum)
                throw new ArgumentException("Not an enum");
    
            FieldInfo[] fieldInfos = enumType.GetFields(BindingFlags.Static | BindingFlags.Public);
    
            Fields[] result = new Fields[fieldInfos.Length];
            for (int i = 0; i < fieldInfos.Length; ++i)
            {
                FieldInfo field = fieldInfos[i];
    
                int value = (int)field.GetValue(null);
    
                DescriptionAttribute attrib = Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute)) as DescriptionAttribute;
                string desc = attrib != null ? attrib.Description : field.Name;
    
                result[i] = new Fields(value, desc);
            }
    
            return result;
        }
    
        public class Fields
        {
            private int value;
            private string description;
    
            public int Value
            {
                get { return this.value; }
            }
    
            public string Description
            {
                get { return this.description; }
            }
    
            public Fields(int value, string description)
            {
                this.value = value;
                this.description = description;
            }
        }
    

    使用起来很简单:

        enum test
        {
            [Description("hello!")]
            ciao,
    
            www
        }
    
        static void Main(string[] args)
        {
            foreach (Fields f in GetEnumFields(typeof(test)))
            {
                Console.WriteLine(f.Description);
            }
        }
    

    在我的实现中,当找不到描述属性时,使用字段名称。

    我们还必须说,反射可能很慢,如果您经常需要,在需要时重建整个数组是浪费时间。 您可以将数组存储在某处,这样您就可以只计算一次并保持缓存。 当然,正如我所说,只有在您经常需要此只读列表时才有意义。

    【讨论】:

    • 很好的解决方案,伙计!像魅力一样工作..很难让它返回任何数组吗?我有一堆代码可以使用你发布的这个方法。我不会只适用于“字段”集合。
    • 对不起,我不明白这个问题。你是什​​么意思?这个函数返回一个字段类数组,如你所问,你希望它返回什么?
    • 一个包含什么、描述或值的数组?还是?
    • 只是一个错误..它肯定解决了我的问题!感谢您的帮助:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-04
    相关资源
    最近更新 更多