如何在 C#/Visual-Studio/Unity3d 的 lambda 范围内观察(即调试)额外的 lambda 变量?

How to watch (i.e. debug) extra-lambda variable inside lambda scope in C#/Visual-Studio/Unity3d?(如何在 C#/Visual-Studio/Unity3d 的 lambda 范围内观察(即调试)额外的 lambda 变量?)

本文介绍了如何在 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 变量?

基础教程推荐