Archive

Archive for the ‘C#’ Category

Generic Flags Enum Extension Method – Parse a Delimited String

September 30, 2010 Leave a comment

I am a big fan of extension methods.  It’s a great way to simply code and to allow developers to write in a more fluent way. 

I have a .Net application that exposes services to Flex.  Because Flex doesn’t have a native flags Enum type that we can pass across the wire, I have a property that converts a delimited string into an Enum of T.  The code to do this the non-generic way was too complex and prone to human error.

To solve the problem of converting a generic flags Enum, I created a generic extension method.  There is already a method to generically parse a string as an Enum, but not a delimited string.

One challenge with extending an Enum is that you can’t have an Enum value type on the constraint clause.  You can however have a struct in the constraint clause, which an Enum inherits from.  This should prevent misuse 99% of the time, after all who will try to parse a non-Enum struct type as a flags Enum?  Don’t answer that.

The extension method created allows us to write code like this:

 String delimitedEnum = "AccountLevel, OpportunityLevel, PackageLevel, None";
 ApplicationLevels applicationLevels = delimitedEnum.EnumParseDelimitedString<ApplicationLevels>();

 

That is much easier than the alternative.  If you want to try this at home, feel free to play with the code snippet below.  If you find any bugs or have improvements, please post them in the comment below.  I can’t claim credit for all of this.  There was already a blog detailing how to parse an Enum from a string with a single value, I simply went the next step with a flags Enum.  If you are using Byte values in your enums you’ll need to code for that, but this should get you most of the way there.  Happy coding!

 

Try this at home kids. . .

        public static T EnumParseDelimitedString<T>(this string value, char delimiter, bool ignoreCase)
            where T : struct
        {
            if (value.Length == 0)
            {
                throw new ArgumentException("Must specify valid information for parsing in the string.", "value");
            }
            if (!typeof(T).IsEnum)
            {
                throw new ArgumentException("Type provided must be an Enum.", "T");
            }

            //Split the string
            value = value.Trim();
            IList<string> values = value.Split(delimiter).ToList<string>();

            //Get the type of enum we are dealing with
            Type numberType = Enum.GetUnderlyingType(typeof(T));

            if (numberType.Equals(typeof(int)))
            {
                int newResult = 0;
                foreach (string val in values)
                {
                    int tempResult;
                    tempResult = (int)(Object)EnumParse<T>(val, ignoreCase);
                    newResult += tempResult;
                }
                return (T)(Object)newResult;
            }
            else
            {
                throw new ArgumentException("Unknown enum underlying type " + numberType.Name + ".");
            }

        }


        public static T EnumParse<T>(this string value, bool ignoreCase)
             where T : struct
        {
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }
            value = value.Trim();
            if (value.Length == 0)
            {
                throw new ArgumentException("Must specify valid information for parsing in the string.", "value");
            }
            Type t = typeof(T);
            if (!t.IsEnum)
            {
                throw new ArgumentException("Type provided must be an Enum.", "T");
            }
            T enumType = (T)Enum.Parse(t, value, ignoreCase);
            return enumType;
        }