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;
            }
        }

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM