Call a method from an external DLL

  • Thread starter Thread starter Tom
  • Start date Start date
T

Tom

I am dynamically loading a DLL that I wrote that contains forms, a module,
and classes. I am doing this via:

Dim asm As [Assembly] = [Assembly].LoadFrom(MyDLLPath)
Dim TypeToLoad As Type = asm.GetType(MyDLLName & "." & MyFormClassName)
GenericObj = Activator.CreateInstance(TypeToLoad)

I have no problems loading and referencing forms, but now I have a method in
this dynamic class (in my module) that I want to reference. Let's (for
argument sake) say the class name is MyClass, the module name in that class
is modMyClass, and the function/sub I want to reference is MyFunction. The
problem with the above code is that the TypeToLoad never loads anything and
is Nothing causing GenericObj to fail (obviously). I tried doing an
asm.GetType("MyDLLName.modMyClass.MyFunction") but that still didn't work.

Again, I don't have any trouble accessing this DLLs forms (via Dim
FormToShow As Form = CType(GenericObj, Form)), it is just I also want to
dynamically access the DLLs public functions/subs in that DLL also.

Anyone got any ideas? Where am I going wrong?

Tom
 
You can do one of two things:

1) create an Interface contract. The classes Implement that Interface, and
are therefore contractually bound to have the functions defined by the
Interface. Then, your program will also have access to the same Interfaces.
Even if your program doesn't know the classes in advance, it does know the
Interfaces, and can cast an object reference from your dynamic assembly to
the known Interface and directly call the member.

2) slightly more painful... you can use System.Reflection to discover and
Invoke the class members by name and attributes.

-Rob Teixeira [MVP]
 
Back
Top