如何在 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 变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to pass lambda #39;include#39; with multiple levels in Entity Framework Core?(如何在 Entity Framework Core 中传递具有多个级别的 lambda include?)
Join/Where with LINQ and Lambda(使用 LINQ 和 Lambda 加入/在哪里)
Getting all types that implement an interface(获取所有实现接口的类型)
Combining two expressions (Expressionlt;Funclt;T, boolgt;gt;)(组合两个表达式 (ExpressionFuncT, boolgt;))
Is there a reason for C##39;s reuse of the variable in a foreach?(C# 在 foreach 中重用变量是否有原因?)
Lambda/Linq with Contains criteria for multiple keywords(Lambda/LINQ WITH包含多个关键字的条件)