I'm trying to convert an XElement
to either null
or whatever type T
is supplied.
Here's what I have so far:
public static T? ConvertToNullable<T>(this XElement element) where T : IConvertible
{
if (element.IsNill())
return null;
else
return (T)element;
}
Error:
The type
T
must be a non-nullable value type in order to use it as parameterT
in the generic type or methodSystem.Nullable<T>
Can this be done or do I have to write a separate extension for bool
, byte
, int
, long
, double
, decimal
, etc.?
Edit
I shouldn't have been using generics for this at all. See my answer below.