Question

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?

Was it helpful?

Solution

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
        }

OTHER TIPS

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

Type.GetProperties

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top