c# - 如何调试 Lazy<T>?

标签 c# .net lazy-loading

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

最佳答案

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

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

很好explained on MSDN , 基于 Where方法,它有两个版本:Enumerable.Where这需要 Func<T, bool>Queryable.Where这需要 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 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.

关于c# - 如何调试 Lazy<T>?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32237202/

相关文章:

c# - 无法继承没有默认构造函数的 Base Controller 类

c# - 正确使用 .NET MemoryCache 的锁定模式

java - 如何根据变量延迟Spring Bean初始化

android - fragment 中的惰性列表

.net - 如何将文本转换为rtf,以便发送电子邮件?

lazy-loading - 如果浏览器禁用了 JavaScript,是否仍会请求 .js 文件?

c# - 来自 sql 表的 TreeView

c# - 在集合中找不到参数 "created_on"

c# - 使用haarcascade进行上身和下身检测的正确比例因子,minNeighbors和大小

c# - 具有多级继承的 base.Method() 未被调用?