title: Passing DataTable Column Type to generic method <T>
body: <p>I have a method declared as such</p>
  
  <pre><code>public static T GetValidatedValue&lt;T&gt;(string param)
  {
     do something here and return object of type T...;
  }
  </code></pre>
  
  <p>usually I call it like this   var somnum = GetValidatedValue("14");
  and expect sumNum to be number or if an invalid value was passed to be "0"</p>
  
  <p>my problem now is that I need to pass a datatabel column type as "T" into this method</p>
  
  <p>something like : </p>
  
  <pre><code>dr[col] = GetValidatedValue &lt;typeof(dr[col])&gt;(dr[col].ToString());
  </code></pre>
  
  <p>this will not compile</p>
  
  <p>it is basicaly a combination of two(2) methods that I have found somewhere(maybe even on this site) and modified to work as i needed </p>
  
  <pre><code>public static T GetValidatedValue&lt;T&gt;(string param)
  {
      return TryParse&lt;T&gt;(param);
  }
  
  private static T TryParse&lt;T&gt;(string inValue)
  {
      var converter = TypeDescriptor.GetConverter(typeof(T));
  
      try
      {
          return (T)converter.ConvertFromString(null, CultureInfo.InvariantCulture,     inValue);
      }
      catch
      {
          return default(T);
      }
  }
  </code></pre>
  
  <p>can anyone shed some light on what I am doing wrong...</p>