如何调试 Lazy<T>?

How to debug Lazy<T>?

我有一个用 lambda 初始化的 Lazy<T>。调试时如何查看初始化 lambda 的主体?我希望有类似 Expression class 的 DebugView 的东西,但我没有找到类似的东西。

因为Lazy<T>接受委托,所以不涉及Expressionclass。您的 lambda 像项目中的任何其他代码一样编译,并且在调试期间没有该代码的预览。

Lambda 表达式既可以编译成 IL,也可以转换成表达式树。发生哪一个取决于上下文。如果您的参数声明为委托,将生成常规 IL 代码。如果是 Expression<TFunc> 你会得到可以预览的表达式树。

很好explained on MSDN,基于Where方法,它有两个版本:Enumerable.Where需要Func<T, bool>Queryable.Where需要[=17] =].

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 type System.Func<T, TResult>. A lambda expression is the most convenient way to create that delegate. When you call the same method in, for example, the System.Linq.Queryable class (as you do in LINQ to SQL) then the parameter type is an System.Linq.Expressions.Expression<Func> where Func is any of the Func 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 the Where calls to look similar although in fact the type of object created from the lambda is different.