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;
            }
  • 相关阅读:
    数据库基本设计
    servlet 高级知识之Listener
    servlet 高级知识之Filter
    servlet-生命周期
    http协议概述
    javase高级技术
    javase高级技术
    IO之4种字节流拷贝文件方式对比
    Map 概述
    图解 数组,链表,2种数据结构
  • 原文地址:https://www.cnblogs.com/nanqinling/p/15650004.html
Copyright © 2011-2022 走看看