如何将“类项目”中的 dll 嵌入到我在 vb.net 中的项目中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10025127/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to embed dll from "class project" into my project in vb.net
提问by Alex
I have a standard "class library" project with a set of classes that I use to import in almost all my new projects.
我有一个标准的“类库”项目,其中包含一组用于导入几乎所有新项目的类。
The way I work is creating a new Solution with an empty project, which is my main project, and then I add to the solution the mentioned class library project, this way I can see both projects in the Soluction Explorer and even see the library code or update it if needed. Then I write the code in my main project and I compile.
我的工作方式是用一个空项目创建一个新的解决方案,这是我的主项目,然后我将提到的类库项目添加到解决方案中,这样我就可以在 Soluction Explorer 中看到这两个项目,甚至可以看到库代码或在需要时更新它。然后我在我的主项目中编写代码并编译。
This lead me to have 2 files when I compile: file *.exe
and stdlib.dll
这导致我在编译时有 2 个文件: file*.exe
和stdlib.dll
Some cases I use the lib for very small tools that I want to redistribute in a easy and clean why, so I would like to embed the stdlib.dll
generated from my class library project into my *.exe
file.
在某些情况下,我将 lib 用于非常小的工具,我想以简单明了的方式重新分发这些工具,因此我想stdlib.dll
将从我的类库项目生成的嵌入到我的*.exe
文件中。
I'm pretty sure there must be a why to do this in my Microsoft Visual Basic 2010 Express but I don't know how.
我很确定为什么在我的 Microsoft Visual Basic 2010 Express 中这样做,但我不知道如何。
Any Suggestion?
有什么建议吗?
回答by Alex Essilfie
Here is a more 'step-by-step' version of Alex's procedureto embedding the assembly.
- Add the desired assembly (stdlib.dll) to the project's resources.
Go to the Resourcestab of the Project Properties and choose Add Resource > Add Existing File... - Switch to the Applicationtab and click on the View Application Eventsbutton.
Add this code to the ApplicationEvents.vbcode that opens.
Private Sub AppStart(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.StartupEventArgs) Handles Me.Startup AddHandler AppDomain.CurrentDomain.AssemblyResolve, AddressOf ResolveAssemblies End Sub Private Function ResolveAssemblies(sender As Object, e As System.ResolveEventArgs) As Reflection.Assembly Dim desiredAssembly = New Reflection.AssemblyName(e.Name) If desiredAssembly.Name = "the name of your assembly" Then Return Reflection.Assembly.Load(My.Resources.STDLIB) 'replace with your assembly's resource name Else Return Nothing End If End Function
Now compile your project and you'll have the dependent assembly incorporated into the output as a single file.
- 将所需的程序集 (stdlib.dll) 添加到项目资源中。
转到“项目属性”的“资源”选项卡,然后选择“添加资源”>“添加现有文件...” - 切换到应用程序选项卡并单击查看应用程序事件按钮。
将此代码添加到打开的ApplicationEvents.vb代码中。
Private Sub AppStart(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.StartupEventArgs) Handles Me.Startup AddHandler AppDomain.CurrentDomain.AssemblyResolve, AddressOf ResolveAssemblies End Sub Private Function ResolveAssemblies(sender As Object, e As System.ResolveEventArgs) As Reflection.Assembly Dim desiredAssembly = New Reflection.AssemblyName(e.Name) If desiredAssembly.Name = "the name of your assembly" Then Return Reflection.Assembly.Load(My.Resources.STDLIB) 'replace with your assembly's resource name Else Return Nothing End If End Function
现在编译您的项目,您会将依赖程序集作为单个文件合并到输出中。
Note that sometimes you may have the dependent assembly in the output folder. This is because VS is preconfigured to copy all dependent assemblies to the output path. You can override this by going to the Referencestab of the project's properties and then set the Copy Localproperty of the dependent assembly to False. This will stop the assembly from being copied to the output directory.
请注意,有时您可能在输出文件夹中有依赖程序集。这是因为 VS 已预先配置为将所有依赖程序集复制到输出路径。您可以通过转到项目属性的References选项卡,然后将依赖程序集的Copy Local属性设置为False来覆盖它。这将阻止程序集被复制到输出目录。
回答by Alex
You can embedd your Assembly
(.dll in your case) into your project by selecting "Add existing file" and then change the Build Option to "Embedded Ressource".
您可以Assembly
通过选择“添加现有文件”将您的(在您的情况下为 .dll)嵌入到您的项目中,然后将构建选项更改为“嵌入式资源”。
You then add a Handler
for the AppDomain.CurrentDomain.AssemblyResolve
event which gets fired as soon as you first access the library inside your code.
然后Handler
为该AppDomain.CurrentDomain.AssemblyResolve
事件添加一个,一旦您第一次访问代码中的库,该事件就会被触发。
That handler code looks like this: (Notice the fully qualified assembly path inclusive correct namespacs. I'd wrap it in a function which gets called on startup of your application.
该处理程序代码如下所示:(注意包含正确命名空间的完全限定程序集路径。我会将它包装在一个函数中,该函数在您的应用程序启动时被调用。
AddHandler AppDomain.CurrentDomain.AssemblyResolve,
Function(sender As Object, args As System.ResolveEventArgs) As System.Reflection.Assembly
Dim ressourceName = "YourNamespace.YourSubNamespace." + New AssemblyName(args.Name).Name + ".dll"
Using stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(ressourceName)
Dim assemblyData(CInt(stream.Length)) As Byte
stream.Read(assemblyData, 0, assemblyData.Length)
Return Assembly.Load(assemblyData)
End Using
End Function
You can then deploy your tool without any additional files.
然后,您无需任何其他文件即可部署您的工具。