Nullable create via reflection mit HasValue = false, wenn ein Parameter vom Typ `Type` only @ angegeben wi

Gab einen Typparameter, der ein @ iNullable<>, wie kann ich eine Instanz dieses Typs erstellen, die @ hHasValue = false?

Mit anderen Worten, vervollständigen Sie diesen 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
}

Mein Versuch, der nicht funktioniert (es wirft einNullReferenceException) da es keinen Standardkonstruktor gibt:

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]);
}

Antworten auf die Frage(4)

Ihre Antwort auf die Frage