我需要在代码中使用外部DLL,而不使用项目引用。我正在尝试在我的项目范围内使用声明函数,但是它不起作用。如果我将项目引用用于DLL,则可以使用,但不能将其用于交付。
问题是,我需要为NotInheritable Class声明函数,而当我在自己的范围内清除时,我无法进入该类。 DLL位于与我的应用程序相同的路径中,当前位于BIN \ Debug项目中。
我的库是libfacturista,不可继承的类是ansiApi:
Namespace libfacturista
Public NotInheritable Class ansiApi
Public Shared Sub init()
...
End Class
End Namespace
使用Porject参考代码可以正常工作,:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
libfacturista.ansiApi.init()
End Sub
但是当我在作用域上使用声明时,即使将类命名为前缀,也无法正常工作。
Declare Sub init Lib "libfacturista_cs.dll" Alias "init" ()
'Declare Sub init Lib "libfacturista_cs.dll" Alias "ansiApi.init" ()
Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
init()
Sub
无论如何,我是否可以在作用域上声明功能,因此我不必使用项目引用?如何进入NotInheritable类?
谢谢。问候
如果您没有在项目中引用程序集,则需要在运行时加载它。
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'Load the external assembly (dll)
Dim externalAssembly As Reflection.Assembly = Reflection.Assembly.LoadFrom("libfacturista.dll")
'Get the specific class that you are interested in
Dim externalClass As Type = externalAssembly.GetType("libfacturista.ansiApi")
'Create an instance of that class
Dim instance As Object = Activator.CreateInstance(externalClass)
'Access public methods from the class (methods will not show in intellisense)
instance.init()
End Sub