随笔 - 148  文章 - 1  评论 - 15  阅读 - 30万

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;
        }
复制代码

 

posted on   冰魂雪魄  阅读(405)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

WPF框架交流群:C#.net. WPF.core 技术交流�      C#WPF技术交流群:C#.net. WPF.core 技术交流�     WPF技术大牛交流群:C#.net. WPF.core 技术交流�
点击右上角即可分享
微信分享提示