Getting a string value for an enum value is a common problem and there's a few solutions knocking around the Internet, however a few of these that I've seen end up having a utility class that handles mapping the enum value to the relevant string value, this is fine but I think this is an operation that should be callable on the enum value without having to go off and use another class. The solution I have here provides this functionality using extension methods that are available in .Net 3.5.
The first thing I did was to create a new attribute that will be applied to the enum values when they are declared so a string value can be specified for each enum value such as is shown in the code snippet below:
public enum { [StringValue("Red")] Red = 0, [StringValue("Blue")] Blue = 1, [StringValue("Green")] Green = 2 }
The code for the attribute class is:
public class StringValueAttribute : Attribute { public string StringValue { get; private set; } public StringValueAttribute(string value) { this.StringValue = value; } }
When writing the extension method we can take advantage of the fact that all enums have Enum as their base class. This allows the following extension to be defined.
public static stringGetStringValue(this Enum enumValue) { . . . }
This method is now available to be called as follows:
Colors myColor = Colors.Red; Console.WriteLine(myColor.GetStringValue());
As the 'GetStringValue' extension method is available on all enums it leaves the possibility that GetStringValue could be called on enums that have not used the 'StringValue' attribute. To protect against this there are two options, option one would be to return an empty string and option two is to use reflection and return the name of the enum value. This was the option I choose, firstly I look for the 'StringValue' attribute if that is present the method returns that as this is the mechanism that allows the developer to take finer control of the string value. So the previous enum could be defined as follows:
public enum Colors { [StringValue("#FF0000")] Red = 0, [StringValue("#0000FF")] Blue = 1, [StringValue("#00FF00")] Green = 2 }
Likewise the enum could be defined as shown below and we would get the values 'Red', 'Green' and 'Blue' when using the 'GetStringValue' method.
public enum Colors { Red = 0, Blue = 1, Green = 2 }
Here is my implementation of the extension method:
public static string GetStringValue(this Enum enumValue) { Typetype = enumValue.GetType(); FieldInfo fieldInfo = type.GetField(enumValue.ToString()); StringValueAttribute[] attribs = fieldInfo.GetCustomAttributes( typeof(StringValueAttribute), false) as StringValueAttribute[]; if(attribs.Length > 0) { return attribs[0].StringValue; } else { return fieldInfo.Name; } }
I've a prebuilt DLL so all you need to do is download this and reference it in your projects making sure to include the namespace 'Gangleri.Enums' when you wish to use the extension method or the StringValue attribute.
DLL: http://enumstringvalue.googlecode.com/files/Gangleri.Enums.dll.zip Source Code: http://code.google.com/p/enumstringvalue/source/checkout
.Net (4) ASP.Net (6) ASP.Net MVC (3) Best Practice (2) Book (4) Burn CD/DVD (1) C# (5) Career (2) Class Library (1) Community event (14) Design (1) Design Patterns (1) Ethernet (1) Home Network (1) i18n (1) Internet Explorer (1) iPod (1) JavaScript (2) jQuery (1) Library (1) Microsoft Translator (1) Ms Office (1) MVC Unit test (1) NWMTUG (6) Open Source (2) PowerShell (13) Python (1) SharePoint (1) SQL Server (1) Team System (1) Test Driven Development (7) Utility (14) Visual C++ (1) Visual Studio 2008 (11) Windows 64 bit (1) Windows 7 (10) Windows XP (3)