Given a type parameter which is a Nullable<>
, how can I create an instance of that type which has HasValue = false
?
In other words, complete this code:
//type is guaranteed to implement Nullable<>
public static object Create(Type type)
{
//Instantiate a Nullable<T> with reflection whose HasValue = false, and return it
}
My attempt, which doesn't work (it throws a NullReferenceException
) as there's no default constructor:
static void Main(string[] args)
{
Console.WriteLine(Create(typeof(Nullable<int>)));
Console.ReadLine();
}
//type is guaranteed to be implement from Nullable<>
public static object Create(Type type)
{
//Instantatie a Nullable<T> with reflection whose HasValue = false, and return it
return type.GetConstructor(new Type[0]).Invoke(new object[0]);
}
If you want a method with a signature of
object
, you just returnnull
:That will always be the boxed representation of any nullable type value where
HasValue
is null. In other words, the method is pointless... you might as well just use thenull
literal:Of course, if
type
isn't guaranteed to be nullable value type, you might want to conditionalize the code... but it's important to understand that there's no such thing as an object representation of aNullable<T>
value. Even for non-null values, the boxed representation is the boxed representation of the non-nullable type:Highly dangerous (and not recommended for non-testing purposes) is to use SharpUtils' method
UnsafeTools.Box<T>(T? nullable)
. It bypasses the normal boxing of nullable types, which boxes their value or returns null, and instead creates an actual instance ofNullable<T>
. Note that working with such an instance can be extremely buggy.