Visual Studio的调试器/交互式窗口如何转储.NET中的COM对象的属性? [英] How does Visual Studio's debugger/interactive window dump the properties of COM Objects in .NET?

查看:374
本文介绍了Visual Studio的调试器/交互式窗口如何转储.NET中的COM对象的属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在本,我相信使用的调试器无法列出 System .__ Comobject 的成员(例如点。 Envelope ),只有强类型RCW(例如 point )。





Visual Studio是如何做到的?



我相信在这种情况下,是因为主互操作程序集存在与这些对象支持的接口的定义,并且Visual Studio可能使用反射列举支持接口和属性。是准确吗?如果是,它是如何工作的?



对于初学者,如何访问PIA?它只查看当前加载的PIA还是动态加载它们(如果是,如何)?它如何确定哪些接口,哪些可以有很多,枚举的属性?它只是似乎使用一个,而不一定是第一个。从API的文档我正在使用(ArcObjects),这些对象的默认界面是 IUnknown ,所以它不只是使用默认接口。



在屏幕截图中的示例中,的是 IEnvelope 界面,它继承了 IGeometry 界面。 VS2010如何知道不枚举 IGeometry 的成员,而在我的测试中,如果你只是枚举了PIA中的所有接口类型,它首先出现?一些非常聪明的事情,或者我缺少一些明显的东西?



我问的原因是 LINQPad 似乎愿意实现相同的功能,如果他知道VS是如何做到的。因此,这里的一个好的答案可以帮助改进这个非常受欢迎的工具。

这是怎么做:





其他增强功能适用:查询 IPersist * 系列接口或 IProvideClassInfo 可替代地获取对象的类型库引用并发现属性。


In this related question, I noted that Visual Studio's debugger is able to enumerate the properties of System.__ComObject references, which is "a hidden type used when the wrapper type is ambiguous" -- e.g., the type of object you get when you obtain it from another COM object and don't instantiate it yourself:

Additionally, if you simply write a COM object's identifier into the Immediate Window, its properties and values are similarly dumped:

Note that this is separate from VS2010's "Dynamic View", which I believe uses IDispatch and COM reflection to enumerate the properties of COM objects without the use of PIAs and .NET reflection. The objects I am working with do not implement IDispatch (nor do they implement IProvideClassInfo for that matter), and as such, "Dynamic View" is not able to obtain any information about them:

Interestingly, SharpDevelop's debugger is not able to list the members of System.__Comobjects (e.g. point.Envelope), only strongly-typed RCWs (e.g. point).

So how is Visual Studio able to do it?

I believe in this case, it is because Primary Interop Assemblies exist with definitions for the interfaces supported by these objects, and Visual Studio is likely using reflection to enumerate the supported interfaces and properties. Is that accurate? And if so, how does it work?

For starters, how does it access the PIAs? Does it only look at currently loaded PIAs or does it dynamically load them (and if so, how)? How does it determine which interface, of which there can be many, to enumerate the properties of? It only seems to use one, and not necessarily the first. From the documentation of the API I'm working with (ArcObjects), the default interface for these objects is IUnknown, so it's not just using the default interface either.

In the example in the screenshots, the interface it is enumerating the members of is the IEnvelope interface, which inherits from the IGeometry interface. How does VS2010 know not to enumerate the members of IGeometry instead, which, in my testing, appears first if you just enumerate all the interface types in the PIA? Something very clever going on or perhaps I am missing something obvious?

The reason I'm asking is that the developer of LINQPad appears willing to implement the same functionality if he knew how VS does it. So a good answer here could go a long ways to helping improve that very popular tool.

解决方案

This is how to do it:

  • obtain COM object's IDispatch (alternative possible path is IDispatchEx)
  • obtain a reference to type library -- IDispatch::GetTypeInfo
  • load type library and enumerate properties
  • query real object for values for discovered properties

Additional enhancement oipions apply: query IPersist* family of interfaces or IProvideClassInfo to alternatively obtain a reference to typelibrary for the object and discover properties.

这篇关于Visual Studio的调试器/交互式窗口如何转储.NET中的COM对象的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆