Enum Parser - Enum with string as values.

Enum  parser - I already wrote a article about this. The enum I used that article was standard enum i.e.
public enum myEnum{ itemOne =0, itemTwo=1 } etc, but sometimes we need to do something like public enum myEnum{ itemOne ="Zero", itemTwo="One" },unfortunately .Net doesn't support that functionality directly. We need to do some thing different to do the job. One approach I used in this example is to use Attribute for every items in the enum. In the  processing time of this enum we will use reflection, using reflection we loop through attribute values and try to match with given value. Also I implement the related parser for the enum. The code I wrote is below, following code will show the enum and its structure.

public enum EnumWithText{ [TextValue("Clr")] ItemOne = 0,[TextValue("Dlr")]  ItemTwo = 1}
The above enum member has an attribute named TextValueAttribute, the implementation of the above class is as below,
public class TextValueAttribute : Attribute{
    public string TextValue { getprotected set; }
    public TextValueAttribute(string value)
     {TextValue = value;}}
Thats all we need to do. Now we need few methods to get the TextValue from the enum, following Parser class will help us to do the job,
public static class Parser
     public static string GetEnumValue(this Enum value)
      Type type = value.GetType();
      FieldInfo fieldInfo = type.GetField(value.ToString());
      TextValueAttribute[] attribs = fieldInfo.GetCustomAttributes(typeof(TextValueAttribute), falseas TextValueAttribute[];
         return attribs.Length > default(int) ? attribs[default(int)].TextValue : null;
     public static T1 ParseEnum<T1, T2>(T2 item)
        return string.IsNullOrEmpty(item.ToString()) ?
        default(T1) :(T1)(Parser.ParseExtension<T1>(typeof(T1), item.ToString(), !default(bool)));

     public static object ParseExtension<T1>(Type type, string stringValue, bool ignoreCase)
        object result = null;string enumStringValue = null;
        if (!type.IsEnum) throw new ArgumentException("Enum type is required.");
                field =>
                    TextValueAttribute[] attrs = field.GetCustomAttributes(typeof(TextValueAttribute), default(bool)) as TextValueAttribute[];
                    if (attrs.Length > default(int))
                        enumStringValue = attrs[default(int)].TextValue;
                    if (string.Compare(enumStringValue, stringValue, ignoreCase) == default(int))
                        result = Enum.Parse(type, field.Name);
             return result == null ? default(T1) : result;
To test above code we could use following test harness code, 
static void Main(string[] args)
  string result = EnumWithText.ItemOne.GetEnumValue();
  EnumWithText myEnum1 = Parser.ParseEnum<EnumWithTextstring>("Clr");
  EnumWithText myEnum2 = Parser.ParseEnum<EnumWithTextstring>("Dlr");
  EnumWithText myEnum3 = Parser.ParseEnum<EnumWithTextstring>("None");

thanks mohammad.

 Few C# and Application Design books from Amazon,

No comments:

Post a Comment