locked
Help: Assembly Communication/Reflection RRS feed

  • Question

  • (please move if I am posting in the wrong forum)

     

    I don't need a specific answer, I just need a push in the right direction so I can get started.

     

    I need to be able to make method calls from an assembly that is being loaded at runtime by my main assembly.  That is, I have a 'child' assembly being loaded by a 'parent' assembly, and I need the 'child' to invoke methods in the 'parent' without creating a new instance of the 'parent' first.  All the 'parent' knows is the assembly filename and the main Type of the 'child' .  I can make calls to the 'child' easily, but I do not know how to reference the 'parent' in the 'child' and then be able to make calls to it.

     

    I have been able to use reflection to get the methods and everything of the 'parent' within the 'child' but I can't figure out how to reference the 'parent'.  I have tried doing

     

    (parent type).GetMethod("method on parent").Invoke(null, null);

     

    but that throws an error saying I need to provide a target object.  I can't figure out how to reference the target object without creating an entirely new instance of the 'parent'

     

    Any ideas or comments would be greatly appreciated.  Thanks

    Wednesday, August 29, 2007 9:47 PM

Answers

All replies

  • Remoting is one solution for this. You can register a type from the parent assembly as a Wellknown Singleton RemotingService and then expose it to class via an IPCServerChannel. Then in your client's main type, you can create a proxy to this parent assembly type and invoke methods by using an IPCClientChannel.

     

    Tuesday, September 4, 2007 11:12 PM
    Moderator
  • If the assemblies are in the same Application Domain you can just get and instance of any type on it. Use for example the activator object:

     

    http://msdn2.microsoft.com/en-us/library/system.activator.createinstance.aspx

     

    Create an instance and invoke any method you want.

    Thursday, September 20, 2007 12:28 PM
    Moderator
  • Maybe you'd better to pass your parent assembly to the child one. (As you say 'parent' knows the main type of the 'child' so add a method to child and invoke it in the parent and pass parent's assembly as parameter to it!

    You can use a terible! way. use StackTrace to get the stacktrace of calling in the 'child' and extract stackframes and get its methods. Use the found methods to get the type and use the type to get the assembly!!!!

    But I strongly recommand the first one!
    Thursday, September 20, 2007 2:12 PM
    Moderator