Public vs. Public Shared (Function/Sub) When Invoking DLL...
Hey guys, I have a DLL I'm using in a project, it's in the resources of the application. I'm getting errors when trying to Invoke it, that's why I'm here posting this thread. Does the Function/Sub I'm trying to call, have to be Shared or does it have to be just Public?!
I believe from the error I'm getting, that is has to be Shared. Reason I say that is the error says somthing along the lines of....
Quote:
An exception has been thrown by the target of invocation, System.Reflection.Target.
If it helps, I'm also trying to load the DLL from resources like so....
Code:
Assembly.Load(My.Resources.ClsLib).GetType("Clib.Run").GetMethod("Execute").Invoke _
(Nothing, New Object() {Byte_Arg, String_Arg})
Anyway, thanks for any future help guys, peace....
Re: Public vs. Public Shared (Function/Sub) When Invoking DLL...
A method, whether in a dll or not, has to be part of something. If it is part of a class, then it can't be called without an instance of the class unless it is Shared. If the function is part of a module, then it might appear that it doesn't have to be Shared, but that's because the Shared is hidden, since all members of a module are quietly turned into Shared members by the compilers. Therefore, if the method you are tying to invoke is a member of a class, then you either need to have an instance of the class, or the method has to be Shared. If the method is in a module, then it is already shared.
Re: Public vs. Public Shared (Function/Sub) When Invoking DLL...
If it's not a shared method then you first need to create an instance of the class before calling one of the methods.
Code:
Dim a As Assembly = Assembly.Load(My.Resources.ClsLib)
Dim o As Object = a.CreateInstance("Clib.Run")
o.GetType().GetMethod("Execute").Invoke(o, New Object() {Byte_Arg, String_Arg})
However this is a really slow and costly process, why don't you just want to have the assembly distributed as file and have a reference to it in your project? It's a lot faster and safer to do so.