문제

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?

도움이 되었습니까?

해결책

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
        }

다른 팁

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

Type.GetProperties

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top