title: How do I cast an object to a dynamic type?
body: <p>All of my objects are composed from a base abstract class called <code>GameObject</code>.  All of these objects inherit from one or more interfaces, such as <code>IRenderable</code>, <code>IAnimatable</code>, <code>IMovable</code>, etc.</p>
  
  <p>I also have a list of these objects stored as <code>List&lt;GameObject&gt;</code>.  I'm trying to write a dynamic method that will return to me all of the objects that has inherited from a specific interface, like so:</p>
  
  <pre><code>List&lt;IRenderable&gt; renderableObjects = ObjectManager.GetAll&lt;IRenderable&gt;();
  </code></pre>
  
  <p>At first, I tried just returning a new list of game objects where each object's type matches the dynamic type, then casting the <code>List&lt;GameObject&gt;</code> to <code>List&lt;T&gt;</code>.  The compiler error I received said that I could not convert those types.</p>
  
  <p>So my second attempt involved casting each object individually, as shown below.</p>
  
  <pre><code>public List&lt;T&gt; GetAll&lt;T&gt;() {
      List&lt;T&gt; list = new List&lt;T&gt;();
  
      foreach( GameObject obj in this.gameObjects )
          if( obj is T )
              list.Add( (T)obj );
  
       return list;
  }
  </code></pre>
  
  <p>However, this throws the same type of error; cannot convert type GameObject to T.  Am I missing something?  Is there another way to accomplish what I want?</p>