title: Getting specific properties from an object child
body: <p>Struggled to come up with a decent way to ask/title this question, but will try and illustrate it as best I can.</p>
  
  <p>I am working with a data structure something like this:</p>
  
  <pre><code>public Foo
  {
       public Bar Bar {get;set;}
  }
  
  public Bar
  {
      public SubTypeA TypeA {get;set;}
      public SubTypeB TypeB {get;set;}
      ...
  }
  
  public SubTypeA
  {
      public int Status {get;set;}
      ...
  }
  </code></pre>
  
  <p>Note that I am unable to change the data structure for this.</p>
  
  <p>There are many different types in the <code>Bar</code> class, which all have different properties within them, but common to all of them is the property of <code>Status</code>.  </p>
  
  <p>What I need to do, is given an object of type <code>Foo</code>, is record the statuses for every item in the <code>Bar</code> object within it.  Not every <code>SubType</code> is going to have a value every time though, some could be null.</p>
  
  <p>I can sort of manage it by using a recursive function like below to loop through all the properties.  It isn't ideal though I don't think as the loop could get quite large as there could be a lot of properties on each <code>SubType</code>.  </p>
  
  <pre><code>private void GetProperties(Type classType, object instance)
  {
      foreach (PropertyInfo property in classType.GetProperties())
      {
  
          object value = property.GetValue(instance, null);
          if (value != null) 
          {
              if (property.Name == "Status")
              {
                  Record(classType, value);
              }
              GetProperties(property.PropertyType, value);
          }
      }
  }
  </code></pre>
  
  <p>Is this about the only approach that there is for such a problem?  </p>
  
  <p>EDIT: Going by the answer given by Selman22, I have come up with another issue wherein I am trying to create an anonymous object based on the status and name of object.</p>
  
  <pre><code>var z = instance.GetType()
              .GetProperties()
              .Select(x =&gt; new 
                  { 
                      status = x.GetValue(instance).GetType().GetProperty("status").GetValue(x, null), 
                      name = x.Name 
                  })
              .ToList();
  </code></pre>
  
  <p>This is throwing an error of <code>Object does not match target type.</code> when trying to retrieve the value.  Is this possible in a 1 liner?</p>