title: How to register dependency injection with generic types? (.net core)
body: <p>I have an asp.net core web app with multiple parameters in appSettings.json file.</p>
  
  <p>I didnt' want to have services having <code>IOptions&lt;MyObject&gt;</code> in the constructor.</p>
  
  <p>I wanted MyObject in the constructor.
  So I found the following article: <a href="https://weblog.west-wind.com/posts/2017/dec/12/easy-configuration-binding-in-aspnet-core-revisited" rel="noreferrer">https://weblog.west-wind.com/posts/2017/dec/12/easy-configuration-binding-in-aspnet-core-revisited</a> which is very interesting.</p>
  
  <p>But I want to go further. I would like to create an extension method to generate the injection.</p>
  
  <p>Here is what I would like to do:</p>
  
  <pre class="lang-cs prettyprint-override"><code>using Microsoft.Extensions.Configuration;
  using Microsoft.Extensions.DependencyInjection;
  
  namespace Common.WebTools.Extensions
  {
      public static class IServiceCollectionExtensions
      {
          public static IServiceCollection AddSingletonConfigurationObject&lt;T&gt;(this IServiceCollection services, 
              IConfiguration configuration,
              string appSettingsKey) where T:new()
          {   
              var obj2 = new T();
              configuration.Bind(appSettingsKey, obj);
              services.AddSingleton(obj2); //compilation failed
              return services;
          }
      }
  }
  </code></pre>
  
  <p>And then in my ConfigureServices method I can call</p>
  
  <pre class="lang-cs prettyprint-override"><code>services.AddSingletonConfigurationObject&lt;Common.Tools.Configuration.GoogleAnalyticsConfiguration&gt;(Configuration, "GoogleAnalytics");
  </code></pre>
  
  <p>But I Have a compliation error on this line:</p>
  
  <pre class="lang-cs prettyprint-override"><code>services.AddSingleton(obj2); 
  </code></pre>
  
  <p>Does somebody know how could I correct the error?</p>