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

时间:2023-12-15 14:37:50

代码:

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