当Convert.ChangeType遇到Nullable的时候

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

 

ContractedBlock.gif ExpandedBlockStart.gif Code
 public static object ChangeType(object 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);
        }

 

 

ContractedBlock.gif ExpandedBlockStart.gif Code
Here's my ugly generic ChangeType method (from my ORMapper): 

static public object ChangeType(object value, Type type) { 
if (value == null && type.IsGenericType) return Activator.CreateInstance(type); 
if (value == nullreturn null
if (type == value.GetType()) return value; 
if (type.IsEnum) { 
if (value is string
return Enum.Parse (type, value as string); 
else 
return 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. 


转载于:https://www.cnblogs.com/genson/archive/2008/10/08/1306099.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值