title: C# use string as a class field name
body: <p>Sorry if the tile is misleading. What i would like to do is to use a string to get the values from a class. What i have:</p>
  
  <pre><code>class foo
  {
      public string field1 {get;set;}
      public string field2 {get;set;}
  }
  
  public void run()
  {
      //Get all fields in class
      List&lt;string&gt; AllRecordFields = new List&lt;string&gt;();
      Type t = typeof(foo);
      foreach (MemberInfo m in t.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic))
      {
          AllRecordFields.Add(m.Name);
      }
  
      foo f = new foo();
      foreach(var field in AllRecordFields)
      { 
          //field is a string with the name of the real field in class
          f.field = "foobar";
      }
  }
  </code></pre>
  
  <p>This a really simple example, so the problem is on the line <code>f.field = "foobar";</code>
  The <code>field</code> is a string with a name of the real class field what i want to assignt the value to. </p>