Reputation: 1921
I have a Lazy<T>
initialized with a lambda. How to see the body of the initializing lambda while debugging? I expect to have something like the DebugView
of the Expression
class but I've found nothing like that.
Upvotes: 4
Views: 1531
Reputation: 125620
Because Lazy<T>
takes a delegate, there is no Expression
class involved. Your lambda is compiled like any other code in your project and there is no preview of that code during debug.
Lambda expression can be compiled either into IL or transformed into Expression Tree. Which one happens depends on the context. If your parameter is declared as delegate regular IL code will be generated. If it's Expression<TFunc>
you'll get expression tree which can be previewed.
It's nicely explained on MSDN, based on Where
method, which has two versions: Enumerable.Where
which takes Func<T, bool>
and Queryable.Where
which takes Expression<Func<T, bool>>
.
When you use method-based syntax to call the Where method in the
Enumerable
class (as you do in LINQ to Objects and LINQ to XML) the parameter is a delegate typeSystem.Func<T, TResult>
. A lambda expression is the most convenient way to create that delegate. When you call the same method in, for example, theSystem.Linq.Queryable
class (as you do in LINQ to SQL) then the parameter type is anSystem.Linq.Expressions.Expression<Func>
whereFunc
is any of theFunc
delegates with up to sixteen input parameters. Again, a lambda expression is just a very concise way to construct that expression tree. The lambdas allow theWhere
calls to look similar although in fact the type of object created from the lambda is different.
Upvotes: 4