123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Reflection;
- using System.Text;
- namespace XYY.Core.Standard.Mvc
- {
- public class EnumItem
- {
- public string Name
- {
- get; set;
- }
- public int Value
- {
- get; set;
- }
- }
- public class EnumItem2
- {
- public string Name
- {
- get; set;
- }
- public int Id
- {
- get; set;
- }
- }
- public static class EnumsHelper
- {
- public static List<EnumItem2> GetEnumItemsDes<T>() where T : Enum
- {
- List<EnumItem2> enumItems = new List<EnumItem2>();
- var t = typeof(T);
- if (t.IsEnum)
- {
- var values = t.GetEnumValues();
- foreach (var v in values)
- {
- FieldInfo field = t.GetField(t.GetEnumName(v));
- DescriptionAttribute attribute = Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute)) as DescriptionAttribute;
- enumItems.Add(new EnumItem2
- {
- Id = (int)v,
- Name = attribute == null ? field.Name : attribute.Description
- });
- }
- }
- return enumItems;
- }
- public static List<EnumItem2> GetEnumItems(string enumName)
- {
- List<EnumItem2> enumItems = new List<EnumItem2>();
- string fileName = System.IO.Path.Combine(AppContext.BaseDirectory, "XYY.Model.Standard.dll");
- var ass = Assembly.LoadFile(fileName);
- var t = ass.GetType("XYY.Model.Standard." + enumName);
- if (t != null)
- {
- if (t.IsEnum)
- {
- var values = t.GetEnumValues();
- foreach (var v in values)
- {
- enumItems.Add(new EnumItem2
- {
- Id = (int)v,
- Name = t.GetEnumName(v)
- });
- }
- }
- }
- return enumItems;
- }
-
- public static List<EnumItem> GetEnumItems(Type type)
- {
- string[] Names = Enum.GetNames(type);
- var valueAraay = Enum.GetValues(type);
- List<EnumItem> list = new List<EnumItem>();
- for (int i = 0; i < Names.Length; i++)
- {
- list.Add(new EnumItem
- {
- Name = Names[i],
- Value = (int)valueAraay.GetValue(i)
- });
- }
- return list;
- }
- public static List<EnumItem2> GetEnumItemsDes(string enumName)
- {
- List<EnumItem2> enumItems = new List<EnumItem2>();
- string fileName = System.IO.Path.Combine(AppContext.BaseDirectory, "XYY.Model.Standard.dll");
- var ass = Assembly.LoadFile(fileName);
- var t = ass.GetType("XYY.Model.Standard." + enumName);
- if (t != null)
- {
- if (t.IsEnum)
- {
- var values = t.GetEnumValues();
- foreach (var v in values)
- {
- FieldInfo field = t.GetField(t.GetEnumName(v));
- DescriptionAttribute attribute = Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute)) as DescriptionAttribute;
- enumItems.Add(new EnumItem2
- {
- Id = (int)v,
- Name = attribute == null ? "未知" : attribute.Description
- });
- }
- }
- }
- return enumItems;
- }
- }
- }
|