title: How do I get the name of a property from a property in C# (2.0)
body: <p>I know I could have an attribute but that's more work than I want to go to... and not general enough.</p>
  
  <p>I want to do something like </p>
  
  <pre><code>class Whotsit
  {
      private string testProp = "thingy";
  
      public string TestProp 
      {
          get { return testProp; }
          set { testProp = value; }
      }
  
  }
  
  ...
  
  Whotsit whotsit = new Whotsit();
  string value = GetName(whotsit.TestProp); //precise syntax up for grabs..
  </code></pre>
  
  <p>where I'd expect value to equal "TestProp"</p>
  
  <p>but I can't for the life of me find the right reflection methods to write the GetName method...</p>
  
  <p>EDIT: Why do I want to do this? I have a class to store settings read from a 'name', 'value' table. This is populated by a generalised method based upon reflection. I'd quite like to write the reverse...</p>
  
  <pre><code>/// &lt;summary&gt;
  /// Populates an object from a datatable where the rows have columns called NameField and ValueField. 
  /// If the property with the 'name' exists, and is not read-only, it is populated from the 
  /// valueField. Any other columns in the dataTable are ignored. If there is no property called
  /// nameField it is ignored. Any properties of the object not found in the data table retain their
  /// original values.
  /// &lt;/summary&gt;
  /// &lt;typeparam name="T"&gt;Type of the object to be populated.&lt;/typeparam&gt;
  /// &lt;param name="toBePopulated"&gt;The object to be populated&lt;/param&gt;
  /// &lt;param name="dataTable"&gt;'name, 'value' Data table to populate the object from.&lt;/param&gt;
  /// &lt;param name="nameField"&gt;Field name of the 'name' field'.&lt;/param&gt;
  /// &lt;param name="valueField"&gt;Field name of the 'value' field.&lt;/param&gt;
  /// &lt;param name="options"&gt;Setting to control conversions - e.g. nulls as empty strings.&lt;/param&gt;
  
  public static void PopulateFromNameValueDataTable&lt;T&gt;
          (T toBePopulated, System.Data.DataTable dataTable, string nameField, string valueField, PopulateOptions options)
      {
          Type type = typeof(T);
          bool nullStringsAsEmptyString = options == PopulateOptions.NullStringsAsEmptyString;
  
          foreach (DataRow dataRow in dataTable.Rows)
          {
              string name = dataRow[nameField].ToString();
              System.Reflection.PropertyInfo property = type.GetProperty(name);
              object value = dataRow[valueField];
  
              if (property != null)
              {
                  Type propertyType = property.PropertyType;
                  if (nullStringsAsEmptyString &amp;&amp; (propertyType == typeof(String)))
                  {
                      value = TypeHelper.EmptyStringIfNull(value);
                  }
                  else
                  {
                      value = TypeHelper.DefaultIfNull(value, propertyType);
                  }
  
                  property.SetValue(toBePopulated, System.Convert.ChangeType(value, propertyType), null);
              }
          }
      }
  </code></pre>
  
  <p>FURTHER EDIT: I am just in code, have an instance of Whotsit and I want to get the text string of the 'TestProp' property. It seems kind of weird I know, I can just use the literal "TestProp" - or in the case of my  class to datatable function I'd be in a foreach loop of PropertyInfos. I was just curious... </p>
  
  <p>The original code had string constants, which I found clumsy.</p>