title: C# Is there a way to obtain a reference for a field from a class and use the reference to call methods?
body: <p>C# Question .Net version 4.6.1</p>
  
  <p>I have a class as shown below and in a function I create an instance of the class.  I would like to create a reference to field(s) from the class instance and call the method "Insert" on the class variables "Name" and "Description".</p>
  
  <p>I used a google search for: "Get reference to class field via 
  reflection in C#". I did find information on stackoverflow for reflection but it does not talk about calling methods. I was able to use some of it to find what looks like a pointer.  However I could not make it all the way to calling methods on the reference.  I either am asking Google the wrong question or what I am trying to do is not possible.<br>
  <a href="https://stackoverflow.com/questions/7649324/c-sharp-reflection-get-field-values-from-a-simple-class">C# Reflection - Get field values from a simple class</a></p>
  
  <pre><code>    Class AClass1
      {
          public String Action = String.Empty;
          public List&lt;KeyValuePair&lt;String, String&gt;&gt; Description = new  List&lt;KeyValuePair&lt;string, string&gt;&gt;();
          public List&lt;KeyValuePair&lt;String, String&gt;&gt; Name= new List&lt;KeyValuePair&lt;string, string&gt;&gt;();
      }
  
  
      // Psudo code:
      // The code has been reduced down to show the problem.  Error handling
      // etc has been removed. This may not compile because I had to 
      // reduce down the code to show what I am trying to accomplish. Any
      // suggestions for creating a reference to a class variable and calling
      // its methods would be appreciated.  If needed I can create a small 
      // sample that does compile but I think the sample provided shows 
      // what I am trying to accomplish.  If not let me know and I will
      // scale down further.
  
  
  public void ProcessFeature1(String fieldName, String value1, String value2)
  {
      AClass1 featureToManage = new AClass1();
  
      KeyValuePair&lt;string, string&gt; entry = new KeyValuePair&lt;string, string&gt;(value1, value2);
      if (entry != null)
      {
          // something along these lines is preferred if possible
          var ptrField = featureToManage.GetType().GetField(fieldName).FieldHandle;
          if (ptrField != null)
          {
              // the problem is figuring how to turn the value from reflection into
              // something I can call directly.  I have tried casting to the same
              // type as the variable in the class (not shown here) 
              // but it did not work or I did it wrong.  When I say it does not
              // work the run-time information does not show the method and the
              // compiler complains generally saying no such method Insert found.
              ptrField.Insert(0, entry);
          }
          else
          {
              // this works but would prefer to lookup the
              // the field in the class and call methods so I
              // 1. have less code
              // 2. do not have to hard code the name of the field.
              switch (fieldName)
              {
                  case "Name":
                      {
                          featureToManage.Name.Insert(0, entry);
                          break;
                      }
                  case "Description":
                      {
                          featureToManage.Description.Insert(0, entry);
                          break;
                      }
                  default:
                      {
                          // something bad happened
                          break;
                      }
              }
          }
      }
  }
  </code></pre>