Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>A page of code is a thousand words, so here's how Microsoft does it in <a href="http://compositewpf.codeplex.com/" rel="nofollow">Prism</a>:</p> <pre><code>///&lt;summary&gt; /// Provides support for extracting property information based on a property expression. ///&lt;/summary&gt; public static class PropertySupport { /// &lt;summary&gt; /// Extracts the property name from a property expression. /// &lt;/summary&gt; /// &lt;typeparam name="T"&gt;The object type containing the property specified in the expression.&lt;/typeparam&gt; /// &lt;param name="propertyExpression"&gt;The property expression (e.g. p =&gt; p.PropertyName)&lt;/param&gt; /// &lt;returns&gt;The name of the property.&lt;/returns&gt; /// &lt;exception cref="ArgumentNullException"&gt;Thrown if the &lt;paramref name="propertyExpression"/&gt; is null.&lt;/exception&gt; /// &lt;exception cref="ArgumentException"&gt;Thrown when the expression is:&lt;br/&gt; /// Not a &lt;see cref="MemberExpression"/&gt;&lt;br/&gt; /// The &lt;see cref="MemberExpression"/&gt; does not represent a property.&lt;br/&gt; /// Or, the property is static. /// &lt;/exception&gt; public static string ExtractPropertyName&lt;T&gt;(Expression&lt;Func&lt;T&gt;&gt; propertyExpression) { if (propertyExpression == null) { throw new ArgumentNullException("propertyExpression"); } var memberExpression = propertyExpression.Body as MemberExpression; if (memberExpression == null) { throw new ArgumentException(Resources.PropertySupport_NotMemberAccessExpression_Exception, "propertyExpression"); } var property = memberExpression.Member as PropertyInfo; if (property == null) { throw new ArgumentException(Resources.PropertySupport_ExpressionNotProperty_Exception, "propertyExpression"); } var getMethod = property.GetGetMethod(true); if (getMethod.IsStatic) { throw new ArgumentException(Resources.PropertySupport_StaticExpression_Exception, "propertyExpression"); } return memberExpression.Member.Name; } } </code></pre> <p>If you want to take attributes into account it's going to be slightly more complicated, but the general idea of accepting an <code>Expression&lt;Func&lt;T&gt;&gt;</code> and fishing out the name of the property being targeted is the same.</p> <p><strong>Update:</strong> As is, the method will accept only one parameter; I only provided it as a guideline. The idea can be generalized of course:</p> <pre><code>public static string[] ExtractPropertyNames&lt;T&gt;( Expression&lt;Func&lt;T, object&gt;&gt; propertyExpression) </code></pre> <p>This method will accept an expression that takes a T and returns an anonymous type which you can then reflect upon. You could substitute a second type parameter for <code>object</code> but that doesn't really do anything here because the only thing you want to do is reflect on the type.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload