问题描述
只是想知道是否有人知道如何解决这个错误?我也使用过 TypeCode.
但仍然没有运气.谢谢
Just wondering if anyone knows how to fix this error? I've also used TypeCode.
but still no luck. Thanks
case typeof(Nullable<int>).ToString(): //<----- error is here
if ((!object.ReferenceEquals(value, DBNull.Value)))
{
return value;
}
else
{
return null;
}
这是开关
public static object HandleDBNull(object value, System.Type _type)
{
switch (Type.GetTypeCode(_type))
{
再次感谢您的帮助
推荐答案
最终,typeof(Nullable)
不是 TypeCode
,而是字符串表示其中不是 TypeCode
.没有 TypeCode
代表 Nullable
具体.
Ultimately, typeof(Nullable<int>)
isn't a TypeCode
, and the string representation of that isn't a TypeCode
. There is no TypeCode
that represents Nullable<int>
specifically.
您可以使用 Nullable.GetUnderlyingType(type)
来检查某些内容是否为 Nullable
并同时获取 T
(如果不是,它返回 null
),你可以在结果上使用 Type.GetTypeCode
,但是:我怀疑在你的情况下,使用 TypeCode
at all 可能是一个错误,简单地检查类型本身 (if (type == typeof(int?)) {...}
) 可能是更好.
You can use Nullable.GetUnderlyingType(type)
to check that something is Nullable<T>
and get the T
at the same time (it returns null
if not), and you can use Type.GetTypeCode
on the result of that, but: I suspect that in your case, using TypeCode
at all may be an error, and simply checking the type itself (if (type == typeof(int?)) {...}
) may be better.
这篇关于无法将“字符串"隐式转换为“System.TypeCode"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!