当Convert.ChangeType遇到Nullable<T>的时候,转换的时候会出现InvalidCastException的异常,我在网上搜了一下,发现了这样的解决方法,如下:

 

 value, Type conversionType)
        {
            // Note: This if block was taken from Convert.ChangeType as is, and is needed here since we're
            
// checking properties on conversionType below.
            if (conversionType == null)
            {
                
throw new ArgumentNullException("conversionType");
            } 
// end if

            
// If it's not a nullable type, just pass through the parameters to Convert.ChangeType

            
if (conversionType.IsGenericType &&
              conversionType.GetGenericTypeDefinition().Equals(
typeof(Nullable<>)))
            {
                
// It's a nullable type, so instead of calling Convert.ChangeType directly which would throw a
                
// InvalidCastException (per http://weblogs.asp.net/pjohnson/archive/2006/02/07/437631.aspx),
                
// determine what the underlying type is
                
// If it's null, it won't convert to the underlying type, but that's fine since nulls don't really
                
// have a type--so just return null
                
// Note: We only do this check if we're converting to a nullable type, since doing it outside
                
// would diverge from Convert.ChangeType's behavior, which throws an InvalidCastException if
                
// value is null and conversionType is a value type.
                if (value == null)
                {
                    
return null;
                } 
// end if

                
// It's a nullable type, and not null, so that means it can be converted to its underlying type,
                
// so overwrite the passed-in conversion type with this underlying type
                NullableConverter nullableConverter = new NullableConverter(conversionType);
               
                conversionType 
= nullableConverter.UnderlyingType;
            } 
// end if

            
// Now that we've guaranteed conversionType is something Convert.ChangeType can handle (i.e. not a
            
// nullable type), pass the call on to Convert.ChangeType
            return Convert.ChangeType(value, conversionType);
        }

 

 

 Enum.ToObject(type, value); 

if (!type.IsInterface && type.IsGenericType) { 
Type innerType 
= type.GetGenericArguments()[0]; 
object innerValue = QueryHelper.ChangeType(value, innerType); 
return Activator.CreateInstance(type, new object[] { innerValue }); 

if (value is string && type == typeof(Guid)) return new Guid(value as string); 
if (value is string && type == typeof(Version)) return new Version(value as string); 
if (!(value is IConvertible)) return value; 
return Convert.ChangeType(value, type); 


It may have more than you need, and there are bound to be even more special cases like Guid and Version, but it works so far. 


相关文章:

  • 2021-09-23
  • 2022-12-23
  • 2022-12-23
  • 2022-02-25
  • 2022-12-23
  • 2021-09-11
  • 2021-04-19
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-09-02
  • 2022-12-23
  • 2021-07-04
  • 2021-08-19
  • 2022-02-08
  • 2022-12-23
相关资源
相似解决方案