0

If I had a COM object (instanced with Activator.CreateInstance) that exposes other objects as properties, like

Object1.Object2.Object3

How can I get Object3 if I only have an instance of Object1 using reflection?

1 Answer 1

3

How can I get Object3 if I only have an instance of Object1 using reflection?

That's impossible. You need to first get the Object2 instance via reflection and only after that you can access Object3. That might have been possible if you had some other object that is holding reference to Object3.

By the way if you are using .NET 4.0 or later you may find that the dynamic keyword might save you lots of hassles when dealing with COM objects and reflection. Check it out here: http://msdn.microsoft.com/en-us/library/vstudio/dd264736.aspx

Sign up to request clarification or add additional context in comments.

2 Comments

Yeah, I've been reading about the wonders of dynamic late binding in 4.0, but unfortunately, I'm on .NET 3.5... :(
Oh, sorry to here that. By the way you could use VB.NET which doesn't have the same strict typing as C#. It's much easier to work with COM objects in VB.NET than it is in C# (before the dynamic keyword of course). So basically you could encapsulate this logic into a .NET assembly written in VB.NET that your other parts of code could access. If you cannot do that, then I am afraid that you have to prepare yourself for hardcore reflection stuff.

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.