zoukankan      html  css  js  c++  java
  • 获取实体类里面所有的名称、DESCRIPTION值

            //获取实体类里面所有的名称、DESCRIPTION值
            public Dictionary<string, string> GetDescription<T>(T t)
            {
                Dictionary<string, string> dic = new Dictionary<string, string>();
    
                PropertyInfo[] ps = t.GetType().GetProperties();
                foreach (var item in ps)
                {
                    if (item.CustomAttributes.Where(p => p.AttributeType.Name.Contains("DescriptionAttribute")).FirstOrDefault() != null)
                    {
                        var des = ((DescriptionAttribute)Attribute.GetCustomAttribute(item, typeof(DescriptionAttribute))).Description;
                        dic.Add(item.Name, des);
                    }
                }
                return dic;
            }
    
    
    
            //获取实体类里面所有的名称、值、DESCRIPTION值
            public string GetProperties<T>(T t)
            {
                string tStr = string.Empty;
                if (t == null)
                {
                    return tStr;
                }
                PropertyInfo[] properties = t.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public);
    
                if (properties.Length <= 0)
                {
                    return tStr;
                }
                foreach (PropertyInfo item in properties)
                {
                    string name = item.Name; //名称
                    object value = item.GetValue(t, null);  //
                    string des = ((DescriptionAttribute)Attribute.GetCustomAttribute(item, typeof(DescriptionAttribute))).Description;// 属性值
    
                    if (item.PropertyType.IsValueType || item.PropertyType.Name.StartsWith("String"))
                    {
                        tStr += string.Format("{0}:{1}:{2},", name, value, des);
                    }
                    else
                    {
                        GetProperties(value);
                    }
                }
                return tStr;
            }
  • 相关阅读:
    周日讲课材料下载
    基础图论练习题
    邻接表存图的小trick(存多个图)
    0/1分数规划
    四道期望题
    基础线性代数大记(二)三道高消题
    基础线性代数大记 (一)前言与行列式的定义
    概率期望小记
    基础线性代数小记
    给二维数组排版
  • 原文地址:https://www.cnblogs.com/nanqinling/p/15650004.html
Copyright © 2011-2022 走看看