C# 获取属性字段上DescriptionAttribute的值

  var ent = new Ent();
            foreach (var item in ent.GetType().GetProperties())
            {
                var v = (DescriptionAttribute[])item.GetCustomAttributes(typeof(DescriptionAttribute), false);
                var descriptionName = v[0].Description;

                item.SetValue(ent,descriptionName+":1");
            }

 

 

        private class EnumHelper
        {
            /// <summary>
            /// 获取枚举值上的Description特性的说明
            /// </summary>
            /// <typeparam name="T">枚举类型</typeparam>
            /// <param name="obj">枚举值</param>
            /// <returns>特性的说明</returns>
            public static string GetEnumDescription<T>(T obj)
            {
                var type = obj.GetType();
                FieldInfo field = type.GetField(Enum.GetName(type, obj));
                DescriptionAttribute descAttr = Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute)) as DescriptionAttribute;
                if (descAttr == null)
                {
                    return string.Empty;
                }

                return descAttr.Description;
            }
        }

 

posted @ 2014-05-20 10:51    阅读(18691)  评论(0编辑  收藏  举报