Skip to content

Instantly share code, notes, and snippets.

@droyad
Created December 3, 2013 02:52
Show Gist options
  • Select an option

  • Save droyad/7763181 to your computer and use it in GitHub Desktop.

Select an option

Save droyad/7763181 to your computer and use it in GitHub Desktop.
PropertyInfo to Expression
void Main()
{
var propertyInfo = typeof(A).GetProperty("Foo");
var lambda = Get<A>(propertyInfo);
lambda.Compile()(new A()).Dump();
}
public Expression<Func<T, int>> Get<T>(PropertyInfo propertyInfo)
{
ParameterExpression entity = Expression.Parameter(typeof (A), "e");
Expression propertyExpr = Expression.Property(entity, propertyInfo);
return Expression.Lambda<Func<T, int>>(propertyExpr, new[] {entity});
}
class A
{
public int Foo { get { return 1; } }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment