Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Using System. Reflection;
Using System. IO;
Namespace ConsoleApplication1
{
[System. AttributeUsage (AttributeTargets. All, AllowMultiple = false)]
Public class EnumSerializedAttribute: System. Attribute
{
Private string _ serialized;
Public EnumSerializedAttribute (string s)
{
This. _ serialized = s;
}
Public string GetValue ()
{
Return _ serialized;
}
}
Public static class MyHelper
{
Public static string AsString (this Enum o)
{
Type t = o. GetType ();
String filedName = Enum. GetName (t, o );
System. Reflection. FieldInfo f = t. GetField (filedName );
Object [] atts = f. GetCustomAttributes (true );
For (int I = 0; I <atts. Length; I ++)
{
Object att = atts [I];
If (att is EnumSerializedAttribute)
{
EnumSerializedAttribute r = (enumserializedattriatt) att;
Return r. GetValue ();
}
}
Return filedName;
}
}
Class Program
{
Enum Operation
{
[EnumSerialized ("Enable Serialized")]
Enable = 1,
[EnumSerialized ("Disable Serialized")]
Disable = 0,
Other = 2
}
Static void Main (string [] args)
{
Console. WriteLine (Operation. Disable. AsString ());
Console. WriteLine (Operation. Enable. AsString ());
Console. WriteLine (Operation. Other. AsString ());
Console. Read ();
}
}
}
From Tim's Blog