zoukankan      html  css  js  c++  java
  • C#获取枚举的描述

    方法一:

            public static Dictionary<string, string> GetEnumDescription<T>()
            {
                Dictionary<string, string> dic = new Dictionary<string, string>();
    
                FieldInfo[] fields = typeof(T).GetFields();
    
                foreach (FieldInfo field in fields)
                {
    
                    if (field.FieldType.IsEnum)
                    {
                        object[] attr = field.GetCustomAttributes(typeof(DescriptionAttribute), false);
                        string description = attr.Length == 0 ? field.Name : ((DescriptionAttribute)attr[0]).Description;
                        dic.Add(field.Name, description);
                    }
                }
    
                return dic;
            }

    方法二:

    /// <summary>        
            /// 获取对应的枚举描述        
            /// </summary>        
            public static List<KeyValuePair<string, string>> GetEnumDescriptionList<T>()
            {
                List<KeyValuePair<string, string>> result = new List<KeyValuePair<string, string>>();
    
                FieldInfo[] fields = typeof(T).GetFields();
    
                foreach (FieldInfo field in fields)
                {
                    if (field.FieldType.IsEnum)
                    {
    
                        object[] attr = field.GetCustomAttributes(typeof(DescriptionAttribute), false);
    
                        string description = attr.Length == 0 ? field.Name : ((DescriptionAttribute)attr[0]).Description;
    
                        result.Add(new KeyValuePair<string, string>(field.Name, description));
    
                    }
    
                }
                return result;
            }

    直接调用

    反射

       #region 处理
    
            public List<string> GetProperties(object t)
            {
                try
                {
                    List<string> ListStr = new List<string>();
                    if (t == null)
                    {
                        return ListStr;
                    }
                    System.Reflection.PropertyInfo[] properties = t.GetType().GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);
                    if (properties.Length <= 0)
                    {
                        return ListStr;
                    }
                    foreach (System.Reflection.PropertyInfo item in properties)
                    {
                        string name = item.Name; //名称
                        object value1 = item.GetValue(t);  //一级菜单
                        if (value1 is System.Collections.IEnumerable ieable)
                        {
                            var enumerator = ieable.GetEnumerator();
                            while (enumerator.MoveNext())
                            {
                                var v2 = enumerator.Current;//二级菜单
                            }
                        }
                    }
                    return ListStr;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    return null;
                }
            }
    
            #endregion
       /// <summary>
            /// 将object尝试转为指定对象
            /// </summary>
            /// <param name="data"></param>
            /// <returns></returns>
            public static T ConvertObjToModel<T>(object data)
                where T : new()
            {
                if (data == null) return new T();
                // 定义集合
                T result = new T();
    
                // 获得此模型的类型
                Type type = typeof(T);
                string tempName = "";
    
                // 获得此模型的公共属性
                PropertyInfo[] propertys = result.GetType().GetProperties();
                foreach (PropertyInfo pi in propertys)
                {
                    tempName = pi.Name;  // 检查object是否包含此列
    
                    // 判断此属性是否有Setter
                    if (!pi.CanWrite) continue;
    
                    try
                    {
                        //object value = GetPropertyValue(data, tempName);
                        //if (value != DBNull.Value)
                        //{
                        //    Type tempType = pi.PropertyType;
                        //    pi.SetValue(result, DealHelper.GetDataByType(value, tempType), null);
    
                        //}
                    }
                    catch
                    { }
                }
    
                return result;
            }
    C#.net. WPF.core 技术交流群 群号205082182,欢迎加入,也可以直接点击左侧和下方的"加入QQ群",直接加入
  • 相关阅读:
    主线程——main线程
    进程和线程概念及原理
    抓取网贷之家的数据爬虫
    感知哈希算法的java实现
    最短路径—Dijkstra算法和Floyd算法
    关于图像特征提取
    hive学习之WordCount单词统计
    pig、hive以及hbase的作用
    zookeeper入门知识
    hadoop文件系统浅析
  • 原文地址:https://www.cnblogs.com/aijiao/p/15599138.html
Copyright © 2011-2022 走看看