使用Convert.ChangeType()不能把datetime转换为datetime?(Nullable<DateTime>)类型,后在Google上查到解决办法,特记之.
原文如下:

While writing a data loading routine in C# with .NET 2.0, I came across an apparent bug which prevented this method from converting a nullable type. A bit of Googling turned up a post by my brother Peter about this issue and a possible workaround.

I took his solution a step further and basically wrote my own ChangeType method which properly handles nullable types. I then got in touch with Peter about my solution and he suggested a better way of detecting the nullable type (I was just using a regex at first). Anyway, the end result is listed below.

In case you’re wondering why I check for a null value only after I determine that conversionType is nullable, it’s because I want the final Convert.ChangeType to throw the exception when conversionType can’t accept a null value, rather than causing the exception to be thrown in the caller.

[转载]using Convert.ChangeType() on nullable types in C#public object ChangeType(object value, Type conversionType)

还有一种方法,可是少个方法,也放上来大家参考一下
 


原文地址

相关文章: