开发者

C# A question about the lambda expression as a method parameter

At the page code.google.com/p/fakeiteasy/ I've noticed the line:

A.CallTo(() => shop.GetTopSellingCandy()).Returns(lollipop开发者_StackOverflow);

so the question is - how to pass a lambda expression as a method parameter ?


This function takes a parameter of type Func<T> (A normal delegate with a generic parameter), or, more likely, Expression<Func<T>> (an expression tree).

The function itself probably has a generic parameter which is inferred from the method passed.

By taking an expression tree, the function is able to inspect the code inside the expression and see what it does.


The code you've given is doing exactly that - passing a lambda expression as a paramter to a method call.


CallTo might have the signature 'CallTo(Action action)'. So lambda is passed as a delegate

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜