C # define DescriptionAttribute for enumeration and convert enumeration to key-value pairs,

Source: Internet
Author: User

C # define DescriptionAttribute for enumeration and convert enumeration to key-value pairs,

In C #, it is convenient to use enumeration to set the state value. For example, I define an enumeration called Season.

public enum Season    {        Spring = 1,        Summer = 2,        Autumn = 3,        Winter = 4    }

The enumeration name cannot contain spaces, ()-/or other characters.

We want to display Spring as Spring. We need to define our own instructions. We can use DescriptionAttribute, as shown below:

Public enum Season {[Description ("Spring")] Spring = 1, [Description ("Summer")] Summer = 2, // [Description ("Autumn")] Autumn = 3, [Description ("Winter")] Winter = 4}

Next we will write an extension method to get the enumeration description, as shown below:

/// <Summary> /// extension method, obtain the enumerated Description /// </summary> /// <param name = "value"> enumerated value </param> /// <param name = "nameInstead"> when descriptionAttribute is not defined for the enumerated value, whether to use enumeration name instead. The default value is </param> // <returns> enumeration Description </returns> public static string GetDescription (this Enum value, Boolean nameInstead = true) {Type type = value. getType (); string name = Enum. getName (type, value); if (name = null) {return null;} F IeldInfo field = type. getField (name); DescriptionAttribute attribute = Attribute. getCustomAttribute (field, typeof (DescriptionAttribute) as DescriptionAttribute; if (attribute = null & nameInstead = true) {return name;} return attribute = null? Null: attribute. Description ;}

Convert enumeration to a set of key-value pairs

/// <Summary> /// convert enumeration to a key-value pair set /// </summary> /// <param name = "enumType"> Enumeration type </param> /// <param name = "getText"> obtain the worthy text </param> /// <returns> use the enumerated value as the key, set of key-value pairs whose enumerated text is value </returns> public static Dictionary <Int32, String> EnumToDictionary (Type enumType, Func <Enum, String> getText) {if (! EnumType. IsEnum) {throw new ArgumentException ("the input parameter must be of the enumeration type! "," EnumType ");} Dictionary <Int32, String> enumDic = new Dictionary <int, string> (); Array enumValues = Enum. getValues (enumType); foreach (Enum enumValue in enumValues) {Int32 key = Convert. toInt32 (enumValue); String value = getText (enumValue); enumDic. add (key, value);} return enumDic ;}

We put the above two methods into the static class named EnumUtil.

The following is how to use

Public enum Season {[Description ("Spring")] Spring = 1, [Description ("Summer")] Summer = 2, // [Description ("Autumn")] Autumn = 3, [Description ("Winter")] Winter = 4} class Program {static void Main (string [] args) {Season spring = Season. spring; // print the enumerated name Console. writeLine (spring. toString (); // print enumeration description Console. writeLine (spring. getDescription (); // The enumerated values are converted to the key-value pair set Dictionary <Int32, String> dic = EnumUtil. enumToDictionary (typeof (Season), e => e. getDescription (); PrintDic (dic); dic = EnumUtil. enumToDictionary (typeof (Season), e => e. getDescription (false); PrintDic (dic); dic = EnumUtil. enumToDictionary (typeof (Season), e => e. toString (); PrintDic (dic); dic = EnumUtil. enumToDictionary (typeof (Season), e => Enum. getName (typeof (Season), e); PrintDic (dic); Console. readLine ();} private static void PrintDic (Dictionary <Int32, String> dic) {foreach (KeyValuePair <Int32, String> item in dic) {Console. writeLine ("Key: {0} ----- Value: {1}", item. key, item. value );}}}

The output result is as follows:


Spring
Spring Day
Key: 1 ----- Value: Spring Day
Key: 2 ----- Value: Summer
Key: 3 ----- Value: Autumn
Key: 4 ----- Value: winter
Key: 1 ----- Value: Spring Day
Key: 2 ----- Value: Summer
Key: 3 ----- Value:
Key: 4 ----- Value: winter
Key: 1 ----- Value: Spring
Key: 2 ----- Value: Summer
Key: 3 ----- Value: Autumn
Key: 4 ----- Value: Winter
Key: 1 ----- Value: Spring
Key: 2 ----- Value: Summer
Key: 3 ----- Value: Autumn
Key: 4 ----- Value: Winter

 

Source code: http://files.cnblogs.com/jm6041/EnumUtil.rar


& In C Language

& Can be used as the bitwise AND or address fetch Operator
The following describes two usage methods:
1. bitwise and operation bitwise AND operator "&" are binary operators. Its function is the binary phase corresponding to the two numbers involved in the operation. The result bit is 1 only when the two binary numbers are 1. Otherwise, the result bit is 0. The number of involved operations is supplemented.
For example, 9 & 5 can be written as follows: 00001001 (Binary complement of 9) & 00000101 (Binary complement of 5) 00000001 (Binary complement of 1) Visible 9 & 5 = 1.
Bitwise AND operations are usually used to clear some bits or retain some bits. For example, if a clears the high eight bits of 0 and retains the low eight bits, it can be used as a & 255 operation (255 of the binary number is 0000000011111111 ).
2. Get the address
& As The unary operator, the result is the address of the right operation object.
For example, & x returns the address of x.
The address itself is an abstract concept used to indicate the logical location of an object in the memory.

& In C Language

& Can be used as the bitwise AND or address fetch Operator
The following describes two usage methods:
1. bitwise and operation bitwise AND operator "&" are binary operators. Its function is the binary phase corresponding to the two numbers involved in the operation. The result bit is 1 only when the two binary numbers are 1. Otherwise, the result bit is 0. The number of involved operations is supplemented.
For example, 9 & 5 can be written as follows: 00001001 (Binary complement of 9) & 00000101 (Binary complement of 5) 00000001 (Binary complement of 1) Visible 9 & 5 = 1.
Bitwise AND operations are usually used to clear some bits or retain some bits. For example, if a clears the high eight bits of 0 and retains the low eight bits, it can be used as a & 255 operation (255 of the binary number is 0000000011111111 ).
2. Get the address
& As The unary operator, the result is the address of the right operation object.
For example, & x returns the address of x.
The address itself is an abstract concept used to indicate the logical location of an object in the memory.

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.