title: c# using reflection to access members of a nested structure
body: <p>I am using C# reflection to implement a scripting language that allows the user to have access to internal program variables.</p>
  <p>It works for simple constructs, but when I try to access data in instantiated structures, it always gives 0 as the data value.
  My test program:</p>
  <pre><code>using System;
  using System.Reflection;
  
  namespace ConsoleApp5
  {
      public class TST         // Test class
      {
          public struct S1_t
          {
              public UInt32 one;
              public UInt32 two;
          }
          public S1_t t1 = new S1_t();
          public UInt32 x;
          public TST()
          {
              t1.one = 6977;
              t1.two = 2;
              x = 7769;
          }
      }
  
      public class NFC
      {
          public TST tst = new TST();
      }
  
      public class PRD
      {
          public NFC nfc = new NFC();
      }
  
      class Program
      {
  
          private static bool GetFullVarString(Type root, string[] varNamePath, out string valStr, int index = 0)
          {
              Object targetObj;
              bool status = false;
              if (index &gt;= varNamePath.Length)    // Guard
              {
                  valStr = &quot;&quot;;
                  return false;
              }
  
              targetObj = Activator.CreateInstance(root);
              
              string varValStr = &quot;&quot;;
              try
              {
                  foreach (MemberInfo member in root.GetMembers())    // BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static | BindingFlags.NonPublic))
                  {
                      if (member.Name != varNamePath[index])
                          continue;
  
                      /* Have we found the variable we're looking for?    */
                      if (member.MemberType == MemberTypes.Field)
                      {
                          FieldInfo fieldInfo = ((FieldInfo)member);
                          if (fieldInfo.GetValue(targetObj) != null)
                          {
                              var miVal = fieldInfo.GetValue(targetObj);
                              Console.WriteLine(String.Format(&quot; Field Type: {0}&quot;, fieldInfo.FieldType.ToString()));
                              Type thisType = Type.GetType(fieldInfo.FieldType.FullName.ToString());
                              Boolean isStruct = thisType.IsValueType &amp;&amp; !thisType.IsEnum &amp;&amp; 
                                  !(fieldInfo.FieldType.ToString().StartsWith(&quot;System&quot;));
                              Console.WriteLine(String.Format(&quot; IsStruct: {0}&quot;, isStruct.ToString()));
  
                              if (fieldInfo.FieldType.IsClass)
                              {
                                  /* We've found a child class, so we need to change
                                   * the root type and step into it.
                                   */
                                  Type newType = Type.GetType(fieldInfo.FieldType.FullName.ToString());
                                  status = GetFullVarString(newType, varNamePath, out varValStr, index + 1);
                              }
  
                              else if (isStruct)
                              {
                                  /* We've found a child struct, so we need to change
                                   * the root type and step into it.
                                   */
                                  Type newType = Type.GetType(fieldInfo.FieldType.FullName.ToString());
                                  status = GetFullVarString(newType, varNamePath, out varValStr, index + 1);
                              }
                              else
                              {
                                  varValStr = miVal.ToString();
                              }
                              status = true;
                              break;
                          }
                      }
                      else if (member.MemberType == MemberTypes.NestedType)
                      {
                          Type nestedType = (Type)member;     // MemberInfo that is a NestedType is just a Type itself
                          status = GetFullVarString(nestedType, varNamePath, out varValStr, index + 1);
                      }
                  }
              }
              catch (Exception ex)
              {
                  Console.WriteLine(String.Format(&quot;Exception: {0}&quot;, ex.Message));
                  status = false;
              }
  
              valStr = varValStr;
              return status;
          }
  
          static void Main(string[] args)
          {
              PRD prd = new PRD();
              string fullVar, tstr;
  
              Type root = typeof(PRD);
  
              fullVar = &quot;nfc.tst.x&quot;;
              GetFullVarString(root, fullVar.Split('.'), out tstr);
              Console.WriteLine(String.Format(&quot;{0} = {1}&quot;, fullVar, tstr));
  
              fullVar = &quot;nfc.tst.t1.one&quot;;
              GetFullVarString(root, fullVar.Split('.'), out tstr);
              Console.WriteLine(String.Format(&quot;{0} = {1}&quot;, fullVar, tstr));
  
              Console.ReadKey();
          }
      }
  }
  </code></pre>
  <p>The output shows that the variable 'x' at the base of the TST class displays fine, but the member of the t1 structure does not.</p>
  <pre><code> Field Type: ConsoleApp5.NFC
   IsStruct: False
   Field Type: ConsoleApp5.TST
   IsStruct: False
   Field Type: System.UInt32
   IsStruct: False
  nfc.tst.x = 7769
   Field Type: ConsoleApp5.NFC
   IsStruct: False
   Field Type: ConsoleApp5.TST
   IsStruct: False
   Field Type: ConsoleApp5.TST+S1_t
   IsStruct: True
   Field Type: System.UInt32
   IsStruct: False
  nfc.tst.t1.one = 0
  </code></pre>
  <p>I'm thinking that maybe it has to do with using the Activator class on the non-class structure, but I have no idea.</p>
  <p>Any help??</p>