Creación con nulos mediante reflexión con HasValue = false, dado un parámetro de tipo `Tipo` solamente
Dado un parámetro de tipo que es unNullable<>
, ¿cómo puedo crear una instancia de ese tipo que tieneHasValue = false
?
En otras palabras, complete este código:
//type is guaranteed to implement Nullable<>
public static object Create(Type type)
{
//Instantiate a Nullable<T> with reflection whose HasValue = false, and return it
}
Mi intento, que no funciona (arroja unNullReferenceException
) ya que no hay un constructor predeterminado:
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]);
}