Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 是否可以在本地语言中看到lambda的表达式,或者在VisualStudio中查看窗口?_C#_.net_Visual Studio - Fatal编程技术网

C# 是否可以在本地语言中看到lambda的表达式,或者在VisualStudio中查看窗口?

C# 是否可以在本地语言中看到lambda的表达式,或者在VisualStudio中查看窗口?,c#,.net,visual-studio,C#,.net,Visual Studio,我正在处理许多类型为Action和Func的lambda表达式,它们被传递给参数,我发现很难跟踪这些变量所代表的实际代码。在调试期间,可以在Localswindows或Watchwindows中查看这些lambda的实际代码? 例如,Action doNothing=(u,uu)=>{}当doNothing作为参数传递给方法时,我希望在Locals或Watch窗口中的某个地方看到(u,uu)=>{}。您不能直接看到委托的内容。它们被设计成不知道自己在调用Invoke时会做什么 Action a

我正在处理许多类型为
Action
Func
的lambda表达式,它们被传递给参数,我发现很难跟踪这些变量所代表的实际代码。在调试期间,可以在
Locals
windows或
Watch
windows中查看这些lambda的实际代码?

例如,
Action doNothing=(u,uu)=>{}
doNothing
作为参数传递给方法时,我希望在
Locals
Watch
窗口中的某个地方看到
(u,uu)=>{}

您不能直接看到委托的内容。它们被设计成不知道自己在调用
Invoke
时会做什么

Action a = () => Console.WriteLine(); // getting "Console.WriteLine()" from "a" is not possible.
但是调试器可以帮助您找出lambda是什么

您可以在“立即”窗口中编写表达式来计算它们

Func<int, int> negate = x => -x; // let's say you can't see this line but you know "negate" exists.
// in Immediate window, you can write the following
// negate.Invoke(1) // -1
// negate.Invoke(-1) // 1
// then you can guess that "negate" will negate the number, though not conclusively
Func negate=x=>-x;//假设你看不到这一行,但你知道“否定”的存在。
//在即时窗口中,您可以编写以下内容
//否定。调用(1)/-1
//Invoke(-1)//1
//然后你可以猜测“否定”将否定这个数字,尽管不是决定性的
然而,我认为最好的方法是追溯您的代码,以查看该委托来自何处。这样,您可以找到添加到委托的原始方法/lambda

您提到委托作为参数传递给方法。这意味着您可以向下查看堆栈跟踪,查看哪个方法调用了该方法。委托可能在那里创建。如果没有,请再次向下查看堆栈,直到找到为止

如果代理存储在字段/属性中,则必须搜索该字段/属性的设置时间,这可能很难

编辑:

见此帖:


lambda本质上被编译成某种类型的方法。您能在调试器中“看到”方法的实现吗?否。

尝试立即窗口,您可以在那里执行代码,并实际看到结果。。