Gets the expansion method of the enum.

Source: Internet
Author: User

public static Class Enumextention
{
<summary>
Gets the description information for the enumeration
</summary>
<typeparam name= "T" ></typeparam>
<returns></returns>
public static Dictionary<int, string> getdescription<t> ()
Where T:struct, IComparable, IFormattable, iconvertible
{
var t = typeof (T);
if (t.isenum)
{

Dictionary<int, string> list = new Dictionary<int, string> ();
Array a = enum.getvalues (t);
for (int i = 0; i < a.length; i++)
{
String enumname = A.getvalue (i). ToString ();
int enumkey = (int) System.Enum.Parse (t, enumname);
String enumdescription = GetDescription (t, EnumKey);
List. ADD (EnumKey, enumdescription);
}
return list;
}
return null;
}

#region Tool Functions

private static string GetName (System.Type T, Object v)
{
Try
{
Return Enum.getname (t, v);
}
Catch
{
return "UNKNOWN";
}
}

<summary>
Returns a description of the specified value for the specified enumeration type
</summary>
<param name= "T" > Enum type </param>
<param name= "V" > Enumeration value </param>
<returns></returns>
private static string GetDescription (System.Type T, Object v)
{
Try
{
FieldInfo fi = T.getfield (GetName (t, v));
descriptionattribute[] attributes = (descriptionattribute[]) fi. GetCustomAttributes (typeof (DescriptionAttribute), false);
return (attributes. Length > 0)? Attributes[0]. Description:getname (t, v);
}
Catch
{
return "UNKNOWN";
}
}
#endregion
}

Gets the expansion method of the enum.

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.