Nullable 类型的转换

本文提供两种解决方案,用于解决.NET中Nullable类型无法直接使用Convert.ChangeType进行转换的问题。通过自定义转换方法,实现对Nullable类型的灵活处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

今天碰到Nullable 不能通过Convert.ChangeType转换,辛苦在网上找到两个解决方法,共享一下。
1. The PumaCode.org Blog
None.gif public   object  ChangeType( object  value, Type conversionType)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
if ( conversionType.IsGenericType &&
ExpandedSubBlockStart.gifContractedSubBlock.gif        conversionType.GetGenericTypeDefinition( ).Equals( 
typeof( Nullable<> ) ) ) dot.gif{
InBlock.gif 
InBlock.gif        
if(value == null)
InBlock.gif            
return null;
InBlock.gif 
InBlock.gif        System.ComponentModel.NullableConverter nullableConverter
InBlock.gif            
= new System.ComponentModel.NullableConverter(conversionType);
InBlock.gif 
InBlock.gif        conversionType 
= nullableConverter.UnderlyingType;
ExpandedSubBlockEnd.gif    }

InBlock.gif 
InBlock.gif    
return Convert.ChangeType(value, conversionType);
ExpandedBlockEnd.gif}
引用: http://blog.pumacode.org/2006/05/18/using-convert-changetype-on-nullable-types/

2. Paul Wilson's .NET Blog
None.gif public   class  DataTypeConverter
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        
public static object ChangeType(Type type,object value)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if ((value == null&& type.IsGenericType)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return Activator.CreateInstance(type);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
if (value == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return null;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
if (type == value.GetType())
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return value;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
if (type.IsEnum)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (value is string)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
return Enum.Parse(type, value as string);
ExpandedSubBlockEnd.gif                }

InBlock.gif                
return Enum.ToObject(type, value);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
if (!type.IsInterface && type.IsGenericType)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Type type1 
= type.GetGenericArguments()[0];
InBlock.gif                
object obj1 = DataTypeConverter.ChangeType(type1,value);
ExpandedSubBlockStart.gifContractedSubBlock.gif                
return Activator.CreateInstance(type, new object[] dot.gif{ obj1 });
ExpandedSubBlockEnd.gif            }

InBlock.gif            
if ((value is string&& (type == typeof(Guid)))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return new Guid(value as string);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
if ((value is string&& (type == typeof(Version)))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return new Version(value as string);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
if (!(value is IConvertible))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return value;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return Convert.ChangeType(value, type);
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }
这个代码是WilsonORMapper中的QueryHelper类,不好意思,我Reflector了一下。
引用: http://weblogs.asp.net/pjohnson/archive/2006/02/07/437631.aspx
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值