I know I can write the following to generate lambda expression:
Expression<Func<st开发者_开发百科ring, bool>> lambda = s => s.Length == 5;
But is there any way to automatically generate expression for property? In other words is there strongly-typed analogue of this:
var property = Expression.Property("Name")
This will give you a lambda which returns the Length
property:
Expression<Func<string, int>> lambda = s => s.Length;
If you don't want the full lambda, but only the MemberExpression
which accesses the property, you can do that:
var propertyExpression = (MemberExpression)lambda.Body;
Expressions<Func<ClassWithProperty, PropertyReturnType>> lambda = C => C.Name;
精彩评论