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