Convert.ChangeType wrapper that handles nullable types

As I mentioned before, the Convert.ChangeType method doesn't handle nullables. One of the comments proposed a replacement, but I preferred to have a wrapper around the original Convert.ChangeType method, so in general it would behave identical to Convert.ChangeType, except that it would handle nullable types as well instead of throwing an InvalidCastException.

I wrote, compiled, and tested such a wrapper finally, and posted it as a CodeSnip article on ASP Alliance, where I've also been an author for over five years. Check out my ChangeType method, use it, let me know what you think! Oh, and if anyone on the CLR team is reading this post, feel free to incorporate my wrapper in the next .NET Framework service pack. ;-)

Special thanks to a fellow ASP Alliance author, the multiple blog-having J. Ambrose Little and alert reader, Google user, weblogs.asp.net surfer, and my brother, Toby Johnson for helping me put together this method.
Published Friday, May 19, 2006 1:06 PM by pjohnson

Comments

# Convert.ChangeType doesn't handle nullables

I had an assembly, originally written against .NET 1.1 and now running
on .NET 2.0, that tried to convert...

Wednesday, May 31, 2006 11:42 AM by Peter Johnson's Blog

# Convert.ChangeType extension « Chris Cavanagh’s Blog

Pingback from  Convert.ChangeType extension « Chris Cavanagh’s Blog

Monday, November 05, 2007 1:13 AM by Convert.ChangeType extension « Chris Cavanagh’s Blog

# re: Convert.ChangeType wrapper that handles nullable types

any Guid supports ??

           if (conversionType == typeof(System.Guid))

           {

               return new Guid(value.ToString());

           }

Tuesday, November 03, 2009 9:02 AM by alhambraeidos

# re: Convert.ChangeType wrapper that handles nullable types

any Guid supports ??

           if (conversionType == typeof(System.Guid))

           {

               return new Guid(value.ToString());

           }

Tuesday, November 03, 2009 9:03 AM by alhambraeidos

# re: Convert.ChangeType wrapper that handles nullable types

You forgot to check for DbNull.

Thursday, March 11, 2010 10:29 AM by iowa man

# re: Convert.ChangeType wrapper that handles nullable types

iowa man:

DBNull is for database values. Regular null is appropriate in this case.

Thursday, March 11, 2010 10:11 PM by pjohnson

# re: Convert.ChangeType wrapper that handles nullable types

Can you please re-post the code for the ChangeType method that handles nullable types. The link you have posted is now dead.

Friday, October 21, 2011 12:35 AM by rp

# re: Convert.ChangeType wrapper that handles nullable types

Wow I really have to thank you for this. Btw your article can't be seen anymore. I could read it using the archive.org "time machine" (nice ;) ) cheers Paolo

Wednesday, October 26, 2011 7:09 AM by firepol

# re: Convert.ChangeType wrapper that handles nullable types

shots of  <a href=www.newtopics.info/prime-actresses-who-have-asked-unclothed.html> naked movies</a> celebrity naked .

Thursday, December 01, 2011 12:35 PM by nakedcelebrityv

Leave a Comment

(required) 
(required) 
(optional)
(required)