title: Calling a generic method with the correct derived type
body: <p>I have the following scenario:</p>
  
  <p>I have three classes, let's call them <code>A</code>, <code>B</code> and <code>C</code>. All they have in common is that they inherit from the same interface, <code>ISomeInterface</code> and that they are classes that are mapped to entities using Entity Framework.</p>
  
  <p>I have a method that received a List of objects that implements this interface, but the objects themselves will be instances of <code>A</code>, <code>B</code> or <code>C</code>.</p>
  
  <p>The method shell looks like this</p>
  
  <pre><code>public void MyMethod(List&lt;ISomeInterface&gt; entityList)
  {
    foreach(var entity in entityList)
    {
      ProcessEntity(entity);
    }
  }
  </code></pre>
  
  <p>Now, the problem is with the <code>ProcessEntity</code> method. This is a generic method, that needs to retrieve the table of matching elements from the database according to the type or entity, so it looks like this:</p>
  
  <pre><code>public void ProcessEntity&lt;T&gt;(T entity)
  {
    using( var repository = new DbRepository())
    {
      var set = repository.Set&lt;T&gt;();
      ...
    }
  }
  </code></pre>
  
  <p>The problem is that the line <code>var set = repository.Set&lt;T&gt;();</code> fails because <code>T</code> is <code>ISomeInterface</code> in this case, and not the actual type( <code>A</code>, <code>B</code> or <code>C</code>), so it gives an exception that is can't relate to the type given, which is understandable.</p>
  
  <p>So my question is: How can i call ProcessEntity with the actual type of the object inside the list, and not the interfacetype that they implements.</p>