title: How to dynamically get a property by name from a C# ExpandoObject?
body: <p>I have an <code>ExpandoObject</code> and want to make a getter for it that will return a property by name at runtime, where the name is specified in a string instead of hardcoded.</p>
  
  <p>For example, I CAN do this:</p>
  
  <pre><code>account.features.isEmailEnabled;
  </code></pre>
  
  <p>and that will return true.  <code>account</code> is a <code>ExpandoObject</code>, and <code>features</code> is also an <code>ExpandoObject</code>.  So I have an <code>ExpandoObject</code> that contains other <code>ExpandoObjects</code>.</p>
  
  <p>So what I want to be able to do is this:</p>
  
  <pre><code>account.features.GetProperty("isEmailEnabled");
  </code></pre>
  
  <p>and have that return true.</p>
  
  <p>The reason is that I have many features, and I want to be able to write one generic getter method where I can pass in the name of the feature I want, and the method will pass me back the value for account.features.whatever (where "whatever" is specified by passing in a string to the generic getter method).  Otherwise I am going to have to write 30-some getters one for each feature.</p>
  
  <p>I did a lot of research and tried doing something like:</p>
  
  <pre><code>var prop = account.features.GetType();  
  // this returns System.Dyanmic.ExpandoObject
  </code></pre>
  
  <p>followed by</p>
  
  <pre><code>var value = prop.GetProperty(featureNameAsString); 
  </code></pre>
  
  <p>but <code>value</code> always comes back as null. I don't understand why. In the watch window I can do <code>account.features.isEmailEnabled</code> and it shows true and says its a boolean.  But if I try to get at this value using the approach above and pass in <code>isEmailEnabled</code> as the <code>featureNameAsString</code> I just get null.</p>
  
  <p>Can someone please tell me what I may be doing wrong and what's a good approach, without it being too complex?</p>
  
  <p>I am working with ASP.NET under the 4.5.1 framework.</p>