Nullable create via reflection with HasValue = false, given a parameter of type `Type` only

后端 未结 2 698
别跟我提以往
别跟我提以往 2021-01-25 04:02

Given a type parameter which is a Nullable<>, how can I create an instance of that type which has HasValue = false?

In other words, compl

相关标签:
2条回答
  • 2021-01-25 04:52

    Given a type parameter which is a Nullable<>, how can I create an instance of that type which has HasValue = false?

    If you want a method with a signature of object, you just return null:

    //type is guaranteed to be implement from Nullable<>
    public static object Create(Type type)
    {
        return null;
    }
    

    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 the null literal:

    var i = (int?) null;
    

    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 a Nullable<T> value. Even for non-null values, the boxed representation is the boxed representation of the non-nullable type:

    int? x = 5;
    object y = x; // Boxing
    Console.WriteLine(y.GetType()); // System.Int32; nullability has vanished
    
    0 讨论(0)
  • 2021-01-25 05:01

    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 of Nullable<T>. Note that working with such an instance can be extremely buggy.

    public static object Create<T>() where T : struct //T must be a normal struct, not nullable
    {
        return UnsafeTools.Box(default(T?));
    }
    
    0 讨论(0)
提交回复
热议问题