- <span style="font-size: 18px; font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"></span><pre name="code" class="csharp">
- public static class EnumKit
- {
- #region 根据枚举生成下拉列表数据源
- /// <summary>
- /// 根据枚举生成下拉列表的数据源
- /// </summary>
- /// <param name="enumType">枚举类型</param>
- /// <param name="firstText">第一行文本(一般用于查询。例如:全部/请选择)</param>
- /// <param name="firstValue">第一行值(一般用于查询。例如:全部/请选择的值)</param>
- /// <returns></returns>
- public static IList<SelectListItem> ToSelectList(Type enumType
- , string firstText = "请选择"
- , string firstValue = "-1")
- {
- IList<SelectListItem> listItem = new List<SelectListItem>();
- if (enumType.IsEnum)
- {
- AddFirst(listItem, firstText, firstValue);
- Array values = Enum.GetValues(enumType);
- if (null != values && values.Length > 0)
- {
- foreach (int item in values)
- {
- listItem.Add(new SelectListItem { Value = item.ToString(), Text = Enum.GetName(enumType, item) });
- }
- }
- }
- else
- {
- throw new ArgumentException("请传入正确的枚举!");
- }
- return listItem;
- }
- static void AddFirst(IList<SelectListItem> listItem, string firstText, string firstValue)
- {
- if (!string.IsNullOrWhiteSpace(firstText))
- {
- if (string.IsNullOrWhiteSpace(firstValue))
- firstValue = "-1";
- listItem.Add(new SelectListItem { Text = firstText, Value = firstValue });
- }
- }
- /// <summary>
- /// 根据枚举的描述生成下拉列表的数据源
- /// </summary>
- /// <param name="enumType"></param>
- /// <returns></returns>
- public static IList<SelectListItem> ToSelectListByDesc(
- Type enumType
- , string firstText = "请选择"
- , string firstValue = "-1"
- )
- {
- IList<SelectListItem> listItem = new List<SelectListItem>();
- if (enumType.IsEnum)
- {
- AddFirst(listItem, firstText, firstValue);
- string[] names = Enum.GetNames(enumType);
- names.ToList().ForEach(item =>
- {
- string description = string.Empty;
- var field = enumType.GetField(item);
- object[] arr = field.GetCustomAttributes(typeof(DescriptionAttribute), true); //获取属性字段数组
- description = arr != null && arr.Length > 0 ? ((DescriptionAttribute)arr[0]).Description : item; //属性描述
- listItem.Add(new SelectListItem() { Value = ((int)Enum.Parse(enumType, item)).ToString(), Text = description });
- });
- }
- else
- {
- throw new ArgumentException("请传入正确的枚举!");
- }
- return listItem;
- }
- #endregion
- #region 获取枚举的描述
- /// <summary>
- /// 获取枚举的描述信息
- /// </summary>
- /// <param name="enumValue">枚举值</param>
- /// <returns>描述</returns>
- public static string GetDescription(this Enum enumValue)
- {
- string value = enumValue.ToString();
- FieldInfo field = enumValue.GetType().GetField(value);
- object[] objs = field.GetCustomAttributes(typeof(System.ComponentModel.DescriptionAttribute), false);
- if (objs == null || objs.Length == 0) return value;
- System.ComponentModel.DescriptionAttribute attr = (System.ComponentModel.DescriptionAttribute)objs[0];
- return attr.Description;
- }
- #endregion
- }
- <span style="font-size: 18px; font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">
- </span>
- <span style="font-size: 18px; font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">调用代码:</span>
- public ActionResult Index()
- {
- IList<SelectListItem> listItem = EnumKit.ToSelectList(typeof(OrderStatus), "全部");
- ViewBag.SelectListItem = listItem;
- IList<SelectListItem> SelectListItemDesc = EnumKit.ToSelectListByDesc(typeof(OrderStatus));
- ViewBag.SelectListItemDesc = SelectListItemDesc;
- // 获取描述特性的值
- string sendHuo = OrderStatus.发货.GetDescription();
- return View();
- }
标签:
反射
【推荐】国内首个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框架的用法!