title: Using Ninject (or some other container) How can I find out the type that is requesting the service?
body: <p>Suppose I have an interface for a service:</p>
  
  <pre><code>public interface IFooService
  {
     void DoSomething();
  }
  </code></pre>
  
  <p>And a concrete implementation of that service that is a generic:</p>
  
  <pre><code>public class FooService&lt;TRequestingClass&gt; : IFooService
  {
     public virtual void DoSomething() { }
  }
  </code></pre>
  
  <p>And I have some other class that needs an instance of IFooService:</p>
  
  <pre><code>public class Bar
  {
     private IFooService _fooService;
     public Bar(IFooService fooService)
     {
        this._fooService = fooService;
     }
  }
  </code></pre>
  
  <p>I need to wire up my IoC container such that when Bar gets created, it gets passed a constructor argument of FooService&lt;Bar&gt;. There are many other classes just like Bar. Each might also need an instance of FooService&lt;TRequestingClass&gt; passed to them where TRequestingClass is the type of the class that needs the instance of IFooService. I don't need to expose this quirkiness to the consumers of IFooService. All they should care about is that they can call the methods of the IFooService they were passed. They should not need to know that the concrete implementation of IFooService they were passed needed anything special to be constructed.</p>
  
  <p>An acceptable alternatative to FooService&lt;T&gt; would to be a non-generic class that has a string argument in its constructur that contains the name of the class it is being created for. i.e:</p>
  
  <pre><code>public class FooService : IFooService
  {
     public FooService(string requestingClassName) { }
  }
  </code></pre>
  
  <p><strong>How can I wire up my IoC container to construct a dependency this way?</strong></p>
  
  <p>If you are confused why I would want such a wierd structure, consider how log4net works best when you get an ILog that gets created with log4net.LogManager.GetLogger(typeof(SomeClass)). I don't want to litter my code with references to log4net, so I'd like to write a simple ILogger interface, and implement it with something like this:</p>
  
  <pre><code>public class GenericLogger&lt;T&gt; : ILogger
  {
      private readonly ILog log;
  
      public GenericLogger()
      {
          this.log = log4net.LogManager.GetLogger(typeof(T));
      }
  
      public void Debug(object message)
      {
          this.log.Debug(message);
      }
  
      /* .... etc ....  */
  }
  </code></pre>