如何从“类项目"中嵌入dll进入我在 vb.net 中的项目 [英] How to embed dll from "class project" into my project in vb.net

查看:14
本文介绍了如何从“类项目"中嵌入dll进入我在 vb.net 中的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个标准的类库"项目,其中包含一组我用来导入几乎所有新项目的类.

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.

这导致我在编译时有 2 个文件:文件 *.exestdlib.dll

This lead me to have 2 files when I compile: file *.exe and stdlib.dll

在某些情况下,我将 lib 用于非常小的工具,我想以简单明了的方式重新分发这些工具,所以我想将从我的类库项目生成的 stdlib.dll 嵌入到我的*.exe 文件.

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.

我很确定为什么要在我的 Microsoft Visual Basic 2010 Express 中执行此操作,但我不知道如何操作.

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.

有什么建议吗?

推荐答案

这里是 的分步"版本Alex 嵌入程序集的过程.

  1. 将所需的程序集 (stdlib.dll) 添加到项目资源中.
    转到项目属性的资源选项卡,然后选择添加资源>添加现有文件...
  2. 切换到应用程序标签并点击查看应用程序事件按钮.
  3. 将此代码添加到打开的 ApplicationEvents.vb 代码中.

  1. Add the desired assembly (stdlib.dll) to the project's resources.
    Go to the Resources tab of the Project Properties and choose Add Resource > Add Existing File...
  2. Switch to the Application tab and click on the View Application Events button.
  3. Add this code to the ApplicationEvents.vb code 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.

    请注意,有时您可能在输出文件夹中有依赖程序集.这是因为 VS 已预先配置为将所有依赖程序集复制到输出路径.您可以通过转到项目属性的引用 选项卡,然后将依赖程序集的Copy Local 属性设置为False 来覆盖它.这将阻止程序集被复制到输出目录.

    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 References tab of the project's properties and then set the Copy Local property of the dependent assembly to False. This will stop the assembly from being copied to the output directory.

    这篇关于如何从“类项目"中嵌入dll进入我在 vb.net 中的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆