title: How can I recursively search properties in C# only if those properties inherit from some base class?
body: <p>How can I recursively get all the properties of an object only if the type of the property inherits from some base class?</p>
  
  <p>This was my attempt:</p>
  
  <pre><code>static IEnumerable&lt;PropertyInfo&gt; FindProperties(object objectTree, Type targetType)
  {
      if (objectTree.GetType().IsAssignableFrom(targetType))
      {
          var properties = objectTree.GetType().GetProperties();
          foreach (var property in properties)
          {
              yield return property;
          }
  
          foreach (var property in FindProperties(properties, targetType))
          {
              yield return property;
          }
      }
  }
  </code></pre>
  
  <p>So I could call,</p>
  
  <pre><code>var allPropertiesOfPageTypes = FindProperties(someClass, typeof(Page));
  </code></pre>
  
  <p>However, the number of properties returned is always zero.  What am I doing wrong?</p>
  
  <p>Edit:</p>
  
  <p>I'm not sure if this matters but the subclasses are generic classes:</p>
  
  <pre><code>public abstract class MasterPage&lt;T&gt; : BasePage&lt;T&gt; where T : MasterPage&lt;T&gt;
  </code></pre>
  
  <p>That inherits:</p>
  
  <pre><code>public abstract class BasePage&lt;T&gt; : Page where T : BasePage&lt;T&gt;
  </code></pre>
  
  <p>Things that inherit from Master/BasePage seem to be returning false for <code>IsAssignableFrom</code>?</p>