title: Create Generic Type with Generic Interface at Run Time
body: <p>I've been working through an issue for a couple of hours now, and I think I'm close. I'm working on an app where we could have 50-100 types that perform the same way. So instead of creating 50-100 classes, I tried to make it generic and this is what I have:</p>
  
  <p>This is the base class:</p>
  
  <pre><code>public class RavenWriterBase&lt;T&gt; : IRavenWriter&lt;T&gt; where T : class, IDataEntity
  </code></pre>
  
  <p>And this is the interface:</p>
  
  <pre><code>public interface IRavenWriter&lt;T&gt;
  {
      int ExecutionIntervalInSeconds { get; }
      void Execute(object stateInfo);
      void Initialize(int executionIntervalInSeconds, Expression&lt;Func&lt;T, DateTime&gt;&gt; timeOrderByFunc);
  }
  </code></pre>
  
  <p>And this is how I'm using it:</p>
  
  <pre><code>private static void StartWriters()
  {
      Assembly assembly = typeof(IDataEntity).Assembly;
      List&lt;IDataEntity&gt; dataEntities = ReflectionUtility.GetObjectsForAnInterface&lt;IDataEntity&gt;(assembly);
  
      foreach (IDataEntity dataEntity in dataEntities)
      {
          Type dataEntityType = dataEntity.GetType();
          Type ravenWriterType = typeof(RavenWriterBase&lt;&gt;).MakeGenericType(dataEntityType);
  
          Expression&lt;Func&lt;IDataEntity, DateTime&gt;&gt; func = x =&gt; x.CicReadTime;
  
          // This is where I'm stuck. How do I activate this as RavenWriterBase&lt;T&gt;?
          var ravenWriter = Activator.CreateInstance(ravenWriterType);
  
          //ravenWriter.Initialize(60, func);  // I can't do this until I cast.
  
          // More functionality here (not part of this issue)
      }
  }
  </code></pre>
  
  <p>I'm stuck on this line from above:</p>
  
  <pre><code>var ravenWriter = Activator.CreateInstance(ravenWriterType);
  </code></pre>
  
  <p><b>This is my question:</b><br>
  <b>How can I use that as RavenWriterBase or IRavenWriter? Something like:</b></p>
  
  <pre><code>ravenWriter.Initialize(60, func);
  </code></pre>
  
  <p>I think it needs to be something like this, but I need to specify a type for IRavenWriter&lt;> and I don't know it yet:</p>
  
  <pre><code>var ravenWriter = Activator.CreateInstance(ravenWriterType) as IRavenWriter&lt;&gt;;
  </code></pre>
  
  <p>If I hover over ravenWriter, I successfully have my object:</p>
  
  <p><img src="https://i.stack.imgur.com/ochg9.png" alt="enter image description here"></p>
  
  <p>But now I need to be able to use it in a generic way. How can I do that?</p>
  
  <p><strong>Update:</strong></p>
  
  <p>I just thought of using the dynamic keyword, and this works:</p>
  
  <pre><code>dynamic ravenWriter = Activator.CreateInstance(ravenWriterType);
  ravenWriter.Initialize(60);
  </code></pre>
  
  <p>I cheated a bit because I realized that the Func was the same for each IDataEntity, so that wasn't necessary to pass as a parameter to Initialize(). However, at least now I <em>can</em> call Initialize(). But now that the Func is the same, I shouldn't need the generic interface either.</p>