C # enumeration customization for data binding .,

Source: Internet
Author: User

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

 



 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.