title: Check if generic object of subtype T
body: <p>I'm trying to refactor the below into a generic function; the code sample below works for me if type is a specified in the function declaration. However, it fails when I try to use T.</p>
  
  <p>I get an IENumerable-like object containing generic objects back from an external application, and from this want to filter out those that are of the specific type in question.</p>
  
  <p>For context, these are geometric features selected on screen by the user prior to the code running. I need to validate that the correct type of thing has been picked, and return those things in a clean list.</p>
  
  <p>Initial code with defined type that works:</p>
  
  <pre><code>    public static List&lt;Point&gt; GetSelectedPoints()
      {
          List&lt;Point&gt; tmp = new List&lt;Point&gt;();
  
          Selection oSel = GetSelectionObject();
  
          for (int i = 1; i &lt;= oSel.Count; i++)
          {
              try
              {
                  if (oSel.Item(i).Value is Point)
                  {
                      Point P = (Point)oSel.Item(i).Value;
                      tmp.Add(P);
                  }
              }
              catch
              {
                  throw new Exception("An error occurred whilst retrieving the selection");
              }
          }
  
          return tmp;
      }
  </code></pre>
  
  <p>Here the attempt to use T:</p>
  
  <pre><code>    static public List&lt;T&gt; GetThisTypeFromSelection&lt;T&gt;()
      {
  
          Selection osel = GetSelectionObject();
          List&lt;T&gt; tmp= new List&lt;T&gt;();
          for(int i = 1; i&lt;=osel.Count ; i++)
          {
              if (osel.Item(i).Value is T)
              {
                  T thing = (T)osel.Item(i).Value;
                  tmp.Add(tmp);
              }
          }
          return tmp;
      }
  </code></pre>
  
  <p>osel.Item(i).Value.GetType() returns a "System.__ComObject"... Which is not helpful.</p>
  
  <p>The object model of the external application is such that everything is derived from a single base class, with many layers of subclassing, something like this:</p>
  
  <pre><code>    public class Base
  {}
  
  public class Geometry2d : Base
  { }
  public class Line : Geometry2d
  { }
  public class Circle : Line
  { }
  public class Face : Geometry2d
  { }
  public class PlanarFace : Face
  { }
  public class CylindricalFace : Face
  { }
  public class PlanarFaceDefinedThroughX : PlanarFace
  { }
  public class PlanarFaceDefinedThroughY : PlanarFace
  { }
  etcetera...
  </code></pre>
  
  <p>So, The selection object (while also deriving from base) returns a list of base object which could be...pretty much anything.</p>
  
  <p>Depending on the application for this function, I might want to get, for example, everything that's a "Face" or derivative, or maybe just the PlanarFaces, or maybe even just the PlanarFaceDefinedThroughXs.</p>
  
  <p><a href="https://i.stack.imgur.com/rxtOB.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/rxtOB.png" alt="The selection object looks like this:"></a></p>
  
  <hr>
  
  <p>Update based on comments (kudos to mm8 pointing in the right direction)</p>
  
  <pre><code>    static public List&lt;T&gt; GetThisTypeFromSelection&lt;T&gt;()
      {
  
          Selection osel = GetSelectionObject();
          List&lt;Base&gt; listA = new List&lt;Base&gt;();
          for(int i = 1; i&lt;=osel.Count ; i++)
          {
              CATBaseDispatch CbD = osel.Item(i).Value;
              listA.Add(CbD);
          }
          List&lt;T&gt; results = listA.Select(x =&gt; x).OfType&lt;T&gt;().ToList();
  
          return results;
      }
  </code></pre>
  
  <p>This approach seems to successfully filter out the right object types - but the returned list still shows them as COM objects...</p>
  
  <p><a href="https://i.stack.imgur.com/KpCLf.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/KpCLf.png" alt="enter image description here"></a></p>