title: Find class by generic interface
body: <p>In my application I have the following class to get instances of an interface:</p>
  
  <pre><code>public static class ServiceProvider
  {
      private static readonly Dictionary&lt;Type, dynamic&gt; _serviceStorage;
      private static readonly object _serviceLock;
  
      static ServiceProvider()
      {
          _serviceLock = new object();
          _serviceStorage = new Dictionary&lt;Type, dynamic&gt;();
      }
  
      public static T GetService&lt;T&gt;()
      {
          Type serviceType = typeof (T);
          lock (_serviceLock)
          {
              if (!_serviceStorage.ContainsKey(serviceType))
              {
                  _serviceStorage.Add(serviceType, (T) CreateService(serviceType));
              }
          }
          return _serviceStorage[serviceType];
      }
  
      private static dynamic CreateService(Type serviceType)
      {
          Type implementationType = AppDomain.CurrentDomain.GetAssemblies()
              .SelectMany(assembly =&gt; assembly.GetTypes())
              .FirstOrDefault(type =&gt; serviceType.IsAssignableFrom(type) &amp;&amp; type.IsClass);
          if (implementationType == null)
              throw new NullReferenceException();
          return Activator.CreateInstance(implementationType);
      }
  }
  </code></pre>
  
  <p>The implementation-class must implement the provided interface and have a parameterless constructor.</p>
  
  <p>For example I have the interface <code>IEventRegistrationService</code> which looks something like:</p>
  
  <pre><code>public interface IEventRegistrationService
  {
    void Register(string name, Action&lt;object&gt; callback);
  }
  </code></pre>
  
  <p>and the implementation-class looks like:</p>
  
  <pre><code>internal class EventRegistrationService : IEventRegistrationService
  {
    public void Register(string name, Action&lt;object&gt; callback)
    {
       // Register the action with this name...
    }
  }
  </code></pre>
  
  <p>And the usage of my service looks like:</p>
  
  <pre><code>IEventRegistrationService service = ServiceProvider.GetService&lt;IEventRegistrationService&gt;();
  </code></pre>
  
  <p>Now I have an instance of my interface. Everything just works fine until here.</p>
  
  <p>Now I want to make the IEventRegistrationService-interface generic to have the correct type in the callback-action. The "new" interface should look like:</p>
  
  <pre><code>public interface IEventRegistrationService&lt;T&gt;
  {
    void Register(string name, Action&lt;T&gt; callback);
  }
  </code></pre>
  
  <p>And the implementation-class now looks like:</p>
  
  <pre><code>internal class EventRegistrationService&lt;T&gt; : IEventRegistrationService&lt;T&gt;
  {
     public void Register(string name, Action&lt;T&gt; callback)
     {
        // Register the action with this name...
     }
  }
  </code></pre>
  
  <p>And the usage should be like:</p>
  
  <pre><code>IEventRegistrationService&lt;string&gt; service = ServiceProvider.GetService&lt;IEventRegistrationService&lt;string&gt;&gt;();
  </code></pre>
  
  <p>But with this generic I get an exception in <code>ServiceProvider</code>-class in the CreateService method. </p>
  
  <p>The statement:</p>
  
  <pre><code> Type implementationType = AppDomain.CurrentDomain.GetAssemblies()
                  .SelectMany(assembly =&gt; assembly.GetTypes())
                  .FirstOrDefault(type =&gt; serviceType.IsAssignableFrom(type) &amp;&amp; type.IsClass);
  </code></pre>
  
  <p>returns null.</p>
  
  <p>I guess this is because of the <code>.IsAssignableFrom(type)</code> but I don't know how to solve this problem. </p>
  
  <p>How can I check if a class implements a generic interface?</p>
  
  <p>Edit:</p>
  
  <p>I don't know what interface-type is expected.</p>