1

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?

2 Answers 2

2

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
        }
Sign up to request clarification or add additional context in comments.

Comments

0

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

Type.GetProperties

1 Comment

That's not true for COM objects. See, msdn.microsoft.com/en-us/library/dd998308.aspx

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.