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