I am working on a project where I am referencing one COM C++ dll. The classes inside this dll are in nested form which creates a hierarchy


      --> Ears
         --> Ear1
         --> Ear2
      --> Ears
         --> Ear1
         --> Ear2

Cats and Ears are Collections, So In my case I am Creating object of mammals class :

Mammals objMammals= new Mammals();
objMammals.Cats.Add(); // Adds a cat object with default values

and then you can add Ears and so On and everything works fine.

But Problem occurs when you try the same thing using reflection.

int MammalsId = (int)objMammals.GetType().InvokeMember("FindById", System.Reflection.BindingFlags.InvokeMethod, null,objMammals, new object{0});// returns the Mammals Object Id from hierarchy

object CatsObject = (object)objMammals.GetType().InvokeMember("Cats", System.Reflection.BindingFlags.GetProperty, null,objMammals, new object{1});

The above line works fine but once you call GC.Collect() in between the last two lines the objMammals.GetType() will turn to __ComObject(which does not have Cats) and it will start throwing runtime exceptions.

The Dll reference is Isolated and I can see the generated Manifest file looks correct. I've tried to register the Dll as well as the Interop as well but No luck, If some how I create any reference of Cats object or Ears object before the GC comes in then It works just fine.

I want the objects to always contain their actual type, So I can use the reflection as i need to.

0 Answers