|
using System;
using System.Reflection;
namespace Tests
{
public class EnumStrValAttr: Attribute
{
public string _Value = "";
public string Value
{
get
{
return _Value;
}
set
{
_Value = value;
}
}
public EnumStrValAttr(string Value)
{
_Value = Value;
}
public static string GetStrValue(Enum EnumConst)
{
FieldInfo fi = EnumConst.GetType().GetField(EnumConst.ToString());
EnumStrValAttr[] attr = (EnumStrValAttr[])fi.GetCustomAttributes(typeof(EnumStrValAttr), false);
if (attr.Length > 0)
{
return attr[0].Value;
}
else
{
return String.Empty;
}
}
public static object GetValue(Type type, string strVal)
{
if (!type.IsEnum)
{
throw new Exception("Type is not an enumeration!");
}
FieldInfo[] fields = type.GetFields();
foreach (FieldInfo fi in fields)
{
EnumStrValAttr[] attr = (EnumStrValAttr[])fi.GetCustomAttributes(typeof(EnumStrValAttr), false);
if (attr.Length > 0 && attr[0].Value == strVal )
{
return Enum.Parse(type,fi.Name);
}
}
return null;
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.