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群",直接加入
  • 相关阅读:
    UVA 10462 Is There A Second Way Left?(次小生成树&Prim&Kruskal)题解
    POJ 1679 The Unique MST (次小生成树)题解
    POJ 2373 Dividing the Path (单调队列优化DP)题解
    BZOJ 2709 迷宫花园
    BZOJ 1270 雷涛的小猫
    BZOJ 2834 回家的路
    BZOJ 2506 calc
    BZOJ 3124 直径
    BZOJ 4416 阶乘字符串
    BZOJ 3930 选数
  • 原文地址:https://www.cnblogs.com/aijiao/p/15599138.html
Copyright © 2011-2022 走看看