Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If it helps, our code base uses the following:</p> <pre><code>public class TypeHelper { private static PropertyInfo GetPropertyInternal(LambdaExpression p) { MemberExpression memberExpression; if (p.Body is UnaryExpression) { UnaryExpression ue = (UnaryExpression)p.Body; memberExpression = (MemberExpression)ue.Operand; } else { memberExpression = (MemberExpression)p.Body; } return (PropertyInfo)(memberExpression).Member; } public static string GetPropertyName&lt;TObject&gt;(Expression&lt;Func&lt;TObject, object&gt;&gt; p) { return GetPropertyNameInternal(p); } public static string GetPropertyName&lt;TObject, T&gt;(Expression&lt;Func&lt;TObject, T&gt;&gt; p) { return GetPropertyNameInternal(p); } public static string GetPropertyName&lt;T&gt;(Expression&lt;Func&lt;T&gt;&gt; p) { return GetPropertyNameInternal(p); } public static string GetPropertyName(Expression p) { return GetPropertyNameInternal((LambdaExpression) p); } private static string GetPropertyNameInternal(LambdaExpression p) { return GetPropertyInternal(p).Name; } public static PropertyInfo GetProperty&lt;TObject&gt;(Expression&lt;Func&lt;TObject, object&gt;&gt; p) { return GetPropertyInternal(p); } public static PropertyInfo GetProperty&lt;TObject, T&gt;(Expression&lt;Func&lt;TObject, T&gt;&gt; p) { return GetPropertyInternal(p); } public static PropertyInfo GetProperty&lt;T&gt;(Expression&lt;Func&lt;T&gt;&gt; p) { return GetPropertyInternal(p); } } </code></pre> <p>That gives you the ability to do:</p> <pre><code>var propertyName = TypeHelper.GetPropertyName&lt;Customer&gt;(c =&gt; c.Id); </code></pre> <p>Or</p> <pre><code>var propertyName = TypeHelper.GetPropertyName(() =&gt; this.Id); // If inside Customer class </code></pre>
 

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