title: Get name of property as a string
body: <p>(See below solution I created using the answer I accepted)</p>
  <p>I'm trying to improve the maintainability of some code involving reflection. The app has a .NET Remoting interface exposing (among other things) a method called <code>Execute</code> for accessing parts of the app not included in its published remote interface.</p>
  <p>Here is how the app designates properties (a static one in this example) which are meant to be accessible via Execute:</p>
  <pre><code>RemoteMgr.ExposeProperty(&quot;SomeSecret&quot;, typeof(SomeClass), &quot;SomeProperty&quot;);
  </code></pre>
  <p>So a remote user could call:</p>
  <pre><code>string response = remoteObject.Execute(&quot;SomeSecret&quot;);
  </code></pre>
  <p>and the app would use reflection to find SomeClass.SomeProperty and return its value as a string.</p>
  <p>Unfortunately, if someone renames SomeProperty and forgets to change the 3rd parm of ExposeProperty(), it breaks this mechanism.</p>
  <p>I need to the equivalent of:</p>
  <pre><code>SomeClass.SomeProperty.GetTheNameOfThisPropertyAsAString()
  </code></pre>
  <p>to use as the 3rd parm in <code>ExposeProperty</code> so refactoring tools would take care of renames.</p>
  <p>Is there a way to do this?</p>
  <p>Okay, here's what I ended up creating (based upon the answer I selected and the question he referenced):</p>
  <pre><code>// &lt;summary&gt;
  // Get the name of a static or instance property from a property access lambda.
  // &lt;/summary&gt;
  // &lt;typeparam name=&quot;T&quot;&gt;Type of the property&lt;/typeparam&gt;
  // &lt;param name=&quot;propertyLambda&quot;&gt;lambda expression of the form: '() =&gt; Class.Property' or '() =&gt; object.Property'&lt;/param&gt;
  // &lt;returns&gt;The name of the property&lt;/returns&gt;
  public string GetPropertyName&lt;T&gt;(Expression&lt;Func&lt;T&gt;&gt; propertyLambda)
  {
      var me = propertyLambda.Body as MemberExpression;
  
      if (me == null)
      {
          throw new ArgumentException(&quot;You must pass a lambda of the form: '() =&gt; Class.Property' or '() =&gt; object.Property'&quot;);
      }
  
      return me.Member.Name;
   }
  </code></pre>
  <p>Usage:</p>
  <pre><code>// Static Property
  string name = GetPropertyName(() =&gt; SomeClass.SomeProperty);
  
  // Instance Property
  string name = GetPropertyName(() =&gt; someObject.SomeProperty);
  </code></pre>
  <p>Now with this cool capability, it's time to simplify the ExposeProperty method. Polishing doorknobs is dangerous work...</p>