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;
            }
  • 相关阅读:
    Nginx+uWsgi+Django+Python+MongoDB+mySQL服务器搭建
    MongoDB的副本集Replica Set
    Google论文之一Bigtable学习翻译
    python类的成员和装饰器
    nginx代码分析进程和模块初始化
    MongoDB的分片Sharding
    MongoDB的分片和副本集部署
    安装sql server 2008 r2
    Hello, World!
    vs2010仿Myeclipse open file 功能插件
  • 原文地址:https://www.cnblogs.com/nanqinling/p/15650004.html
Copyright © 2011-2022 走看看