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

前端 未结 2 531
盖世英雄少女心
盖世英雄少女心 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条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-06 08:06

    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".

    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.

    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.

提交回复
热议问题