Enum With String Values In C# I am curre…

Few days back, I  came to a point where I had to replicate the enums in my classes, but the issue with them is that the enums do not use integer values but string ones. In C# you cannot have an enum that has string values. So the solution I came up with (I am sure it has been done before) was just to make a new custom attribute called StringValueAttribute and use this to give values in my enum a string based value.

First I created the new custom attribute class, the source is below:

///
/// This attribute is used to represent a string value
/// for a value in an enum.
///
public class StringValueAttribute : Attribute {

#region Properties

///
/// Holds the stringvalue for a value in an enum.
///
public string StringValue { get; protected set; }

#endregion

#region Constructor

///
/// Constructor used to init a StringValue Attribute
///
///
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:

///
/// 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.
///
///
///
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();

All this is very easy to implement and I like the ease of use you get from the extension method [I really like them :)] and it gave me the power I needed to do what I needed. I haven’t tested the performance hit that the reflection may give and might do so sometime soon.

Thanks

Advertisements

About Saad Khan

Saad is an ASP.NET developer with 4 years of experience, and has also made a number of contributions to the Sitefinity marketplace and community. He loves to explore new code, and create innovative tools and procedures while exploring new approaches in his work with Sitefinity and MVC. As an experienced .NET developer, he frequents both Telerik and Microsoft technologies. In his spare time, Saad is an avid gamer and movie fanatic, and also experiments with new and interesting tools on the Microsoft platform.​​

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: