Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Why not just use a simple extension method?</p> <p>Jon Skeet's answer about just using the default result from the various TryParse methods is good. There <em>is</em> still a nice aspect of the extension methods, though. If you are doing this a lot, you can accomplish the same thing in the calling code (plus optionally specifying an explicit default) in one line of code rather than three.</p> <p><strong>-- EDIT --</strong> I <strong>do</strong> realize that in my original answer I basically just provided a slightly different way of doing the same thing the author was already doing. I caught this earlier today when I was real busy, thought the delegate and custom parser stuff looked like it might be a bit much, then cranked out an answer without <em>really</em> taking the time to completely understand what the question was. Sorry.</p> <p>How about the following, which uses an (overloaded) extension method and reflection? Refer to <a href="https://stackoverflow.com/a/4740544/618649">https://stackoverflow.com/a/4740544/618649</a></p> <p>Caveat Emptor: my example does not account for you trying to convert types that do not have a TryParse method. There should be some exception handling around the <code>GetMethod</code> call, and so on.</p> <pre><code>/* The examples generates this output when run: 0 432123 -1 1/1/0001 12:00:00 AM 1/1/1970 12:00:00 AM 1/30/2013 12:00:00 PM -1 12342.3233443 */ class Program { static void Main ( string[] args ) { Debug.WriteLine( "blah".Parse&lt;Int64&gt;() ); Debug.WriteLine( "432123".Parse&lt;long&gt;() ); Debug.WriteLine( "123904810293841209384".Parse&lt;long&gt;( -1 ) ); Debug.WriteLine( "this is not a DateTime value".Parse&lt;DateTime&gt;() ); Debug.WriteLine( "this is not a DateTime value".Parse&lt;DateTime&gt;( "jan 1, 1970 0:00:00".Convert&lt;DateTime&gt;() ) ); Debug.WriteLine( "2013/01/30 12:00:00".Parse&lt;DateTime&gt;() ); Debug.WriteLine( "this is not a decimal value".Parse&lt;decimal&gt;( -1 ) ); Debug.WriteLine( "12342.3233443".Parse&lt;decimal&gt;() ); } } static public class Extensions { static private Dictionary&lt;Type,MethodInfo&gt; s_methods = new Dictionary&lt;Type, MethodInfo&gt;(); static public T Parse&lt;T&gt; ( this string value ) where T : struct { return value.Parse&lt;T&gt;( default( T ) ); } static public T Parse&lt;T&gt; ( this string value, T defaultValue ) where T : struct { // *EDITED* to cache the Reflection lookup--NOT thread safe MethodInfo m = null; if ( s_methods.ContainsKey( typeof( T ) ) ) { m = s_methods[ typeof( T ) ]; } else { m = typeof( T ).GetMethod( "TryParse" , BindingFlags.Public | BindingFlags.Static , Type.DefaultBinder , new[] { typeof( string ), typeof( T ).MakeByRefType() } , null ); s_methods.Add( typeof( T ), m ); } var args = new object[] { value, null }; if( (bool)m.Invoke( null, args )) { return (T) args[ 1 ]; } return defaultValue; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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