反射获取属性DisplayName特性名字以及属性值

2019-09-26 17:24 来源:未知

原作:反射获取属性DisplayName性情名字以及属性值

 /// <summary>        /// 反射获取所有DisplayName标记值        /// </summary>        /// <typeparam name="T">实体类型</typeparam>        /// <param name="model">需要获取的实体</param>        /// <returns></returns>        List<string> GetDisplayName<T>        {            //获取所有属性            PropertyInfo[] properties = model.GetType().GetProperties();            var list = new List<string>();            foreach (var item in properties)            {                var attrs = item.GetCustomAttributes(typeof(DisplayNameAttribute), true);                if (attrs != null)                {                    var displayName = ((DisplayNameAttribute)attrs[0]).DisplayName;                    list.Add(displayName);                }            }            return list;        }        /// <summary>        /// 反射获取属性值        /// </summary>        /// <typeparam name="T">实体类型</typeparam>        /// <param name="modelList">需要获取value的实体集合</param>        /// <returns></returns>        List<string> GetValue<T>(T modelList)        {            var list = new List<string>();            var type = modelList.GetType();            var properties = type.GetProperties();            foreach (var item in properties)            {                var pName = item.Name;                PropertyInfo propertyInfo = type.GetProperty;                var value = propertyInfo.GetValue(modelList)?.ToString();                list.Add;            }            return list;        }
TAG标签:
版权声明:本文由www.129028.com-澳门金沙唯一官网www129028com发布于编程新闻,转载请注明出处:反射获取属性DisplayName特性名字以及属性值