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