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

Leave a Comment

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