How do I load a Class Library DLL at runtime and run a class function using VB.NET?

允我心安 提交于 2019-12-06 04:05:39

I suggest to make DoIt shared, since it does not require class state:

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

Then calling it is easy:

' 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)

If you cannot make the method shared, you can create an instance of your class with Activator.CreateInstance and pass it as a parameter to Type.InvokeMember.

All my code examples assume Option Strict On and Option Infer On.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!