简体   繁体   中英

How to get value of a Lambda MemberExpression

Given a Lambda Expression:

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

and used like:

Define(x => x.Collection.Count)

What is the best method of getting the value of Count? Is there an easy way with the Expression Tree or should I use reflection to parse the tree to get the PropertyInfo and GetValue()?

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM