title: Downcast instance using its type
body: <p>I have a method which uses a generic parameter and requires the downcasted form of the instance. For example,</p>
  
  <pre><code>public abstract class Animal {
  }
  public class Dog : Animal {
  }
  public class Cat : Animal {
  }
  public class AnimalHandler {
      public virtual void Pet&lt;T&gt;(T animal)
      {
      }
  }
  </code></pre>
  
  <p>Given a collection of animals.</p>
  
  <pre><code>public List&lt;Animal&gt; Animals { get; set; }
  </code></pre>
  
  <p>How do I downcast individual animals before calling the <strong>Pet</strong> method?</p>
  
  <p>I currently having working in this form.</p>
  
  <pre><code>if (animal is Dog) {
      _animalHandler.Pet((Dog)animal);
  }
  if (animal is Cat) {
      _animalHandler.Pet((Cat)animal);
  }
  </code></pre>
  
  <p>Ideally, it would be something of this form.</p>
  
  <pre><code>var type = animal.GetType();
  _animalHandler.Pet(animal.CastTo(type));
  </code></pre>
  
  <p><strong>Clarification:</strong> I need the instance to be of the downcasted type before it is passed to the method. I need a Dog or Cat specifically to be passed in.</p>