Detecting a Nullable Type via reflection

前端 未结 5 544
天命终不由人
天命终不由人 2021-01-01 12:14

Surprisingly the following code fails the Assert:

int? wtf = 0;
Assert.IsType>(wtf);

So just out curiosity, how c

5条回答
  •  忘掉有多难
    2021-01-01 12:46

    What namespace is Assert in?

    The following returns true as you would expect:

    int? wtf = 0;
    if (typeof(Nullable).IsInstanceOfType(wtf))
    {
        // Do things
    }
    

    Although its worth noting that typeof(Nullable).IsInstanceOfType(42) also returns true - this is because this method accepts an object and so gets boxed as a Nullable.

提交回复
热议问题