title: How Do I Return An IConvertible From A Generic Method If T Has No Parameterless Constructor?
body: <p>I have a custom class that reads in a set of key-pair values as objects that implement IConvertible. Instead of writing a host of GetValueAsSomeObjectType methods, I want to write one generic method instead. </p>
  
  <p>I think I've ALMOST got it, but not quite:</p>
  
  <pre><code>    public static IConvertible GetSetting&lt;T&gt;(string settingName,
          string settingsFile = null, bool
          ignoreCase = true) where T : IConvertible, new()
      {
          IConvertible t = new T();
  
          foreach (Setting setting in Settings)
          {
              if ((settingsFile == setting.File || settingsFile == null)
                  &amp;&amp; (settingName == setting.Name
                     || (ignoreCase == true &amp;&amp; (settingName.ToLower() == setting.Name.ToLower()))))
              {
                  t = (IConvertible)setting.Value;
                  return (T)t.ToType(typeof(T), null);
              }
          }
  
          return t;
      }
  </code></pre>
  
  <p>Now I seem to be able to do this, which is kind of cool:</p>
  
  <pre><code>object KeyName;
  bool ValueAsBool = (bool)GetSetting&lt;bool&gt;(nameof(KeyName));
  </code></pre>
  
  <p>However the following results in a syntax error, because string doesn't have a parameterless constructor:</p>
  
  <pre><code>string ValueAsString = (string)GetSetting&lt;string&gt;(nameof(KeyName));
  </code></pre>
  
  <p>Any suggestions on how to make this work?</p>