VS2017调试:无法访问COM对象的成员

VS2017 debugging: Unable to access members of COM object

问题

通常,Visual Studio 的 "Dynamic View" 显示对象的成员。有时,该对象需要所有线程 运行 并且您必须手动允许它才能看到成员。但是,对于此 COM 对象,当我尝试查看成员时出现错误:

Unable to evaluate the expression. Operation not supported. Unknown error: 0x80070057.


详情

以下是该过程的屏幕截图:

  1. 查看 COM 对象:

  2. 查看 COM 对象的成员:

  3. 打开动态视图导致:

    The function evaluation requires all threads to run.

  4. 当我点击刷新图标时,错误变化:

    Unable to evaluate the expression. Operation not supported. Unknown error: 0x80070057.


我试过的

除了对这两个错误信息的无定论研究外,我唯一尝试过的就是删除.suo文件并重新启动Visual Studio。

我知道 API 调用了哪些数据 returns 因为我能够将数据打印到控制台。但是,无法从调试器查看这些数据确实让我慢了下来。任何帮助,将不胜感激。谢谢!

Hans Passant 在 this question 中的以下评论帮助我找到了解决方案:

You can usually cast it to one of the interface types supported by the component. Then the debugger gets smart again... iterate it with foreach. Actual underlying runtime type is a proxy, happens when you call the method from a worker thread or if it is an out-of-process server.

解决方案是先遍历 COM 对象,然后再尝试使用它。因此,如果 COM 对象是一个集合,则迭代项目,转换为接口类型,并将结果存储在 var 或自定义模型中。

像这样:

var myObject = com.GetStuff().OfType<InterfaceClass>().Select(s => new { Name = s.Name, Description = s.Description });

在我的例子中,通过启用选项 "Use managed compatibility mode"

,我能够在调试器中看到 com 对象类型

在 visual studio 中,如果您打开工具 >> 选项然后调试 >> 一般确保选项 "Use managed compatibility mode" 它已选中。

这应该将 com 对象显示为其在调试器中的正确类型。