Enum With String Values In C#
First I created the new custom attribute class, the source is below:
/// <summary>
/// This attribute is used to represent a string value
/// for a value in an enum.
/// </summary>
public class StringValueAttribute : Attribute {
#region Properties
/// <summary>
/// Holds the stringvalue for a value in an enum.
/// </summary>
public string StringValue { get; protected set; }
#endregion
#region Constructor
/// <summary>
/// Constructor used to init a StringValue Attribute
/// </summary>
/// <param name="value"></param>
public StringValueAttribute(string value) {
this.StringValue = value;
}
#endregion
}
Then I created a new Extension Method which I will use to get the string value for an enums value:
/// <summary>
/// Will get the string value for a given enums value, this will
/// only work if you assign the StringValue attribute to
/// the items in your enum.
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static string GetStringValue(this Enum value) {
// Get the type
Type type = value.GetType();
// Get fieldinfo for this type
FieldInfo fieldInfo = type.GetField(value.ToString());
// Get the stringvalue attributes
StringValueAttribute[] attribs = fieldInfo.GetCustomAttributes(
typeof(StringValueAttribute), false) as StringValueAttribute[];
// Return the first if there was a match.
return attribs.Length > 0 ? attribs[0].StringValue : null;
}
So now create your enum and add the StringValue attributes to your values:
public enum Test : int {
[StringValue("a")]
Foo = 1,
[StringValue("b")]
Something = 2
}
Now you are ready to go, to get the string value for a value in the enum you can do so like this now:
Test t = Test.Foo;
string val = t.GetStringValue();
- or even -
string val = Test.Foo.GetStringValue();
---------------
public class Cls_EnumHandle
{
public string GetDescription(Enum value)
{
Type type = value.GetType();
MemberInfo[] memInfo = type.GetMember(value.ToString());
if (memInfo != null && memInfo.Length > 0)
{
object[] attrs = memInfo[0].GetCustomAttributes(typeof(DescriptionAttribute), false);
if (attrs != null && attrs.Length > 0)
{
return ((DescriptionAttribute)attrs[0]).Description;
}
}
return value.ToString();
}
public IList ToList(Type type)
{
if (type == null) throw new ArgumentNullException("type");
if (!type.IsEnum) throw new ArgumentException("Type provided must be an Enum.", "type");
ArrayList list = new ArrayList();
Array array = Enum.GetValues(type);
foreach (Enum value in array)
{
list.Add(new KeyValuePair<Enum, string>(value, GetDescription(value)));
}
return list;
}
}
[AttributeUsage(AttributeTargets.Enum | AttributeTargets.Field, AllowMultiple = false)]
public class EnumDescriptionAttribute : Attribute
{
private string description;
public string Description
{
get
{
return this.description;
}
}
public EnumDescriptionAttribute(string description)
: base()
{
this.description = description;
}
}
DropDownList1.DataSource = new Cls_EnumHandle().ToList(typeof(SqlOperator));
DropDownList1.DataTextField = "Value";
DropDownList1.DataValueField = "Key";
DropDownList1.DataBind();
http://weblogs.asp.net/stefansedich/archive/2008/03/12/enum-with-string-values-in-c.aspx

浙公网安备 33010602011771号