How to embed dll from “class project” into my project in vb.net

前端 未结 2 547
盖世英雄少女心
盖世英雄少女心 2020-12-06 07:39

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

2条回答
  •  萌比男神i
    2020-12-06 08:03

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

    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 
      
    4. Now compile your project and you'll have the dependent assembly incorporated into the output as a single file.

    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.

提交回复
热议问题