title: Late-binding collection to overloaded method
body: <p><strong>Background</strong></p>
  
  <p>I have code that receives some deserialized data of type <em>IProduct</em> but the implementing class is unknown at runtime. I'm supposed to pass this data to an overloaded method that accepts an instance of the implementing class. </p>
  
  <p>This can be achieved by using a switch statement, but it will look clunky and would require alot of code. I've solved this by using late-binding and runtime compilation for single entities but I'm unable to figure out how to do this for collections. </p>
  
  <p><strong>Solving the problem for single entities</strong></p>
  
  <p>If I have an object of type IProduct and want to pass this to the correct overloaded method that accepts an implementing class like Toy, Food, Car or something else. How do I achieve this without creating a big switch statement?</p>
  
  <p>Given the following methods and interface:</p>
  
  <pre><code>public interface IProduct
  {
       string Name { get;}
       double Price { get;}
  }
  
  public void Store(Apple apple);
  public void Store(Toy toy);
  public void Store(Car car);
  </code></pre>
  
  <p>With the switch statement it would look something like this:</p>
  
  <pre><code>IProduct product = new Apple();
  
  switch (product)
  {
      case Apple apple:
          Store(apple);
          break;
      case Toy toy:
          Store(toy);
          break;
      case Car car:
          Store(car);
          break;
  }
  </code></pre>
  
  <p>I've managed to reduce this to the following using late-binding and runtime compilation.</p>
  
  <pre><code>IProduct product = new Apple();
  
  Store((dynamic)product);
  </code></pre>
  
  <p><strong>Question</strong></p>
  
  <p>However, I've not been able to figure out how to do late-binding in a similar fashion when it comes to collections. </p>
  
  <p>Given the following methods:</p>
  
  <pre><code>public static void Store(ICollection&lt;Apple&gt; apples);
  public static void Store(ICollection&lt;Toy&gt; toys);
  public static void Store(ICollection&lt;Car&gt; cars);
  </code></pre>
  
  <p>How do I pass this:</p>
  
  <pre><code>ICollection&lt;IProduct&gt; products = new List&lt;IProduct&gt;() { new Apple()};
  </code></pre>
  
  <p>Is there a way to achieve the same thing without using alot of magical code?</p>