title: Test if string can be converted to other, various type
body: <p>(SOLVED) I'm building an application that can create some of its control dynamically, based on some description from XML file.<br>
  What I need now is something very similar to TryParse() method: a possibility to check (wihtout throwing/catching exception), if a text in string variable can be converted (or parsed) to a type, which name I have in other variabe (<code>myType</code>).<br>
  Problem is that <code>myType</code> can be any of .NET types: <code>DateTime, Bool, Double, Int32</code> etc.</p>
  
  <p>Example:  </p>
  
  <pre><code>string testStringOk = "123";
  string testStringWrong = "hello";
  string myType = "System.Int32";
  
  bool test1 = CanCovertTo(testStringOk, myType);      //true
  bool test2 = CanCovertTo(testStringWrong, myType);   //false
  </code></pre>
  
  <p>How does <code>CanCovertTo(string testString, string testType)</code> function should look like?</p>
  
  <p>The closest I get is following code:  </p>
  
  <pre><code>private bool CanCovertTo(string testString, string testType)
  {
      Type type = Type.GetType(testType, null, null);
      TypeConverter converter = TypeDescriptor.GetConverter(type);
  
      converter.ConvertFrom(testString);  //throws exception when wrong type
      return true;
  }
  </code></pre>
  
  <p>however, it throws an exception while trying to convert from wrong string, and I prefer not to use <code>try {} catch()</code> for that.   </p>
  
  <hr>
  
  <p><strong>Solution:</strong> </p>
  
  <pre><code>private bool CanCovertTo(string testString, string testType)
  {
      Type type = Type.GetType(testType, null, null);
      TypeConverter converter = TypeDescriptor.GetConverter(type);
      return converter.IsValid(testString);
  }
  </code></pre>