在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property | AttributeTargets.Enum)] public class EnumSignAttribute : Attribute { // Fields private string _displayName; public EnumSignAttribute(string displayname) { this._displayName = displayname; } // Properties public string DisplayName { get { return this._displayName; } set { this._displayName = value; } } } 枚举
?123456789101112public enum DataType { [EnumSign("布尔类型")] Boolean = 4, [EnumSign("日期时间")] DateTime = 3, None = 0, [EnumSign("数字")] Numberic = 2, [EnumSign("字符串")] String = 1 }
public static class EnumsUtils { /// <summary> /// 根据object类型的数据,获取枚举类型 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="enumValue"></param> /// <returns></returns> public static T ConvertToEnum<T>(object enumValue) { if (enumValue is int) { return (T)enumValue; } Type type = typeof(T); FieldInfo[] fields = type.GetFields(); int num = 0; foreach (FieldInfo info in fields) { if (num == 0) { num++; } else { T local = (T)info.GetValue(type); if (local.ToString().Equals(enumValue)) { return local; } } } return default(T); } /// <summary> /// 根据枚举标识,获取显示名字 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="enumValue">枚举标识</param> /// <returns>显示名字</returns> public static string GetAttriValue<T>(T enumValue) { Type type = typeof(T); FieldInfo[] fields = type.GetFields(); int num = 0; foreach (FieldInfo info in fields) { if (num == 0) { num++; } else { T local = (T)info.GetValue(type); if (local.Equals(enumValue)) { return GetDisplayName(info); } } } return string.Empty; } private static string GetDisplayName(FieldInfo field) { string displayName = string.Empty; object[] arr = field.GetCustomAttributes(typeof(EnumSignAttribute), true); if (arr.Length > 0) { EnumSignAttribute aa = (EnumSignAttribute)arr[0]; displayName = aa.DisplayName; } return displayName; } /// <summary> /// 获取枚举集合列表 /// </summary> /// <param name="type"></param> /// <returns></returns> public static EnumList GetSourceEnum(Type type) { EnumList list = new EnumList(); FieldInfo[] fields = type.GetFields(); foreach (FieldInfo field in fields) { EnumItem item = new EnumItem(); if (field.FieldType.IsEnum) { item.Value = ((int)type.InvokeMember(field.Name, BindingFlags.GetField, null, null, null)); object[] arr = field.GetCustomAttributes(typeof(EnumSignAttribute), true); if (arr.Length > 0) { EnumSignAttribute aa = (EnumSignAttribute)arr[0]; item.Display = aa.DisplayName; } item.Name = field.Name; list.Add(item); } } return list; } } [StructLayout(LayoutKind.Sequential)] public struct EnumItem { private string _display; private string _name; private object _value; public EnumItem(string display, string name, object value) { this._display = display; this._name = name; this._value = value; } public string Display { get { return this._display; } set { this._display = value; } } public string Name { get { return this._name; } set { this._name = value; } } public object Value { get { return this._value; } set { this._value = value; } } } 获取枚举集合列表
public class EnumList : BindingList<EnumItem> { } //方法1: EnumList = EnumsUtils.GetSourceEnum.(typeof(DataType)); //方法2: var = EnumsUtils.GetSourceEnum.(typeof(DataType)); 数据源
|
请发表评论