Detecting a Nullable Type via reflection

前端 未结 5 541
天命终不由人
天命终不由人 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:45

    int? i = 0;
    var type = TypedReference.GetTargetType(__makeref(i));
    var isNullable = type.IsGenericType &&
        type.GetGenericTypeDefinition() == typeof(Nullable<>);
    

提交回复
热议问题