C # Features

Source: Internet
Author: User

Add attributes to enumerations, get attribute characters by reflection, and some need to display enumerations in the interface options where they are useful

public static Class Enumdescription
{
<summary>
Gets the description of the enumeration
</summary>
<param name= "Value" > enumeration value </param>
<param name= "Nameinstead" > Whether to use an enumeration name instead of when the enumeration does not define description, using the default </param>
<returns> enumeration of 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;
}
FieldInfo 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;

}
<summary>
The enumeration is converted to a set of key-value pairs.
</summary>
<param name= "Enumtype" > Enum type </param>
<param name= "GetText" > or value Text </param>
<returns> The enumeration value is key, and the enumerated text is a value pair collection </returns>
public static Dictionary<int32, string> enumtodescription (Type enumtype, Func<enum, string> GetText)
{
if (!enumtype.isenum)
{
throw new ArgumentException ("The parameter passed in must be an enumeration type!") "," enumtype ");

}
Dictionary<int, 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;

}
}

The first method is to enumerate the extension methods if the enumeration does not have the description default output as the enumeration name, otherwise the output is empty

The second method is converted to a set of key pairs with an enumeration value of key, description as value, by an enumeration

Instance

public enum Person
{
[Description ("Zhang San")]
Zhangsan=1,
[Description ("John Doe")]
lisi=2,
Wangwu
}

Class Program

{

static void Main (string[] args)

{

Person p = Person.wangwu;
Console.WriteLine (P.getdescription (false));
Console.WriteLine (P.getdescription ());
p = Person.zhangsan;
Console.WriteLine (P.getdescription ());
Dictionary<int32, string> dic = Enumdescription.enumtodescription (typeof (person),
E = E.getdescription ());

Printdic (DIC);

}

static void Printdic (Dictionary<int32, string> dic)
{
foreach (Keyvaluepair<int32, string> item in DIC)
{
Console.WriteLine ("{0}--{1}", item. Key, item. Value);
}
}

}

Results:

Disciption need to refer to using System.ComponentModel;

FieldInfo need to refer to using System.Reflection;

About extension methods:

The extension method enables you to add a method to an existing type without creating a new derived type, recompiling, or otherwise modifying the original type. "

Must be a static class, such as the first method

= = Symbol:

Lamda expression-

E=>e.getdescription ()

Similar

String GetText (Person p)

{

return P.getdescription ();

}

C # Features

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.