确定类型是 *任一 * 的 int 还是可为<int>空



反射代码。

我可以检查一下myTypeObject==typeof(decimal(|| myTypeObject=typeof(decimal?(

有没有办法做到这一点而不重复decimal

我猜大概是这样的:

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

如果输入类型不可为null,则可以使用返回null的Nullable.GetUnderlyingType

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

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

bool isDecimal = boxedDecimal is decimal?;

最新更新