zoukankan      html  css  js  c++  java
  • 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.  
      }
  • 相关阅读:
    MCU开发之I2C通信
    hibernate特殊的映射
    Hibernate使用
    css设置让a标签充满整个li
    margin
    border属性
    列表
    链接样式
    相机内参外参
    tmux
  • 原文地址:https://www.cnblogs.com/soundcode/p/14140444.html
Copyright © 2011-2022 走看看