如何在C#/Visual-Studio/Unity3d中的lambda范围内观看(即调试)extra-lambda变量? [英] How to watch (i.e. debug) extra-lambda variable inside lambda scope in C#/Visual-Studio/Unity3d?
问题描述
当将Visual Studio Professional 2015与Unity一起使用时,我注意到当我单步执行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?
推荐答案
结果证明,这是带有协程和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.
请参见 查看全文