title: Getting the value of a property of a class in a class
body: <p>Ok so I have been up and down the internet looking for a solution to this question.  I think my title is maybe no to informative so some background.</p>
  
  <p>I have the following classes:</p>
  
  <pre><code>public class foo { public string Name { get; set; } }
  public class foo1 { public string Name { get; set; } }
  public class foo2 { public string Name { get; set; } }
  public class foo3 { public string Name { get; set; } }
  public class foo4 { public string Name { get; set; } }
  public class foo5 { public string Name { get; set; } }
  
  public class goo 
  { 
     public string Desc { get; set; }
     public foo f { get; set; }
     public foo1 f1 { get; set; }
     public foo2 f2 { get; set; }
     public foo3 f3 { get; set; }
     public foo4 f4 { get; set; }
  }
  </code></pre>
  
  <p>So now my question,  Using Reflection, how can I get to the value of foo.Name when only having a reference to goo.</p>
  
  <p>The normal Reflection code is:</p>
  
  <pre><code>goo g = new goo();
  PropertyInfo pInfo = g.GetType().GetProperty("Name");
  string Name = (string)pInfo.GetValue(g, null);
  </code></pre>
  
  <p>So the above code is how you get a property from the goo class.  But now how do you get the value of foo.Desc?</p>
  
  <p>I tried the following which doesn't work:</p>
  
  <pre><code>goo g = new goo();
  PropertyInfo pInfo = g.GetType().GetProperty("f");
  PropertyInfo pInfo2 = pInfo.PropertyType.GetProperty("Desc");
  string Name = (string)pInfo2.GetValue(pInfo.PropertyType, null);
  </code></pre>
  
  <p>Unfortunately I get a Mismatched object error which I can understand because I am trying to use the property type and not the actual instance of the foo class.  I also tried to fins a way to instantiate an object from the propertyinfo but if there is a way then it eludes me.  I could do something like this:</p>
  
  <pre><code>goo g = new goo();
  PropertyInfo propInfo = g.GetType().GetProperty("f");
  object tmp;
  
  propInfo.SetValue(g, Convert.ChangeType(new foo(), propInfo.PropertyType), null);
  tmp = g.f;
  </code></pre>
  
  <p>This works but besides having to hard code the class, that is creating a new instance and therefore of now use to me.</p>
  
  <p>As I say I have been up and down looking for a solution.  Everything I have found is basically variants on the "get a value of a property of a class" theme but nothing about going another level deeper.</p>
  
  <p>Can anyone help?  Is this even possible because I would really like to stay away from hard coding.</p>
  
  <p>EDIT: I have edited the class to more accurately represent what I am working with.  Based on the comments below, I am getting the names of the foo instances from a database and that is why I am using Reflection or want to use Reflection instead of hard coding 30+ switch statements.</p>
  
  <p>EDIT: Also I don't know before runtime which foo classes will be populated with data.  Also each foo class is different.  Unlike my example where each foo class has a string property, in my project each class has a different design which mirrors the database.</p>
  
  <p>EDIT:  So Ulugbek Umirov gave the answer.  I just didn't see it immediately.  Below my implementation so as to maybe help others in the future.</p>
  
  <pre><code>foreach (PropertyInfo pInfo in _standard.GetType().GetProperties())
  {
      if (_fullDataModel.ClassDefinitions.Contains(pInfo.Name))
      {
          PropertyInfo _std_pinfo = _standard.GetType().GetProperty(pInfo.Name);
          object g = _std_pinfo.GetValue(_standard, null);
  
          PropertyInfo props = g.GetType().GetProperty("showMe");
          bool showMe = (bool)props.GetValue(g, null);
  
          if (showMe)
          {
              string tblName = _fullDataModel.ClassDefinitions[pInfo.Name].                   PropertyGroupDefinitions.Where(p =&gt; p.TransactionsTable != true).First().Token;
  
              //  Use tblName to build up a dataset
          }
      }
  }
  </code></pre>
  
  <p>This does exactly what I wanted.
  Thank you.</p>