Non-generic Enum.TryParse

It turns out that there isn’t a non-generic TryParse overload in .net 4. This seems like a pretty egregious oversight so I created my own.

public static class EnumHelper
    static MethodInfo enumTryParse;

    static EnumHelper()
        enumTryParse = typeof(Enum).GetMethods(BindingFlags.Public | BindingFlags.Static)
            .Where(m => m.Name == "TryParse" && m.GetParameters().Length == 3)

    public static bool TryParse(
        Type enumType, 
        string value, 
        bool ignoreCase, 
        out object enumValue)
        MethodInfo genericEnumTryParse = enumTryParse.MakeGenericMethod(enumType);

        object[] args = new object[] { value, ignoreCase, Enum.ToObject(enumType, 0) };
        bool success = (bool)genericEnumTryParse.Invoke(null, args);
        enumValue = args[2];

        return success;


Author: justinmchase

I'm a Software Developer from Minnesota.

Drop a brain bomb

Please log in using one of these methods to post your comment: Logo

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

Google photo

You are commenting using your Google 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 )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.