title: Generic factory that creates generic classes
body: <p>I have a set of value type converters that convert strings into their respective types. I have a factory that is responsible for creating these converters based on the type, when needed. I'm trying to keep the factory and converters generic, but I'm running into some issues. I don't know the type until I call the .Create method on the factory, so I need to be able to pass in the type as an argument. The trouble is that, then, my .Create method thinks I'm looking for a <code>ValueConverter&lt;Type&gt;</code> instead of a more appropriate value converter like <code>ValueConverter&lt;int&gt;</code>. I'm missing something, or perhaps even doing it completely wrong. </p>
  
  <p>Here are a couple of my converters and the interface:</p>
  
  <pre><code>public interface IValueConverter&lt;T&gt;
  {
      T Convert(object objectToConvert);
  }
  
  public class IntValueConverter : IValueConverter&lt;int&gt;
  {
      public int Convert(object objectToConvert)
      {
          return System.Convert.ToInt32(objectToConvert);
      }
  }
  
  public class DateTimeValueConverter : IValueConverter&lt;DateTime&gt;
  {
      public DateTime Convert(object objectToConvert)
      {
          return System.Convert.ToDateTime(objectToConvert);
      }
  }
  </code></pre>
  
  <p>Then, I have a factory like this:</p>
  
  <pre><code>public class ValueConverterFactory : IValueConverterFactory
  {
      private readonly IUnityContainer _container;
  
      public ValueConverterFactory(IUnityContainer container)
      {
          _container = container;
      }
  
      public IValueConverter&lt;T&gt; Create&lt;T&gt;(T type)
      {
          return _container.Resolve&lt;IValueConverter&lt;T&gt;&gt;();
      }
  }
  </code></pre>
  
  <p>And unity is configured something like this:</p>
  
  <pre><code>Container.RegisterType&lt;IValueConverter&lt;int&gt;, IntValueConverter&gt;();
  Container.RegisterType&lt;IValueConverter&lt;DateTime&gt;, DateTimeValueConverter&gt;();
  </code></pre>
  
  <p>I need to be able to call the factory like this:</p>
  
  <pre><code>var objectType = someObj.GetType();
  var valueConverter = _valueConverterFactory.Create(objectType);
  </code></pre>