Common tool classes 6-enumeration classes

Source: Internet
Author: User

public static Class Enumhelper
{
<summary>
Enumeration to Dictionary
</summary>
<typeparam name= "T" ></typeparam>
<returns></returns>
public static dictionary<string, int> enumdictionary<t> ()
{
dictionary<string, int> dic = new dictionary<string, int> ();
if (typeof (T) = = typeof (Enum))
{
throw new ArgumentOutOfRangeException ("T can only be an enum type");
}

Type enumtype = typeof (T);
foreach (String key in Enum.getnames (Enumtype))
{
int val = (int) Enumtype.getfield (key). GetValue (NULL);
Dic. Add (Key, Val);
}
return dic;
}

<summary>
Enumeration to Dictionary
</summary>
<typeparam name= "T" ></typeparam>
<returns></returns>
public static Dictionary<int, string> enumtodictionary<t> ()
{
Dictionary<int, string> dic = new Dictionary<int, string> ();
if (typeof (T) = = typeof (Enum))
{
throw new ArgumentOutOfRangeException ("T can only be an enum type");
}

Type enumtype = typeof (T);
foreach (String key in Enum.getnames (Enumtype))
{
int val = (int) Enumtype.getfield (key). GetValue (NULL);
Dic. Add (Val, key);
}
return dic;
}

<summary>
Enumeration description and Key conversion dictionary
</summary>
<typeparam name= "T" ></typeparam>
<returns></returns>
public static dictionary<string, string> enumkeyanddescriptiontodictionary<t> ()
{
dictionary<string, string> dic = new dictionary<string, string> ();

if (typeof (T) = = typeof (Enum))
{
throw new ArgumentOutOfRangeException ("T can only be an enum type");
}

Type enumtype = typeof (T);

foreach (String key in Enum.getnames (Enumtype))
{
FieldInfo finfo = Enumtype.getfield (key);
object[] cattr = Finfo. GetCustomAttributes (typeof (DescriptionAttribute), true);
if (Cattr.length > 0)
{
DescriptionAttribute desc = cattr[0] as DescriptionAttribute;
if (desc! = null)
{
Dic[key] = desc. Description;
}
}
}

return dic;
}

<summary>
Enumeration description and Key conversion dictionary
</summary>
<typeparam name= "T" ></typeparam>
<returns></returns>
public static dictionary<string, string> enumdescriptionandkeytodictionary<t> ()
{
dictionary<string, string> dic = new dictionary<string, string> ();

if (typeof (T) = = typeof (Enum))
{
throw new ArgumentOutOfRangeException ("T can only be an enum type");
}

Type enumtype = typeof (T);

foreach (String key in Enum.getnames (Enumtype))
{
FieldInfo finfo = Enumtype.getfield (key);
object[] cattr = Finfo. GetCustomAttributes (typeof (DescriptionAttribute), true);
if (Cattr.length > 0)
{
DescriptionAttribute desc = cattr[0] as DescriptionAttribute;
if (desc! = null)
{
Dic[desc. Description] = key;
}
}
}

return dic;
}

<summary>
Enumeration description and value converted to dictionary
</summary>
<typeparam name= "T" ></typeparam>
<returns></returns>
public static Dictionary<int, string> enumvalueanddescriptiontodictionary<t> ()
{
Dictionary<int, string> dic = new Dictionary<int, string> ();

if (typeof (T) = = typeof (Enum))
{
throw new ArgumentOutOfRangeException ("T can only be an enum type");
}

Type enumtype = typeof (T);

foreach (String key in Enum.getnames (Enumtype))
{
int val = (int) Enumtype.getfield (key). GetValue (NULL);

FieldInfo finfo = Enumtype.getfield (key);
object[] cattr = Finfo. GetCustomAttributes (typeof (DescriptionAttribute), true);
if (Cattr.length > 0)
{
DescriptionAttribute desc = cattr[0] as DescriptionAttribute;
if (desc! = null)
{
Dic[val] = desc. Description;
}
}
}

return dic;
}

<summary>
Enumeration description and value converted to dictionary
</summary>
<typeparam name= "T" ></typeparam>
<returns></returns>
public static dictionary<string, int> enumdescriptionandvaluetodictionary<t> ()
{
dictionary<string, int> dic = new dictionary<string, int> ();

if (typeof (T) = = typeof (Enum))
{
throw new ArgumentOutOfRangeException ("T can only be an enum type");
}

Type enumtype = typeof (T);

foreach (String key in Enum.getnames (Enumtype))
{
int val = (int) Enumtype.getfield (key). GetValue (NULL);

FieldInfo finfo = Enumtype.getfield (key);
object[] cattr = Finfo. GetCustomAttributes (typeof (DescriptionAttribute), true);
if (Cattr.length > 0)
{
DescriptionAttribute desc = cattr[0] as DescriptionAttribute;
if (desc! = null)
{
Dic[desc. Description] = val;
}
}
}

return dic;
}

<summary>
Enumeration description
</summary>
<param name= "E" ></param>
<returns></returns>
public static String Getenumdesc (Enum e)
{
FieldInfo FieldInfo = E.gettype (). GetField (E.tostring ());
if (fieldInfo! = null)
{
Descriptionattribute[] Enumattributes = (descriptionattribute[]) fieldinfo.getcustomattributes (typeof ( DescriptionAttribute), false);
if (Enumattributes.length > 0)
{
Return enumattributes[0]. Description;
}
}
return e.tostring ();
}
}

public class Enumview<t>
{
private static Dictionary<int, string> s_dictcodetotext = null;
private static dictionary<string, int> s_dicttexttocode = null;

Static Enumview ()
{
S_dictcodetotext = enumhelper.enumtodictionary<t> ();
S_dicttexttocode = enumhelper.enumdictionary<t> ();
}

Private Enumview () {}

public static Dictionary<int, string> Dictcodetotext {get {return s_dictcodetotext;}}
public static dictionary<string, int> Dicttexttocode {get {return s_dicttexttocode;}}


public static int GetCode (string text)
{
if (S_dicttexttocode.containskey (text))
{
return S_dicttexttocode[text];
}
return int. MinValue;
}

public static string GetText (int code)
{
if (S_dictcodetotext.containskey (code))
{
Return S_dictcodetotext[code]?? String. Empty;
}
return string. Empty;
}
}

public class Enumhelperex
{

public static T toenum<t> (int value, T defaultt) where t:struct
{
var enumname = Enum.getname (typeof (T), value);

Return toenum<t> (Enumname, DEFAULTT);
}

public static T Toenum<t> (string enumname, T defaultt) where t:struct
{
if (string. Isnullorwhitespace (Enumname))
{
return DEFAULTT;
}

T result;

if (! Enum.tryparse<t> (Enumname.trim (), out result))
{
return DEFAULTT;
}

if (enum.isdefined (typeof (T), result))
{
return result;
}

return DEFAULTT;
}

}

Common Tool Classes 6-enumeration classes

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.