简体   繁体   English

在 Visual Studio 的监视窗口中使用 LINQ 表达式

[英]Using LINQ expressions in Visual Studio's Watch window

I have a byte[] variable in program, eg:我在程序中有一个 byte[] 变量,例如:

byte[] myByteArray = new byte[] { 0xF0, 0x0F };

When debugging this program, I wanted to display the byte array content as individual hexadecimal values inside Visual Studio's Watch window.在调试这个程序时,我想在 Visual Studio 的 Watch 窗口中将字节数组内容显示为单独的十六进制值。

So I tried to use the following LINQ expression in the Watch Window, without success:所以我尝试在 Watch Window 中使用以下 LINQ 表达式,但没有成功:

myByteArray.Select(value => value.ToString("X2")).ToArray()

Watch window's error message:观察窗口的错误信息:

error CS1061: 'byte[]' does not contain a definition for 'Select' and no extension method 'Select' accepting a first argument of type 'byte[]' could be found (are you missing a using directive or an assembly reference?)错误 CS1061:“byte[]”不包含“Select”的定义,并且找不到接受“byte[]”类型的第一个参数的扩展方法“Select”(您是否缺少 using 指令或程序集引用? )

Does anyone know if there is a way to use LINQ expressions in Visual Studio's Watch window without installing third-party extensions?有谁知道是否有一种方法可以在不安装第三方扩展的情况下在 Visual Studio 的监视窗口中使用 LINQ 表达式?

I'm using VS2017 15.6.6 at this moment.我此时正在使用 VS2017 15.6.6。

Edit: A screenshot of this issue...编辑:此问题的屏幕截图...

在此处输入图片说明

如果代码中没有 'using System.Linq' 语句,您仍然可以通过手动调用扩展方法来使用 Linq 查询:

System.Linq.Enumerable.Select(collection, x=>x.Name)

I tried to reproduce your problem and found the following:我试图重现您的问题并发现以下内容:

It seems the watch window uses the namespaces you referenced (via using ) in your code.似乎监视窗口使用了您在代码中引用(通过using )的命名空间。

If you don't use linq (and System.Linq namespace) in the code file, the watch window cannot find the extensions.如果您不在代码文件中使用 linq(和System.Linq命名空间),监视窗口将找不到扩展名。

If you have a using System.Linq;如果你有一个using System.Linq; and use something from that namespace in your code, the watch window will find and execute the linq extensions.并在您的代码中使用该命名空间中的某些内容,监视窗口将查找并执行 linq 扩展。 (If you don't use anything from System.Linq the reference is optimized away, so this assembly is not loaded at runtime and the debugger can't use it). (如果您不使用System.Linq的任何System.Linq该引用将被优化掉,因此该程序集不会在运行时加载并且调试器无法使用它)。

在此处输入图片说明

Try to add the following: (Its working on my test)尝试添加以下内容:(它正在我的测试中工作)

> using System.Collections.Generic;
 using System.Linq;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 有没有办法在我自己的应用程序中使用Visual Studio的“监视窗口”? - Is there a way to use Visual Studio's Watch Window in my own App? Visual Studio,在监视窗口中查看变量的内存地址 - Visual Studio, See variable's memory address in watch window Visual Studio - 在调试时将多行表达式插入Watch Window - Visual Studio - inserting multi-line expressions into Watch Window while debugging 在Visual Studio监视窗口中隐藏空成员 - Hide null members in Visual Studio watch window 如何在 Visual Studio 2019 中打开手表 Window - How to open the Watch Window in Visual Studio 2019 Visual Studio 2019 观察 Window:IntelliSense 完成 - Visual Studio 2019 Watch Window: IntelliSense Completion Visual Studio Watch窗口没有考虑使用 - Visual Studio Watch window not taking into account usings Visual Studio调试“快速监视”工具和lambda表达式 - Visual Studio debugging “quick watch” tool and lambda expressions 如何创建类似Visual Studio监视窗口的表? - How can I create a table like Visual Studio's watch window? LINQ 的 OrderBy 在 Visual Studio 的立即数 window 中的行为与在代码中的行为不同 - LINQ's OrderBy behaves different in Visual Studio's Immediate window than within the code
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM