获取实体属性名称(Property)和DisplayName属性名称(Attribute)

代码:

public Dictionary<string, string> XXXModelColumnName
        {
            get
            {
                var dic = new Dictionary<string, string>();
                PropertyInfo[] fields = typeof(XXXModel).GetProperties();
                foreach (PropertyInfo field in fields)
                {
                    var fieldName = field.Name;
                    var displayName = field.GetCustomAttribute<DisplayNameAttribute>();
                    dic.Add(fieldName, displayName == null ? fieldName : displayName.DisplayName);
                }
                return dic;
            }
        }

 

posted @ 2016-06-07 09:46  talentzemin  阅读(1636)  评论(0编辑  收藏  举报