Cannot implicitly convert 'string' to 'System.TypeCode'

人走茶凉 提交于 2019-12-20 05:38:26

问题


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;
}

This is the switch

    public static object HandleDBNull(object value, System.Type _type)
{
    switch (Type.GetTypeCode(_type))
    {

Thanks again for any help


回答1:


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.

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.




回答2:


The type you use in the switch statement has to match the type in the case statements, where currently you're comparing a TypeCode to a string.

The TypeCode enumeration only contains values for the primitive types, and all others will be TypeCode.Object. So you can't get a specific TypeCode for the Nullable<int> type.



来源:https://stackoverflow.com/questions/44671596/cannot-implicitly-convert-string-to-system-typecode

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!