只想知道是否有人知道如何解决此错误?我也用过TypeCode.
,但还是没有运气。谢谢
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<int>)
不是TypeCode
,并且它的字符串表示形式不是TypeCode
。没有TypeCode
专门表示Nullable<int>
。
您可以使用Nullable.GetUnderlyingType(type)
来检查是否是Nullable<T>
并同时获取T
(如果不是,则返回null
),并且可以在其结果上使用Type.GetTypeCode
,但是:我怀疑您的情况下,完全使用TypeCode
可能是一个错误,仅检查类型本身(if (type == typeof(int?)) {...}
)可能会更好。
关于c# - 无法将 'string'隐式转换为 'System.TypeCode',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44671596/