如何在运行时加载类库DLL并使用VB.NET运行类函数?

如何在运行时加载类库DLL并使用VB.NET运行类函数?,vb.net,dll,runtime,.net-assembly,Vb.net,Dll,Runtime,.net Assembly,假设我在类库项目SomeClass中有这样一个类- Public Class SomeClass Public Function DoIt() as String Return "Do What?" End Function End Class 我得到一个SomeClass.dll,我想在运行时从另一个Windows窗体应用程序加载它,然后让它调用DoIt()函数,并在messagebox或其他东西中显示它的值。我该怎么做?我建议将DoIt共享,因为它不需要类状态: Publi

假设我在类库项目SomeClass中有这样一个类-

Public Class SomeClass
  Public Function DoIt() as String
    Return "Do What?"
  End Function
End Class

我得到一个
SomeClass.dll
,我想在运行时从另一个Windows窗体应用程序加载它,然后让它调用
DoIt()
函数,并在messagebox或其他东西中显示它的值。我该怎么做?

我建议将
DoIt
共享,因为它不需要类状态:

Public Class SomeClass
    Public Shared Function DoIt() as String
        Return "Do What?"
    End Function
End Class
那么说起来很容易:

' Loads SomeClass.dll
Dim asm = Assembly.Load("SomeClass")  

' Replace the first SomeClass with the base namespace of your SomeClass assembly
Dim type = asm.GetType("SomeClass.SomeClass")

Dim returnValue = DirectCast(type.InvokeMember("DoIt", _
                                               BindingFlags.InvokeMethod | BindingFlags.Static, _
                                               Nothing, Nothing, {}),
                             String)
如果无法共享该方法,则可以使用创建类的实例,并将其作为参数传递给

我所有的代码示例都假定Option Strict On和Option Inferre On