title: Unit: Resolve generic service using object type at runtime
body: <p>What i'm trying to accomplish is to have a single wcf service that has (for example) an Insert(ICommand command) method and can resolve a generic service that knows how to insert the underling object type.</p>
  
  <p>Where's my (poor) attempt on a sandbox project.
  Besides other flaws, this services allow to attempt to insert any ICommand.
  I'm relative new Unit user and i was wondering if someone has a better way to accomplish this.</p>
  
  <pre><code>class Program
      {
          static void Main(string[] args)
          {
              var cmd = new SingleCommand();
              var cmd2 = new DoubleCommand();
  
              var s = new MyWcfService();
              s.Insert(cmd);
              s.Insert(cmd2);
              Console.ReadKey();
          }
      }
  
      public interface ICommand { }
      public interface ICommandService
      {
          void Insert(ICommand command);
      }
  
      public interface ICommandService&lt;in T&gt; : ICommandService where T : ICommand
      {
          void Insert(T command);
      }
  
      public class SingleCommand : ICommand { }
      public class DoubleCommand : ICommand { }
  
      public class SingleCommandService : ICommandService&lt;SingleCommand&gt;
      {
          public void Insert(SingleCommand singleCommand)
          {
              Console.WriteLine("Inserted a SingleCommand");
          }
  
          void ICommandService.Insert(ICommand command)
          {
              Insert((SingleCommand)command);
          }
      }
  
      public class DoubleCommandService : ICommandService&lt;DoubleCommand&gt;
      {
          public void Insert(DoubleCommand doubleCommand)
          {
              Console.WriteLine("Inserted a DoubleCommand");
          }
  
          void ICommandService.Insert(ICommand command)
          {
              Insert((DoubleCommand)command);
          }
      }
  
      public static class Config
      {
          public static UnityContainer Container = new UnityContainer();
          static Config()
          {
              Container.RegisterType&lt;ICommandService, SingleCommandService&gt;(typeof(SingleCommand).Name);
              Container.RegisterType&lt;ICommandService, DoubleCommandService&gt;(typeof(DoubleCommand).Name);
          }
      }
  
      public class MyWcfService
      {
          public void Insert(ICommand command)
          {
              var foo = Config.Container.Resolve&lt;ICommandService&gt;(command.GetType().Name);
              foo.Insert(command);
          }
      }
  </code></pre>