C# 获取属性的displayName
生活随笔
收集整理的這篇文章主要介紹了
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;}
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; }
總結
以上是生活随笔為你收集整理的C# 获取属性的displayName的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 阳台适宜面向什么方位?
- 下一篇: C#反射调用 异常信息:Ambiguou