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
精彩评论