title: c# - Return a generic interface dynamically
body: <p>I'm trying to return one of two repository instances based on a flag. My Repository Interface and implementation look like so:</p>
  
  <pre><code>public interface IRepository&lt;T&gt;
  {
      T Insert(T entity);
      void Delete(T entity);
      IEnumerable&lt;T&gt; SearchFor(Expression&lt;Func&lt;T, bool&gt;&gt; predicate);
      IEnumerable&lt;T&gt; GetAll();
      T SearchForFirst(Expression&lt;Func&lt;T, bool&gt;&gt; predicate);
      bool Has(Expression&lt;Func&lt;T, bool&gt;&gt; predicate);
  }
  
  class Repository&lt;T&gt; : IRepository&lt;T&gt; where T : class    { ... implementation here ...}
  </code></pre>
  
  <p>And the method that is returning the one of two instances of a repository should look something like:</p>
  
  <pre><code>    public IRepository&lt;T&gt; GenerateRepository(bool flag)
      {
          if ( flag ) {
             return new Repository&lt;MyFirstType&gt;();
          } else {
             return new Repository&lt;MySecondType&gt;();
          }
      }
  </code></pre>
  
  <p>Could anyone point me in the right direction as to how this should actually be written?</p>
  
  <p>Thanks</p>