C # enumeration customization for data binding .,
[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 ;}}}Enumeration
Public enum DataType {[EnumSign ("Boolean Type")] Boolean = 4, [EnumSign ("DateTime")] DateTime = 3, None = 0, [EnumSign ("Number")] Numberic = 2, [EnumSign ("String")] String = 1}
Public static class EnumsUtils {// <summary> // Based on the object type data, obtain the enumeration type /// </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> // identifies an enum, obtain the display name /// </summary> /// <typeparam name = "T"> </typeparam> /// <param name = "enumValue"> enumeration id </ param> // <returns> display name </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 (enumsignattrites), true); if (arr. length> 0) {EnumSignAttribute aa = (EnumSignAttribute) arr [0]; displayName = aa. displayName;} return displayName ;} /// <summary> /// obtain the list of enumerated sets /// </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); object [] arr = field. getCustomAttributes (typeof (enumsignattrites), 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 ;}}}Obtain the public class EnumList: BindingList <EnumItem >{}// Method 1: EnumList = EnumsUtils. getSourceEnum. (typeof (DataType); // Method 2: var = EnumsUtils. getSourceEnum. (typeof (DataType ));Data Source