Search code examples
c#.netcastingtype-conversionnullable

Invalid cast from 'System.Int32' to 'System.Nullable`1[[System.Int32, mscorlib]]


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

I am getting InvalidCastException in above code. For above I could simply write int? nVal = val, but above code is executing dynamically.

I am getting a value(of non nullable type like int, float, etc) wrapped up in an object (here val), and I have to save it to another object by casting it to another type(which can or cannot be nullable version of it). When

Invalid cast from 'System.Int32' to 'System.Nullable`1[[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]'.

An int, should be convertible/type-castable to nullable int, what is the issue here ?


Solution

  • You have to use Nullable.GetUnderlyingType to get underlying type of Nullable.

    This is the method I use to overcome limitation of ChangeType for 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);
    }
    

    non generic method:

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