比如我有一个枚举
public enum _SourceType
{
  [Description("物料")]
   Storage_Goods,
}我要如何获取到"物料"这个说明信息呢?如果这样不行,那么还有其它方法可以为枚举加说明信息吗?这个说明信息要在其它地方能够能过编程方式获取的到。

解决方案 »

  1.   

    写一个返回字符串数组或是返回list的方法,这个方法调用就返回类成员的相关说明信息。
      

  2.   

    这样不太好,我还得建立类成员与说明信息的对应信息,我是想直接获取成员的attribute信息。
      

  3.   

    http://topic.csdn.net/u/20090508/18/3d90c559-c1e2-40a1-b7b2-158cb524390c.html
      

  4.   

    基本原来类似下面的代码
                Type type = typeof(Button);
                Object[] attrs = type.GetCustomAttributes(typeof(DescriptionAttribute), true);
                if (attrs != null && attrs.Length > 0)
                {                System.ComponentModel.DescriptionAttribute desAttr = attrs[0] as System.ComponentModel.DescriptionAttribute;
                    MessageBox.Show(desAttr.Description);
                }
      

  5.   

    http://topic.csdn.net/u/20110614/23/8bc008eb-c7f0-46d2-8353-c82240f09290.html
      

  6.   


                DescriptionAttribute[] das = typeof(_SourceType).GetField("Storage_Goods")
                    .GetCustomAttributes(typeof(DescriptionAttribute), false)
                    .OfType<DescriptionAttribute>().ToArray();
                if (das.Length > 0)
                    Console.WriteLine(das[0].Description);
    或者:
                Console.WriteLine(typeof(_SourceType).GetField("Storage_Goods")
                    .GetCustomAttributesData()[0].ConstructorArguments[0].Value);
      

  7.   

    foreach (PropertyInfo var in t.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.DeclaredOnly | BindingFlags.Static))
                {
                    foreach (object item in var.GetCustomAttributes(false))
                    {
                        if (item is DescriptionAttribute)
                        {
                            MessageBox.Show(var.Name+"----"+(item as DescriptionAttribute).Description);
                        }
                    }
                }t是你的类