确定类型是 *int 还是 Nullable<int>

Identify if a Type is *either* of int or Nullable<int>

反射码。

我可以检查是否 myTypeObject == typeof(十进制) || myTypeObject == typeof(十进制?)

有没有不重复的方法decimal

我猜是这样的:

myRealTypeObject = myTypeObject.IsNullable() ? myTypeObject.GetTypeInsideNullability() : myTypeObject;
myRealTypeObject == typeof(decimal)

如果输入类型不可为空,您可以使用 Nullable.GetUnderlyingType which returns null:

var myRealTypeObject = Nullable.GetUnderlyingType(myTypeObject) ?? myTypeObject;

如果您有一些要检查的对象,您可以使用 is(或 as):

bool isDecimal = boxedDecimal is decimal?;