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


免责声明!

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



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