从'System.Int32'到'System.Nullable`1 [[System.Int32,mscorlib]]的无效演员表

时间:2013-08-02 11:09:48

标签: c# .net casting type-conversion nullable

Type t = typeof(int?); //will get this dynamically
object val = 5; //will get this dynamically
object nVal = Convert.ChangeType(val, t);//getting exception here

我在上面的代码中得到了InvalidCastException。对于上面我可以简单地写int? nVal = val,但上面的代码是动态执行的。

我得到一个包含在对象(这里是val)中的值(非可空类型,如int,float等),我必须通过将它转换为另一个类型(可以或不可以)将其保存到另一个对象是它的可空版本)。什么时候

  

从'System.Int32'到'System.Nullable`1 [[System.Int32,   mscorlib,版本= 4.0.0.0,文化=中性,   公钥= b77a5c561934e089]]”。

int,应该是可转换/类型转换为nullable int,这里有什么问题?

3 个答案:

答案 0 :(得分:113)

您必须使用Nullable.GetUnderlyingType来获取Nullable的基础类型。

这是我用来克服ChangeType

Nullable限制的方法
public static T ChangeType<T>(object value) 
{
   var t = typeof(T);

   if (t.IsGenericType && t.GetGenericTypeDefinition().Equals(typeof(Nullable<>))) 
   {
       if (value == null) 
       { 
           return default(T); 
       }

       t = Nullable.GetUnderlyingType(t);
   }

   return (T)Convert.ChangeType(value, t);
}

非通用方法:

public static object ChangeType(object value, Type conversion) 
{
   var t = conversion;

   if (t.IsGenericType && t.GetGenericTypeDefinition().Equals(typeof(Nullable<>))) 
   {
       if (value == null) 
       { 
           return null; 
       }

       t = Nullable.GetUnderlyingType(t);
   }

   return Convert.ChangeType(value, t);
}

答案 1 :(得分:7)

  

对于上面我可以简单地写int? nVal = val

实际上,你也做不到。没有从objectNullable<int>的隐式转换。但 是从intNullable<int>的隐式转换,因此您可以这样写:

int? unVal = (int)val;

您可以使用Nullable.GetUnderlyingType方法。

  

返回指定可为空类型的基础类型参数

     

泛型类型定义是类型声明,例如Nullable,   包含类型参数列表和类型参数列表   声明一个或多个类型参数。封闭的泛型类型是一种类型   声明,其中为类型参数指定了特定类型。

Type t = typeof(int?); //will get this dynamically
Type u = Nullable.GetUnderlyingType(t);
object val = 5; //will get this dynamically
object nVal = Convert.ChangeType(val, u);// nVal will be 5

这是DEMO

答案 2 :(得分:2)

我想我应该解释为什么这个功能不起作用:

1-抛出异常的行如下:

throw new InvalidCastException(Environment.GetResourceString("InvalidCast_FromTo", new object[]
  {
    value.GetType().FullName, 
    targetType.FullName
    }));
事实上,在数组Convert.ConvertTypes中的函数搜索之后,它会看到目标是否为枚举,当找不到任何内容时,它会抛出上面的异常。

2-将Convert.ConvertTypes初始化为:

Convert.ConvertTypes = new RuntimeType[]
   {
      (RuntimeType)typeof(Empty), 
      (RuntimeType)typeof(object), 
      (RuntimeType)typeof(DBNull), 
      (RuntimeType)typeof(bool), 
      (RuntimeType)typeof(char), 
      (RuntimeType)typeof(sbyte), 
      (RuntimeType)typeof(byte), 
      (RuntimeType)typeof(short), 
      (RuntimeType)typeof(ushort), 
      (RuntimeType)typeof(int), 
      (RuntimeType)typeof(uint), 
      (RuntimeType)typeof(long), 
      (RuntimeType)typeof(ulong), 
      (RuntimeType)typeof(float), 
      (RuntimeType)typeof(double), 
      (RuntimeType)typeof(decimal), 
      (RuntimeType)typeof(DateTime), 
      (RuntimeType)typeof(object), 
      (RuntimeType)typeof(string)
   };

因为int?不在ConvertTypes数组中而不是Enum,所以抛出了异常。

所以要恢复,要使用Convert.ChnageType函数,你需要:

  1. 要转换的对象是IConvertible

  2. 目标类型在ConvertTypes范围内,而不是Empty,也不是DBNull(对抛出异常的那两个进行了一次显式测试)

  3. 此行为是因为int(以及所有其他默认类型)使用Convert.DefaultToType作为IConvertibale.ToType implementation. and here is the code of the DefaultToType extracted使用ILSpy

    internal static object DefaultToType(IConvertible value, Type targetType, IFormatProvider provider)
    {
        if (targetType == null)
        {
            throw new ArgumentNullException("targetType");
        }
        RuntimeType left = targetType as RuntimeType;
        if (left != null)
        {
            if (value.GetType() == targetType)
            {
                return value;
            }
            if (left == Convert.ConvertTypes[3])
            {
                return value.ToBoolean(provider);
            }
            if (left == Convert.ConvertTypes[4])
            {
                return value.ToChar(provider);
            }
            if (left == Convert.ConvertTypes[5])
            {
                return value.ToSByte(provider);
            }
            if (left == Convert.ConvertTypes[6])
            {
                return value.ToByte(provider);
            }
            if (left == Convert.ConvertTypes[7])
            {
                return value.ToInt16(provider);
            }
            if (left == Convert.ConvertTypes[8])
            {
                return value.ToUInt16(provider);
            }
            if (left == Convert.ConvertTypes[9])
            {
                return value.ToInt32(provider);
            }
            if (left == Convert.ConvertTypes[10])
            {
                return value.ToUInt32(provider);
            }
            if (left == Convert.ConvertTypes[11])
            {
                return value.ToInt64(provider);
            }
            if (left == Convert.ConvertTypes[12])
            {
                return value.ToUInt64(provider);
            }
            if (left == Convert.ConvertTypes[13])
            {
                return value.ToSingle(provider);
            }
            if (left == Convert.ConvertTypes[14])
            {
                return value.ToDouble(provider);
            }
            if (left == Convert.ConvertTypes[15])
            {
                return value.ToDecimal(provider);
            }
            if (left == Convert.ConvertTypes[16])
            {
                return value.ToDateTime(provider);
            }
            if (left == Convert.ConvertTypes[18])
            {
                return value.ToString(provider);
            }
            if (left == Convert.ConvertTypes[1])
            {
                return value;
            }
            if (left == Convert.EnumType)
            {
                return (Enum)value;
            }
            if (left == Convert.ConvertTypes[2])
            {
                throw new InvalidCastException(Environment.GetResourceString("InvalidCast_DBNull"));
            }
            if (left == Convert.ConvertTypes[0])
            {
                throw new InvalidCastException(Environment.GetResourceString("InvalidCast_Empty"));
            }
        }
        throw new InvalidCastException(Environment.GetResourceString("InvalidCast_FromTo", new object[]
        {
            value.GetType().FullName, 
            targetType.FullName
        }));
    }
    

    另一方面,强制转换由Nullable类本身实现,定义为:

    public static implicit operator T?(T value)
    {
        return new T?(value);
    }
    public static explicit operator T(T? value)
    {
        return value.Value;
    }
    
相关问题