Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can use Lambda Expressions to resolve a compile-time property reference. (code modified from <a href="https://stackoverflow.com/questions/671968/retrieving-property-name-from-lambda-expression">Retrieving Property name from lambda expression</a>)</p> <pre><code>public PropertyInfo GetPropertyInfo&lt;TProperty&gt;( Expression&lt;Func&lt;TProperty&gt;&gt; propertyLambda) { MemberExpression member = propertyLambda.Body as MemberExpression; if (member == null) throw new ArgumentException(string.Format( "Expression '{0}' refers to a method, not a property.", propertyLambda.ToString())); PropertyInfo propInfo = member.Member as PropertyInfo; if (propInfo == null) throw new ArgumentException(string.Format( "Expression '{0}' refers to a field, not a property.", propertyLambda.ToString())); return propInfo; } </code></pre> <p>You don't need to use it as an extension method (though if you want to adapt it you <em>can</em>, but having a source object instance is not necessary except to write the line)</p> <pre><code>public class Test { public string Prop { get; set; } } Test t = new Test(); PropertyInfo propInfo = GetPropertyInfo(() =&gt; t.Prop); Console.WriteLine(propInfo.Name + " -&gt; " + propInfo.PropertyType); //Prop -&gt; System.String </code></pre> <p>EDIT: If you want to have some nice syntax and have to avoid having an existing reference to an object of the type, you can do something like:</p> <pre><code>public static class TypedReflection&lt;TSource&gt; { public static PropertyInfo GetPropertyInfo&lt;TProperty&gt;( Expression&lt;Func&lt;TSource, TProperty&gt;&gt; propertyLambda) { MemberExpression member = propertyLambda.Body as MemberExpression; if (member == null) throw new ArgumentException(string.Format( "Expression '{0}' refers to a method, not a property.", propertyLambda.ToString())); PropertyInfo propInfo = member.Member as PropertyInfo; if (propInfo == null) throw new ArgumentException(string.Format( "Expression '{0}' refers to a field, not a property.", propertyLambda.ToString())); return propInfo; } } </code></pre> <p>And call it like:</p> <pre><code>PropertyInfo propInfo = TypedReflection&lt;Test&gt;.GetPropertyInfo(o =&gt; o.Prop); </code></pre> <p>At this point, it's pretty easy to add additional typed reflection methods (get methods, fields, etc.)</p> <p>EDIT: It's still sorta two generic types dependent, but it's hidden away through type inference. I prefer the second example; at the very minimum you need to specify the declaring class type (because you want type safety), but you don't need an object instance. It also has the benefit (which I suspect you're after) that if you rename the property, it's propagated to this code obtaining the <code>PropertyInfo</code>.</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