How to watch (i.e. debug) extra-lambda variable inside lambda scope in C#/Visual-Studio/Unity3d?(如何在 C#/Visual-Studio/Unity3d 的 lambda 范围内观察(即调试)额外的 lambda 变量?)
问题描述
在将 Visual Studio Professional 2015 与 Unity 结合使用时,我注意到当我单步执行 lambda 表达式的主体时,我无法看到在 lambda 表达式外部声明/分配但在 lambda 表达式内部读取的变量.
When using Visual Studio Professional 2015 with Unity, I've noticed that when I am stepping though the body of a lambda expression, I cannot watch variables which were declared/assigned outside of but are being read inside of the lambda expression.
private IEnumerator DoTheThing(string filter)
{
TextureDownload texDl = new TextureDownload();
texDl.OnCompleted += () =>
{
_log.Log("Mesh texture " + texDl.GetType() + ":" + texDl.GetHashCode());
textures.Add(texDl);
};
yield break;
}
我得到了错误
标识符texDl
不在范围内
显然该变量在这个范围内是可访问的,因为 lambda 函数正在使用它.
Obviously the variable is accessible in this scope, because the lambda function is using it.
是否有任何远程简单/方便的方法来观察这些变量的值?
Is there any remotely easy/convenient way to watch the value of such variables?
推荐答案
原来这是 C# lambda 调用 for 循环引用到最后一个对象
使用协程和 lambda 表达式的 Mono/Unity 存在问题;
There is an issue with Mono/Unity with coroutines and lambda expressions;
协程本身也是一个隐式闭包,它将所有局部变量作为成员变量移动到一个单独的类中.这意味着不可能在生成器方法中创建真正的"局部变量.这就是显式局部变量声明不起作用的原因.
The coroutine itself is an implicit closure as well which moves all local variable to a seperate class as member variables. That means it's impossible to create a "real" local variable inside a generator method. That's why the explicit local variable declaration doesn't work.
请参阅 http://answers.unity3d.com/questions/974036/c-lambda-call-in-for-loop-references-to-last-objec.html
这篇关于如何在 C#/Visual-Studio/Unity3d 的 lambda 范围内观察(即调试)额外的 lambda 变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 C#/Visual-Studio/Unity3d 的 lambda 范围内观察(即调试)额外的 lambda 变量?
基础教程推荐
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- 将 XML 转换为通用列表 2022-01-01