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群",直接加入
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 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框架的用法!