How to get Lambda MemberExpression value

Given a Lambda expression:

Define(Expression<Func<T, int>> property) 

and is used as:

 Define(x => x.Collection.Count) 

What is the best method to get the count value? Is there an easy way with an expression tree, or use reflection to parse the tree to get PropertyInfo and GetValue ()?

+4
source share
1 answer

You can use the following to get a delegate that matches your lambda:

 var propDelegate = property.Compile(); var count = propDelegate(...); 

propDelegate will be Func<T, int> , and you can call it by passing the required object of type T.

+4
source

All Articles