C# 获取属性的displayName

public static Dictionary<string, decimal?> DisplayNameModel<T>(T t)
        {
            Type type = typeof(T);
            PropertyInfo[] properties = type.GetProperties();
            Dictionary<string, decimal?> dic = new Dictionary<string, decimal?>();
            foreach (var p in properties)
            {
                //display名字
                var name = p.GetCustomAttribute<DisplayNameAttribute>().DisplayName;
                //对应的值
                var value = t.GetType().GetProperty(p.Name).GetValue(t, null);
                dic.Add(name, Convert.ToDecimal(value));
            }
            return dic;
        }

  

  1. public static Dictionary<string, decimal?> DisplayNameModel<T>(T t)
  2.  
    {
  3.  
    Type type = typeof(T);
  4.  
    PropertyInfo[] properties = type.GetProperties();
  5.  
    Dictionary<string, decimal?> dic = new Dictionary<string, decimal?>();
  6.  
    foreach (var p in properties)
  7.  
    {
  8.  
    //display名字
  9.  
    var name = p.GetCustomAttribute<DisplayNameAttribute>().DisplayName;
  10.  
    //对应的值
  11.  
    var value = t.GetType().GetProperty(p.Name).GetValue(t, null);
  12.  
    dic.Add(name, Convert.ToDecimal(value));
  13.  
    }
  14.  
    return dic;
  15.  
    }
posted @ 2020-12-15 19:06  左正  阅读(2097)  评论(0编辑  收藏  举报