将“Nullable<T>”作为类型参数传递给 C# 函数
Passing a `Nullable<T>` as a Type parameter to a C# function
这是在 .NET Core 1.1.4 项目中,因此请考虑到这一点。
我正在尝试创建一个函数来验证是否可以将值分配给类型,但我遇到了 Nullable<T>
类型的问题。
我的函数:
protected void CheckIsAssignable(Object value, Type destinationType)
{
if (value == null)
{
// Nullable.GetUnderlyingType returns null for non-nullable types.
if (Nullable.GetUnderlyingType(destinationType) == null)
{
var message =
String.Format(
"Property Type mismatch. Tried to assign null to type {0}",
destinationType.FullName
);
throw new TargetException(message);
}
}
else
{
// If destinationType is nullable, we want to determine if
// the underlying type can store the value.
if (Nullable.GetUnderlyingType(destinationType) != null)
{
// Remove the Nullable<T> wrapper
destinationType = Nullable.GetUnderlyingType(destinationType);
}
// We can now verify assignability with a non-null value.
if (!destinationType.GetTypeInfo().IsAssignableFrom(value.GetType()))
{
var message =
String.Format(
"Tried to assign {0} of type {1} to type {2}",
value,
value.GetType().FullName,
destinationType.FullName
);
throw new TargetException(message);
}
}
}
上面的 if
子句处理 value
为 null
的情况,并尝试验证 destinationType
为 Nullable<T>
; else
子句处理 value
是否确实包含某些内容,因此它会尝试确定您是否可以将其分配给 destinationType
,或者如果它是 Nullable<T>
,则它可以分配给 T
.
问题是 Nullable<T>
不是 Type
,因此调用 CheckIfAssignable(3, Nullable<Int32>)
与函数签名不匹配。
将签名更改为:
protected void CheckIsAssignable(Object value, ValueType destinationType)
让我传递一个 Nullable<T>
,但我不能将它作为参数提交给 Nullable.GetUnderlyingType
。
我不确定我是否把这个问题复杂化了,但我觉得有一个我只是没有看到的简单解决方案。
您没有在那里传递类型本身。您需要像这样使用 typeof()
命令:
CheckIfAssignable(3, typeof(Nullable<Int32>))
这是在 .NET Core 1.1.4 项目中,因此请考虑到这一点。
我正在尝试创建一个函数来验证是否可以将值分配给类型,但我遇到了 Nullable<T>
类型的问题。
我的函数:
protected void CheckIsAssignable(Object value, Type destinationType)
{
if (value == null)
{
// Nullable.GetUnderlyingType returns null for non-nullable types.
if (Nullable.GetUnderlyingType(destinationType) == null)
{
var message =
String.Format(
"Property Type mismatch. Tried to assign null to type {0}",
destinationType.FullName
);
throw new TargetException(message);
}
}
else
{
// If destinationType is nullable, we want to determine if
// the underlying type can store the value.
if (Nullable.GetUnderlyingType(destinationType) != null)
{
// Remove the Nullable<T> wrapper
destinationType = Nullable.GetUnderlyingType(destinationType);
}
// We can now verify assignability with a non-null value.
if (!destinationType.GetTypeInfo().IsAssignableFrom(value.GetType()))
{
var message =
String.Format(
"Tried to assign {0} of type {1} to type {2}",
value,
value.GetType().FullName,
destinationType.FullName
);
throw new TargetException(message);
}
}
}
上面的 if
子句处理 value
为 null
的情况,并尝试验证 destinationType
为 Nullable<T>
; else
子句处理 value
是否确实包含某些内容,因此它会尝试确定您是否可以将其分配给 destinationType
,或者如果它是 Nullable<T>
,则它可以分配给 T
.
问题是 Nullable<T>
不是 Type
,因此调用 CheckIfAssignable(3, Nullable<Int32>)
与函数签名不匹配。
将签名更改为:
protected void CheckIsAssignable(Object value, ValueType destinationType)
让我传递一个 Nullable<T>
,但我不能将它作为参数提交给 Nullable.GetUnderlyingType
。
我不确定我是否把这个问题复杂化了,但我觉得有一个我只是没有看到的简单解决方案。
您没有在那里传递类型本身。您需要像这样使用 typeof()
命令:
CheckIfAssignable(3, typeof(Nullable<Int32>))