Is it possible to add an extension method to a generic class that is independent of that class’ instance generic type?
I want to write a extension method for handling Nullable values. So far, the method looks like this:
public static object GetVerifiedValue(this Nullable<> obj)
{
return obj.HasValue ? obj.Value : DBNull.Value;
}
But the compiler doesn’t accept the Nullable<> type.
Is there any way I can do this?
Note: I’m using C# 3.0, .NET 3.5.
Just make it a generic method:
The calling code can use type inference when calling it:
EDIT: Another option is to use the boxing behaviour of nullable types:
Of course that doesn’t verify that you’re trying to pass in an expression of a nullable value type…