title: Casting to a generic interface with a dynamic type parameter
body: <p>I have two generic interfaces like these:</p>
  
  <pre><code>public interface IFoo&lt;T&gt; { }
  public interface IBar&lt;TFoo, T&gt; where TFoo : Foo&lt;T&gt; { 
      T Qux(TFoo foo);
  }
  </code></pre>
  
  <p><code>IFoo&lt;T&gt;</code> has a bunch of implementations, each with a matching implementation of <code>IBar&lt;S,T&gt;</code>, registered in my IoC container (which happens to be Castle.Windsor).</p>
  
  <p>Now, I want to create a method which, based on an argument of some type that implements <code>IFoo&lt;T&gt;</code>, returns a corresponding <code>IBar&lt;IFoo&lt;T&gt;,T&gt;</code> from Windsor. A naïve implementation looks like this:</p>
  
  <pre><code>public IBar&lt;TFoo,T&gt; GetBarFor(IFoo&lt;T&gt; foo) where TFoo : IFoo&lt;T&gt;
  {
      return container.Resolve&lt;IBar&lt;TFoo,T&gt;&gt;();
  }
  </code></pre>
  
  <p>However, in the calling code, I don't have a concretely typed <code>Foo&lt;T&gt;</code> - just an <code>IFoo&lt;T&gt;</code>:</p>
  
  <pre><code>public T DoStuffWithStuff&lt;T&gt;(IFoo&lt;T&gt; foo)
  {
      var bar = GetBarFor(foo);
      return bar.Qux(foo);
  }
  </code></pre>
  
  <p>and in this call, the compiler is unable to deduce the type parameters (specifically, I guess, <code>TFoo</code>), so I need to take another approach.</p>
  
  <p>I have tried to resolve the type with the non-generic version of <code>Resolve()</code>, but then I can't get the return type right:</p>
  
  <pre><code>public AbstractBar&lt;T&gt; GetBarFor(IFoo&lt;T&gt; foo) where TFoo : IFoo&lt;T&gt;
  {
      var barType = typeof(IBar&lt;,&gt;).MakeGenericType(foo.GetType(),typeof(T));
      var bar = container.Resolve(barType); // bar is now a System.Object
  
      var wrapperType = typeof(BarWrapper&lt;,&gt;).MakeGenericType(foo.GetType(), typeof(T));
      var wrapper = Activator.CreateInstance(wrapperType, bar);
  
      return (AbstractBar&lt;T&gt;)wrapper;
  }
  </code></pre>
  
  <p>with</p>
  
  <pre><code>public abstract class AbstractBar&lt;T&gt; { public abstract T Qux(IFoo&lt;T&gt; foo); }
  
  public class BarWrapper&lt;TFoo, T&gt; : AbstractBar&lt;T&gt; where TFoo : IFoo&lt;T&gt;
  {
      private IBar&lt;TFoo,T&gt; _inner;
      public BarWrapper(IBar&lt;TFoo,T&gt; inner) { _inner = inner; }
  
      public T Qux(IFoo&lt;T&gt; foo) { return _inner.Qux((TFoo)foo); }
  }
  </code></pre>
  
  <p>but I keep getting exceptions on the <code>Activator.CreateInstance</code> call saying that the constructor for <code>BarWrapper</code> is not found (yes, everything is <code>public</code>).</p>
  
  <p>Is there a good way to get the call in <code>DoStuffWithStuff</code> above working for exactly that signature (i.e. I can't defer any more specific generics from the calling code)? </p>
  
  <hr>
  
  <p><sup>Footnote: I have seen the latter pattern work, in <a href="https://github.com/jbogard/MediatR" rel="nofollow">Jimmie Bogard's MediatR implementation of the mediator pattern</a>, and in fact what I'm trying to accomplish here is a very similar implementation, that doesn't rely on the Common Service Locator, but rather goes straight to our IoC container. In context, <code>IFoo&lt;T&gt;</code> is really <code>IRequest&lt;out TResponse&gt;</code>, <code>IBar&lt;TFoo, T&gt;</code> is really <code>IRequestHandler&lt;in TRequest, out TResponse&gt;</code>, etc.</sup></p>
  
  <p><sup>Looking at his code, and trying to recreate it but with a call to the Windsor container instead of the service locator, I can't figure out why his code works, and mine does not.</sup></p>