Vb.net,外部动态dll及其调用各自的功能 [英] Vb.net, external dynamic dlls, and calling their respective functions
问题描述
大家好,
似乎有很多有关此主题的信息,但没有任何信息可以真正解决我的问题.我正在编写带有外部dll的基于插件的程序.我的程序动态加载dll(从特定目录加载),但在编译之前我不知道它们是什么.
我需要能够从该dll调用函数.我可以为每个插件(又名GetInfo或其他名称)指定函数的名称,但是我不确定如何在插件中创建函数的实例.
这是我到目前为止所拥有的.
Hey all,
Seems there is a lot of info on this topic but none that actually solve my problem. I am writing a plugin based program with external dlls. My program loads the dlls dynamically (loading from a specific directory) and I do not know what they are prior to compiling.
I need to be able to call a function from that dll. I can specify the name of the function for each plugin (aka, GetInfo or whatever) but I am not sure how to create an instance of the function in the plugin.
Here is what I have so far.
For Each dll In dlls
Try
extAssembly = System.Reflection.Assembly.LoadFrom(dll.FullName)
extForms(index) = extAssembly.CreateInstance("myPlugin.frmMain", True)
Catch ex As Exception
End Try
Next
然后我可以做一个简单的...
And then later I can do a simple...
extForms(index).Showdialog()
但是,如何创建一个可以动态链接到特定函数的对象,该对象可以是表单还是单独的类?
谢谢! :)
But how do I create an object that I can link to a specific function either in my form or a separate class dynamically?
Thanks! :)
推荐答案
好,我已经在不同的版本中做到了这一点.请在这里查看我的设计草图:
创建使用可重载插件的WPF应用程序... [ ^ ],
AppDomain拒绝加载程序集 [
OK, I''ve done this in different variants. Please see my sketch of design here:
Create WPF Application that uses Reloadable Plugins...[^],
AppDomain refuses to load an assembly[^].
Correct way of recognition of plug-ins is by some plug-in interface. Doing it by name is absolutely unsupportable.
—SA
好,我能够找到并调用一个函数.问题.有没有一种方法可以通过整个dll直接调用我的函数"Test"而不是For Each"?
Ok, I was able to find and call a function. Question. Is there a way to directly invoke my function "Test" instead of For Each''ing through the entire dll?
Dim assem As System.Reflection.Assembly =System.Reflection.Assembly.LoadFrom("myplugin.dll")
Dim ty As Type = assem.GetType("myPlugin.frmMain")
Dim class1 As Object = Activator.CreateInstance(ty)
Dim strMsg As String
Dim myMember() As System.Reflection.MethodInfo = class1.GetType.GetMethods()
For Each member As System.Reflection.MethodInfo In myMember
If member.Name = "Test" Then
strMsg = member.Invoke(Nothing, Nothing, Nothing, Nothing, Nothing)
Exit For
End If
Next
这篇关于Vb.net,外部动态dll及其调用各自的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!