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

查看:235
本文介绍了如何从“类项目”中嵌入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.

我的工作方式是创建一个新的解决方案,一个空的项目,这是我的主要项目,然后我添加到解决方案提到的类库项目,这样我可以在Soluction Explorer中看到这两个项目,甚至可以看到库代码,如果需要可以更新。然后我在我的主要项目中编写代码,然后编译。

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个文件:file * .exe和stdlib.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的程序 a>嵌入程序集。

Here is a more 'step-by-step' version of Alex's procedure to embedding the assembly.


  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已预配置为将所有依赖程序集复制到输出路径。您可以通过转到项目属性的引用选项卡来覆盖此,然后将依赖程序集的复制本地属性设置为 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天全站免登陆