<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://weblogs.asp.net/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Convert.ChangeType doesn't handle nullables</title><link>http://weblogs.asp.net/pjohnson/archive/2006/02/07/Convert.ChangeType-doesn_2700_t-handle-nullables.aspx</link><description>I had an assembly, originally written against .NET 1.1 and now running on .NET 2.0, that tried to convert a boxed DateTime object to a Nullable&amp;lt;DateTime&amp;gt; (DateTime?) object, using the Convert.ChangeType method. When this code runs, I get this exception</description><dc:language>en</dc:language><generator>CommunityServer 2007 SP1 (Build: 20510.895)</generator><item><title>re: Convert.ChangeType doesn't handle nullables</title><link>http://weblogs.asp.net/pjohnson/archive/2006/02/07/Convert.ChangeType-doesn_2700_t-handle-nullables.aspx#6916210</link><pubDate>Wed, 18 Feb 2009 22:20:37 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6916210</guid><dc:creator>Thomas Jensen</dc:creator><author>Thomas Jensen</author><description>&lt;p&gt;public static T ChangeType&amp;lt;T&amp;gt;(object value)&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt;Type conversionType = typeof(T);&lt;/p&gt;
&lt;p&gt;if (conversionType.IsGenericType &amp;amp;&amp;amp; conversionType.GetGenericTypeDefinition().Equals(typeof(Nullable&amp;lt;&amp;gt;)))&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt;if (value == null)&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt;return default(T);&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;else&lt;/p&gt;
&lt;p&gt;{				NullableConverter nullableConverter = new NullableConverter(conversionType);&lt;/p&gt;
&lt;p&gt;conversionType = nullableConverter.UnderlyingType;&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;return (T)Convert.ChangeType(value, conversionType);&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6916210" width="1" height="1"&gt;</description></item><item><title>re: Convert.ChangeType doesn't handle nullables</title><link>http://weblogs.asp.net/pjohnson/archive/2006/02/07/Convert.ChangeType-doesn_2700_t-handle-nullables.aspx#6427042</link><pubDate>Mon, 21 Jul 2008 12:37:45 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6427042</guid><dc:creator>Idrees</dc:creator><author>Idrees</author><description>&lt;p&gt;Great work. especially the generic method. &lt;/p&gt;
&lt;p&gt;Thankx.&lt;/p&gt;
&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6427042" width="1" height="1"&gt;</description></item><item><title>re: Convert.ChangeType doesn't handle nullables</title><link>http://weblogs.asp.net/pjohnson/archive/2006/02/07/Convert.ChangeType-doesn_2700_t-handle-nullables.aspx#6377973</link><pubDate>Tue, 08 Jul 2008 22:46:31 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6377973</guid><dc:creator>vkelman</dc:creator><author>vkelman</author><description>&lt;p&gt;I slightly modified your &lt;a rel="nofollow" target="_new" href="http://aspalliance.com/852"&gt;http://aspalliance.com/852&lt;/a&gt; code. I canno tput code there due to server error.&lt;/p&gt;
&lt;p&gt;I modified your code slightly, to make it generic. Thanks!&lt;/p&gt;
&lt;p&gt;public static T ChangeType&amp;lt;T&amp;gt;(object value) {&lt;/p&gt;
&lt;p&gt; &amp;nbsp;Type conversionType = typeof(T);&lt;/p&gt;
&lt;p&gt; &amp;nbsp;if (conversionType.IsGenericType &amp;amp;&amp;amp;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp;conversionType.GetGenericTypeDefinition().Equals(typeof(Nullable&amp;lt;&amp;gt;))) {&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;if (value == null) { return default(T); }&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;conversionType = Nullable.GetUnderlyingType(conversionType); ;&lt;/p&gt;
&lt;p&gt; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp;return (T)Convert.ChangeType(value, conversionType);&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6377973" width="1" height="1"&gt;</description></item><item><title>re: Convert.ChangeType doesn't handle nullables</title><link>http://weblogs.asp.net/pjohnson/archive/2006/02/07/Convert.ChangeType-doesn_2700_t-handle-nullables.aspx#5667764</link><pubDate>Mon, 28 Jan 2008 23:37:58 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:5667764</guid><dc:creator>Andrew</dc:creator><author>Andrew</author><description>&lt;p&gt;Very elegant solution Rich, love it. &amp;nbsp;Thanks.&lt;/p&gt;
&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=5667764" width="1" height="1"&gt;</description></item><item><title>re: Convert.ChangeType doesn't handle nullables</title><link>http://weblogs.asp.net/pjohnson/archive/2006/02/07/Convert.ChangeType-doesn_2700_t-handle-nullables.aspx#5347294</link><pubDate>Tue, 27 Nov 2007 05:20:42 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:5347294</guid><dc:creator>Chris Carter</dc:creator><author>Chris Carter</author><description>&lt;p&gt;Thanks Rich, that rocks!&lt;/p&gt;
&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=5347294" width="1" height="1"&gt;</description></item><item><title>re: Convert.ChangeType doesn't handle nullables</title><link>http://weblogs.asp.net/pjohnson/archive/2006/02/07/Convert.ChangeType-doesn_2700_t-handle-nullables.aspx#5175898</link><pubDate>Wed, 14 Nov 2007 16:02:52 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:5175898</guid><dc:creator>Martin</dc:creator><author>Martin</author><description>&lt;p&gt;Excellent solution Rich. Thanks.&lt;/p&gt;
&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=5175898" width="1" height="1"&gt;</description></item><item><title>re: Convert.ChangeType doesn't handle nullables</title><link>http://weblogs.asp.net/pjohnson/archive/2006/02/07/Convert.ChangeType-doesn_2700_t-handle-nullables.aspx#4849533</link><pubDate>Wed, 31 Oct 2007 22:14:18 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:4849533</guid><dc:creator>Rich</dc:creator><author>Rich</author><description>&lt;p&gt;Thanks was looking into this issue..&lt;/p&gt;
&lt;p&gt;How about this as an option?&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;public static class Converter {&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public static T ChangeType&amp;lt;T&amp;gt;(object value) {&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;TypeConverter tc = TypeDescriptor.GetConverter(typeof(T));&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return (T)tc.ConvertFrom(value);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public static void RegisterTypeConverter&amp;lt;T, TC&amp;gt;() where TC : TypeConverter {&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;TypeDescriptor.AddAttributes(typeof(T), &amp;nbsp;new TypeConverterAttribute(typeof(TC)));&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;This allows you to create new TypeDescriptors and add them at runtime, for any missing converters e.g. Version&lt;/p&gt;
&lt;p&gt;public class VersionConverter : TypeConverter &lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) {&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;string strvalue = value as string;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if (strvalue != null) {&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return new Version(strvalue);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;else {&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return new Version();&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;Test code...&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;static void Main(string[] args) {&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Converter.RegisterTypeConverter&amp;lt;Version, VersionConverter&amp;gt;();&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; int? i = Converter.ChangeType&amp;lt;int?&amp;gt;(&amp;quot;123&amp;quot;);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; DateTime dt = Converter.ChangeType&amp;lt;DateTime&amp;gt;(&amp;quot;20:33&amp;quot;);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; char c = Converter.ChangeType&amp;lt;char&amp;gt;(&amp;quot;x&amp;quot;);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Guid g = Converter.ChangeType&amp;lt;Guid&amp;gt;(&amp;quot;{32F92EEB-A703-4eb7-A9F8-62E09F87D03F}&amp;quot;);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Version v = Converter.ChangeType&amp;lt;Version&amp;gt;(&amp;quot;1.2.3.4&amp;quot;);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; DateTime? k = Converter.ChangeType&amp;lt;DateTime?&amp;gt;(null);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=4849533" width="1" height="1"&gt;</description></item><item><title>http://aspalliance.com/852</title><link>http://weblogs.asp.net/pjohnson/archive/2006/02/07/Convert.ChangeType-doesn_2700_t-handle-nullables.aspx#449813</link><pubDate>Wed, 31 May 2006 15:33:42 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:449813</guid><dc:creator>TrackBack</dc:creator><author>TrackBack</author><description>&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=449813" width="1" height="1"&gt;</description></item><item><title>The PumaCode.org Blog: using Convert.ChangeType() on nullable types in C#</title><link>http://weblogs.asp.net/pjohnson/archive/2006/02/07/Convert.ChangeType-doesn_2700_t-handle-nullables.aspx#447287</link><pubDate>Fri, 19 May 2006 19:24:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:447287</guid><dc:creator>TrackBack</dc:creator><author>TrackBack</author><description>The PumaCode.org Blog: using Convert.ChangeType() on nullable types in C#&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=447287" width="1" height="1"&gt;</description></item><item><title>re: Convert.ChangeType doesn't handle nullables</title><link>http://weblogs.asp.net/pjohnson/archive/2006/02/07/Convert.ChangeType-doesn_2700_t-handle-nullables.aspx#437646</link><pubDate>Tue, 07 Feb 2006 19:39:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:437646</guid><dc:creator>Paul Wilson</dc:creator><author>Paul Wilson</author><description>By the way, the call to QueryHelper.ChangeType is just a recursive call on the exact same method, so change the QueryHelper class name to your own class name.&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=437646" width="1" height="1"&gt;</description></item></channel></rss>