title: Register decorator with another dependency of same generic type
body: <p>I think I have stumbled upon a quirk in Simple Injector's <code>RegisterDecorator()</code>. It occurs even in the most recent 2.5.0. I have a situation where I want to decorate a closed generic type, for example <code>ICommandHandler&lt;MessageCommand&gt;</code>, with a decorator that takes (via constructor injection) an inner handler of type <code>ICommandHandler&lt;MessageCommand&gt;</code>, but also another type of handler, say <code>ICommandHandler&lt;LogCommand&gt;</code>. Even though these command handler types are distinct, SimpleInjector seems to get confused and throws an exception when I call <code>RegisterDecorator</code> on such a decorator type:</p>
  
  <blockquote>
    <p>ArgumentException: For the container to be able to use MessageLogger as a decorator, its constructor must include a single parameter of type <code>ICommandHandler&lt;MessageCommand&gt;</code> (or <code>Func&lt;ICommandHandler&lt;MessageCommand&gt;&gt;</code>) - i.e. the type of the instance that is being decorated. The parameter type <code>ICommandHandler&lt;MessageCommand&gt;</code> is defined multiple times in the constructor of class MessageLogger.</p>
  </blockquote>
  
  <p>... even though the decorator clearly only has one <code>ICommandHandler&lt;MessageCommand&gt;</code> parameter.</p>
  
  <p>Here is the full working example that throws the exception:</p>
  
  <pre><code>public interface ICommandHandler&lt;T&gt;
  {
      void Execute(T command);
  }
  
  public class LogCommand
  {
      public string LogMessage { get; set; }
      public DateTime Time { get; set; }
  }
  
  public class Logger : ICommandHandler&lt;LogCommand&gt;
  {
      public void Execute(LogCommand command)
      {
          Debug.WriteLine(string.Format("Message \"{0}\" sent at {1}",
              command.LogMessage, command.Time));
      }
  }
  
  
  public class MessageCommand
  {
      public string Message { get; set; }
  }
  
  public class MessageSender : ICommandHandler&lt;MessageCommand&gt;
  {
      public void Execute(MessageCommand command)
      {
          Debug.WriteLine(command.Message);
      }
  }
  
  // message command handler decorator that logs about messages being sent
  public class MessageLogger : ICommandHandler&lt;MessageCommand&gt;
  {
      private ICommandHandler&lt;MessageCommand&gt; innerHandler;
      private ICommandHandler&lt;LogCommand&gt; logger;
  
      // notice these dependencies are two distinct closed generic types
      public MessageLogger(ICommandHandler&lt;MessageCommand&gt; innerHandler,
          ICommandHandler&lt;LogCommand&gt; logger)
      {
          this.innerHandler = innerHandler;
          this.logger = logger;
      }
  
      public void Execute(MessageCommand command)
      {
          innerHandler.Execute(command);
  
          var logCommand = new LogCommand
              {
                  LogMessage = command.Message,
                  Time = DateTime.Now
              };
          logger.Execute(logCommand);
      }
  }
  
  // this works as intended, but is tedious in a real-world app
  ICommandHandler&lt;MessageCommand&gt; ResolveManually()
  {
      ICommandHandler&lt;MessageCommand&gt; sender = new MessageSender();
      ICommandHandler&lt;LogCommand&gt; logger = new Logger();
  
      ICommandHandler&lt;MessageCommand&gt; loggerSender =
          new MessageLogger(sender, logger);
  
      return loggerSender;
  }
  
  // this is what I want to work - seems simple?
  ICommandHandler&lt;MessageCommand&gt; ResolveWithSimpleInjector()
  {
      var container = new Container();
  
      container.Register&lt;ICommandHandler&lt;LogCommand&gt;, Logger&gt;();
  
      container.Register&lt;ICommandHandler&lt;MessageCommand&gt;, MessageSender&gt;();
  
      // this next line throws the exception
      container.RegisterDecorator(typeof(ICommandHandler&lt;MessageCommand&gt;),
          typeof(MessageLogger));
  
      return container.GetInstance&lt;ICommandHandler&lt;MessageCommand&gt;&gt;();
  }
  
  void Main()
  {
      //ICommandHandler&lt;MessageCommand&gt; sender = ResolveManually();
      ICommandHandler&lt;MessageCommand&gt; sender = ResolveWithSimpleInjector();
  
      var command = new MessageCommand { Message = "Hello World!" };
      sender.Execute(command);
  }
  </code></pre>
  
  <p>I could not find any information about this situation. Is this a bug, or am I missing something?</p>
  
  <p><strong>Edit</strong></p>
  
  <p>I am fishing for feedback from a dev on SimpleInjector to find out if there is a technical reason for this limitation, or it is something that was overlooked... Unless someone can convince me there is a logical flaw in this design and there is a good reason I shouldn't do things this way, which none of the answers thus far have been able to do. I do appreciate the feedback.</p>
  
  <p>It seems to me that the core issue is that <code>RegisterDecorator()</code> is treating two different closed generic types as the same type. There may be a technical reason for this based on its inner workings, but maybe not?</p>