How to get properties from COM Object using the dynamic keyword

时光总嘲笑我的痴心妄想 提交于 2019-12-08 08:35:23

问题


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".

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;

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...

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


回答1:


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
        }



回答2:


Visual Studio's Debugger uses reflection to enumerate fields and properties for objects of any kind.

Type.GetProperties



来源:https://stackoverflow.com/questions/23433338/how-to-get-properties-from-com-object-using-the-dynamic-keyword

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!