Created
December 3, 2013 02:52
-
-
Save droyad/7763181 to your computer and use it in GitHub Desktop.
PropertyInfo to Expression
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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