Thursday, April 24, 2014

StringValueAttribute implementation

Hi, after I posted the article on String Value for enum, I received comments asking for the detail implementations.  so I published the implementation in C# 4.5 as following. If you are using VB.Net or different version of C#, it is not difficulty to covert the code below to your targeted language or targeted version.

ok, happy enuming.


StringValueAttribute





public sealed class ValueAttribute : Attribute



{

public string Value { get; private set; }

public ValueAttribute(string value)



{

Value = value;

}

}







Extension methods





public static class EnumExtensions



{

public static string GetStringValue(this Enum value)



{

if (value == null) throw new ArgumentNullException("value", "paramater cannot be null");

// Get the type

var type = value.GetType();

// Get fieldinfo for this type

var fieldInfo = type.GetField(value.ToString());

// Get the stringvalue attributes

var attribs = fieldInfo.GetCustomAttributes(

typeof(ValueAttribute), false) as ValueAttribute[];

// Return the first if there was a match.

if (attribs == null) return value.ToString();

return attribs.Length > 0 ? attribs[0].Value : value.ToString();



}

public static T GetEnumValue<T>(this string value,bool ignoreCase)



{

return (T)(GetEnumStringValue(value, typeof(T), ignoreCase));



}

public static T GetEnumValue<T>(this string value)



{

return (T)(GetEnumStringValue(value, typeof(T), true));



}

private static object GetEnumStringValue(string value, Type enumType, bool ignoreCase)



{

if (!enumType.IsEnum) throw new ArgumentException("enumType should be a valid enum", "enumType");

foreach (var fieldInfo in from fieldInfo in enumType.GetFields() let attributes = fieldInfo.GetCustomAttributes(typeof (ValueAttribute), false) where attributes.Length > 0 let attribute = (ValueAttribute) attributes[0] where String.Compare(attribute.Value, value, ignoreCase) == 0 select fieldInfo)



{

return Enum.Parse(enumType, fieldInfo.Name);



}

return Enum.Parse(enumType, value);



}

}








Test Cases



public enum VehicleCategory




{

[Value ("SEDAN")] Sedan,

[Value("WAGON")] Wagon,

[Value("LUXRY")] Luxry,

[Value("CROSS OVER")] Crossover,

[Value("CROSS OVER")] Coupe,

[Value ("SUV")]Suv,

[Value("VAN MINIVAN")] VanMinivan,



Hibrid
}
 
[TestClass]

public class StringValueAttributeTest



{
 
[TestMethod]

public void StringValueTest()



{
 



Assert.AreEqual("CROSS OVER", VehicleCategory.Crossover.GetStringValue());

Assert.AreEqual("SUV", VehicleCategory.Suv.GetStringValue());

Assert.AreEqual("VAN MINIVAN", VehicleCategory.VanMinivan.GetStringValue());

Assert.AreEqual("Hibrid", VehicleCategory.Hibrid.GetStringValue());

string myStringValue = "VAN MINIVAN";

Assert.AreEqual(VehicleCategory.VanMinivan, myStringValue.GetEnumValue<VehicleCategory>());

myStringValue = "Hibrid";

Assert.AreEqual(VehicleCategory.Hibrid, myStringValue.GetEnumValue<VehicleCategory>());
}
 
 
 




 


No comments:

Post a Comment