开发者

Extracting input arguments from Lambda expressions

I have method that accepts LambdaExpression as a parametar

    public void SomeMethod(Expression<Func<SomeObject, bool>> predicate)
    { }

inside this method I would like to extract the members and the arguments values from the expression Body. Is this possible and how can be achieved?

Method can be invoked like this:

    SomeMethod(t=> t.Id == 3 && t.Name=="Name");

or

    SomeMethod(t=> t.Id.Equals(3));

or

    SomeMethod(t=> t.Id > 3 || t.Id = 1);
开发者_StackOverflow中文版

etc. You get the point.


The solution you are looking for is called Expression Tree Visitor. By using it you can go though lambda expression members and arguments.

Also this walkthrough may be useful, it contains code for lambda expression partial evaluator in case arguments in lambda expression is not a constants

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜