title: How to use Activator to create an instance of a generic Type and casting it back to that type?
body: <p>I have a generic type <code>Store&lt;T&gt;</code> and use <code>Activator</code> to make an instance of this type. Now how, after using the Activator, can I cast the resulted object of type <code>object</code> back to the instantiated type? I know the type that I used to instantiate the generic. Please see the following code:</p>
  
  <pre><code>class Store&lt;T&gt; where T : IStorable 
  {}
  
  class Beer : IStorable 
  {}
  
  class BeerStore : Store&lt;Beer&gt;
  {}
  
  Type storeType = someObjectThatImplementsIStorable.GetType();
  Type classType = typeof(Store&lt;&gt;);
  Type[] typeParams = new Type[] { storeType };   
  Type constructedType = classType.MakeGenericType(typeParams);
  
  object x = Activator.CreateInstance(constructedType, new object[] { someParameter });
  </code></pre>
  
  <p>What I would like to do is something like this:</p>
  
  <pre><code>var store = (Store&lt;typeof(objectThatImplementsIStorable)&gt;)x;
  </code></pre>
  
  <p>but that doesn't work for obvious reasons. As an alternative I tried:</p>
  
  <pre><code>var store = (Store&lt;IStorable&gt;)x;
  </code></pre>
  
  <p>which could possibly work in my opinion, but gives an <code>InvalidCastException</code>.</p>
  
  <p>How do I get access again to the <code>Store&lt;T&gt;</code> methods that I know are in the object <code>x</code>?</p>