第一步:设置dll引用属性如下图

第二步,将dll作为项目资源添加进来


第三步,最为重要,添加如下代码到form1开始的地方
Public Class Form1
Public Sub New()
''加载DLL到exe的事件
AddHandler AppDomain.CurrentDomain.AssemblyResolve, New ResolveEventHandler(AddressOf CurrentDomain_AssemblyResolve)
InitializeComponent()
End Sub
''' <summary>
''' 把DLL加载到EXE中
''' </summary>
''' <param name="sender"></param>
''' <param name="args"></param>
''' <returns></returns>
Private Function CurrentDomain_AssemblyResolve(sender As Object, args As ResolveEventArgs) As System.Reflection.Assembly
Dim dllName As String = If(args.Name.Contains(","), args.Name.Substring(0, args.Name.IndexOf(","c)), args.Name.Replace(".dll", ""))
dllName = dllName.Replace(".", "_")
If dllName.EndsWith("_resources") Then
Return Nothing
End If
Dim rm As New System.Resources.ResourceManager([GetType].Namespace & ".Resources", System.Reflection.Assembly.GetExecutingAssembly())
Dim bytes As Byte() = DirectCast(rm.GetObject(dllName), Byte())
Return System.Reflection.Assembly.Load(bytes)
End Function
'以下为项目正式的代码
End Class
推荐另外一种基于fody的更简便方法,适用于将所有引用一起打包的情况:https://www.juziyu.cn/2999.html
© 版权声明
文章版权归作者所有,未经允许请勿转载。