title: C#: Using reflection to get access to all fields within a class, returns error
body: <p>can anyone help me?</p>
  
  <p>I have some reflection code that i have written and it seems to work well but it gives me an error when trying to pass in "this" to the GetValue.</p>
  
  <p>I am a little stuck, i think the problem is that i am running the reflection code in frmMain adn the AbCCompany is defined in another project but i have a reference.</p>
  
  <p>I get the error </p>
  
  <blockquote>
    <p>Field 'AbcCompany' defined on type 'MyApp.Companies.Config' is not a
    field on the target object which is of type 'MyApp.frmMain'.</p>
  </blockquote>
  
  <p>Here is the code..</p>
  
  <pre><code>        var companies = MyIems.Companies.GetType().GetFields();
  
  
          foreach (var list in companies )
          {
  
              List&lt;CompanyBase&gt; thisCompanyCollection = (List&lt;CompanyBase&gt;)list.GetValue(this);
              foreach (var company in thisCompanyCollection )
              {
                  Console.WriteLine();
              }
          }
  </code></pre>
  
  <p><strong>EDIT</strong></p>
  
  <p>I forgot to mention that Inside "Companies" is lots of List where xxx is a class.. all classes inherit from CompanyBase.  i.e AbcCompany</p>