Here's a bit of a tricky one. Perhaps someone's C#-fu is superior to mine, as I couldn't find a solution.
I have a method that takes a parameter that holds either an enum or a string indicating the value of an Enum and returns an instance of that enum. It's basically an implementation of Enum.Parse
but implemented as a generic method. Why the .NET Framework doesn't have this built in is beyond me.
public static T Parse<T>(object value) where T : struct
{
if (!typeof (T).IsEnum)
throw new ArgumentException("T must be an Enum type.");
if (value == null || value == DBNull.Value)
{
throw new ArgumentException("Cannot parse enum, value is null.");
}
if (value is String)
{
return (T)Enum.Parse(typeof(T), value.ToString());
}
return (T)Enum.ToObject(typeof(T), value);
}
Now, I can do something like:
MyEnum foo = Parse<MyEnum>(obj);
And get an instance of MyEnum
. If obj
is null, I throw an exception.
However, sometimes obj
is null
and I want to allow that. In this case, I'd like to be able to do:
MyEnum? foo = Parse<MyEnum?>(obj);
However, for the life of me, I can't figure out a way to get that working. First off, even though Nullable<MyEnum>
is a struct
, it's unable to be used as a type parameter to Parse<T>
. I think this has something to do with all the magic the compiler does with Nullable<>
, so I won't question it.
It doesn't appear you can overload the method and only differentiate it based on constraints on T
. For example, if I do:
public static T Parse<T>(object value) where T : new()
{
// This should be called if I pass in a Nullable, in theory
}
I'll get the error: Member with the same signature is already declared
So, that leaves me with only one option left: Implement a entirely separate method designed for nullable types:
public static T? ParseNullable<T>(object value) where T : struct
{
if (!typeof (T).IsEnum)
throw new ArgumentException("T must be an Enum type.");
if (value == null || value == DBNull.Value)
return null;
if (value is String)
return Enum.Parse(typeof (T), value.ToString()) as T?;
return Enum.ToObject(typeof (T), value) as T?;
}
I can now call this with:
MyEnum? foo = ParseNullable<T>(obj);
My Question: Is there a way to combine these two methods into a single method that will do the right thing depending on the type parameter, or create overloads where one overload will be used in the case where the type parameter is Nullable<> and the other overload called when it's not?