public static List <EnumParameter> GetEnumMembers(this Type type) { if (type == null) { throw new ArgumentNullException("type", "输入的枚举类型为空"); } List <EnumParameter> list = new List <EnumParameter>(); foreach (string str in Enum.GetNames(type)) { foreach (Attribute attribute in type.GetField(str).GetCustomAttributes(typeof(EnumAttribute), false)) { EnumAttribute attribute2 = attribute as EnumAttribute; if (attribute2 != null) { EnumParameter item = new EnumParameter { Key = str, Description = attribute2.Description }; if (attribute2.Value == -2147483648) { item.Value = Convert.ToInt32(Enum.Parse(type, str)); } else { item.Value = attribute2.Value; } list.Add(item); break; } } } return(list); }
public EnumHelperT(Type enumType) { this._EnumParameterList = null; this._EnumParameterList = new List <EnumParameter>(); foreach (string str in Enum.GetNames(enumType)) { foreach (Attribute attribute in enumType.GetField(str).GetCustomAttributes(typeof(EnumAttribute), false)) { EnumAttribute attribute2 = attribute as EnumAttribute; if (attribute2 != null) { EnumParameter item = new EnumParameter { Key = str, Description = attribute2.Description }; if (attribute2.Value == -2147483648) { item.Value = Convert.ToInt32(Enum.Parse(enumType, str)); } else { item.Value = attribute2.Value; } this._EnumParameterList.Add(item); break; } } } }
public static List <EnumParameter> GetEnumMembers(this string typeName, string assemblyName) { Type enumType = assemblyName.FindType(typeName); if (enumType == null) { throw new ArgumentNullException("typeName", "输入的类型系统无法找到请重新输入"); } List <EnumParameter> list = new List <EnumParameter>(); foreach (string str in Enum.GetNames(enumType)) { foreach (Attribute attribute in enumType.GetField(str).GetCustomAttributes(typeof(EnumAttribute), false)) { EnumAttribute attribute2 = attribute as EnumAttribute; if (attribute2 != null) { EnumParameter item = new EnumParameter { Key = str, Description = attribute2.Description }; if (attribute2.Value == -2147483648) { item.Value = Convert.ToInt32(Enum.Parse(enumType, str)); } else { item.Value = attribute2.Value; } list.Add(item); break; } } } return((from s in list orderby s.Value select s).ToList <EnumParameter>()); }