如何使用dynamic关键字从COM Object获取属性 [英] How to get properties from COM Object using the dynamic keyword

查看:226
本文介绍了如何使用dynamic关键字从COM Object获取属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个从第三方API返回的COM对象。该COM对象具有一个属性,该属性具有关于该对象的大量系统信息...返回的属性的类型为 System.Object。

I have a COM object returned from a 3rd party API. This COM object has a property on it that has a bunch of system information regarding that object... the property returned is of type "System.Object".

调试时这个COM对象,我看到了我感兴趣的属性,Visual Studio调试器(2012)将动态视图附加到该对象,让我看到它是某些对象的数组,例如字典...

When debugging this COM object, I see the property I'm interested in, and the Visual Studio debugger (2012) attaches a dynamic view to that object, allowing me to see that it's an array of some object like a dictionary...

我能够对一些动态关键字用法进行硬编码,以从我关心的对象中提取值,例如:

I'm able to hard code some dynamic keyword usage to extract the value out of this object that I care about, like this:

var temp = ((dynamic)myObject.someProperty)[11].ValueString;

虽然可行,但显然存在更好的方法,因为Visual Studio调试器能够枚举并显示该对象的内容是动态的...

While this works, there's obviously a better way, as Visual Studio debugger is able to enumerate and display the contents of this object dynamically...

我该如何在C#中实现相同的目的,最好不使用任何互操作方法,并使用dynamic关键字?

How can I achieve the same thing in C#, preferably without using any interop methods, and with the dynamic keyword?

推荐答案

我找到了一种解决方法:

I found one way of doing it:

        var myObject = ((dynamic)comObject.someProperty);
        foreach (var index in myObject)
        {
           // This will loop over each object in the array
        }

这篇关于如何使用dynamic关键字从COM Object获取属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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