Note that there are some explanatory texts on larger screens.

plurals
  1. POdefault(T) on reflected type
    text
    copied!<p>In browsing other answers, I have come up with the following extension method which works a treat:</p> <pre><code>public static T Convert&lt;T&gt;( this string input ) { var converter = TypeDescriptor.GetConverter( typeof( T ) ); if ( converter != null ) { try { T result = (T) converter.ConvertFromString( input ); return result; } catch { return default( T ); } } return default( T ); } </code></pre> <p>And I can use it like:</p> <pre><code>string s = "2011-09-21 17:45"; DateTime result = s.ConvertTo( typeof( DateTime ) ); if ( result == DateTime.MinValue ) doSomethingWithTheBadData(); </code></pre> <p>Awesome! Works great. Now, I'd like to do something similar with a reflected type. I have:</p> <pre><code>public static dynamic ConvertTo( this string input, Type type ) { var converter = TypeDescriptor.GetConverter( type ); if ( converter != null ) { try { dynamic result = converter.ConvertFromString( input ); return ( result ); } catch { return default( type ); // bogus } } return default( type ); // bogus } </code></pre> <p>And I'd like to use it thus:</p> <pre><code>Type someType; // will be DateTime, int, etc., but not known until runtime DateTime result = s.ConvertTo( sometype ); if ( result == DateTime.MinValue ) doSomethingWithTheBadData(); </code></pre> <p>Of course, the compiler objects to the 'bogus' lines in the <strong>ConvertTo</strong> method. What I need (ok, don't necessarily <em>need</em>, but it'd be nice) is a way to get the same result as in the first example so that if the conversion fails, something that can be assigned to the reflected object is returned and can be recognized in the same manner as in the first example.</p> <p><strong>Edit:</strong></p> <p>What I finished with:</p> <pre><code>public static dynamic ConvertTo( this string input, Type type, out bool success ) { dynamic result; var converter = TypeDescriptor.GetConverter( type ); if ( converter != null ) { try { result = converter.ConvertFromString( input ); success = true; return result; } catch { /* swallow the exception */ } } result = type.IsValueType ? Activator.CreateInstance( type ) : null; success = false; return result; } </code></pre> <p>and used:</p> <pre><code>bool success; string val = "2011-09-21 17:25"; dateTime = val.ConvertTo( typeof( DateTime ), out success ); if ( success ) doSomethingGood(); val = "foo"; dateTime = val.ConvertTo( typeof( DateTime ), out success ); if ( !success ) dealWithBadData(); </code></pre> <p>Remembering that, for the purpose of demonstration, I'm hard-coding the <strong>typeof()</strong> bit. In my application, the types are all reflected.</p> <p>Thanks to all for the speedy answers!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload