简体   繁体   English

在项目中“添加为链接”时如何引用dll?

[英]How do I reference dlls when “added as link” in project?

I have two dll files in the project tree(not the references). 我在项目树中有两个dll文件(不是引用)。 They are added as link, they are assemblies of other project in solution. 它们作为链接添加,它们是解决方案中其他项目的程序集。 I'm trying to set their Build Action to Embedded Resource, so I can import them to .exe file. 我正在尝试将Build Action设置为Embedded Resource,因此我可以将它们导入.exe文件。 I can't write using statement, so I can't reference them in current project. 我不能用using语句写,所以我不能在当前项目中引用它们。 How can that be done? 怎么办?

项目文件

You need to add a hard reference to the assemblies and set their Copy Local to False, then extract the assemblies from your embedded resources to the application directory before they are invoked. 您需要向程序集添加硬引用并将其Copy Local设置为False,然后在调用程序集之前将程序集从嵌入资源中提取到应用程序目录。 You can't reference a linked (shortcut) like you want. 您无法像想要的那样引用链接(快捷方式)。

Key Points (in this example) and the Blog Article with Example Code 关键点(在此示例中)和博客文章与示例代码

  • EmbeddedReferenceApplication hard references EmbeddedReference.dll EmbeddedReferenceApplication硬引用EmbeddedReference.dll
  • EmbeddedReference reference property Copy Local is set to False EmbeddedReference引用属性Copy Local设置为False
  • Linked assembly (Add as Link) is set as Embedded Resource 链接的程序集(Add as Link)设置为Embedded Resource

Here is a working example. 这是一个有效的例子。 (EmbeddedReferenceApplication.exe | Console Application) (EmbeddedReferenceApplication.exe |控制台应用程序)

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Windows.Forms;
using System.Reflection;
using EmbeddedReference; // Hard reference with Copy Local = False

namespace EmbeddedReferenceApplication {
    class Program {
        static void Main(string[] args) {
            AppDomain.CurrentDomain.AssemblyResolve += AppDomain_AssemblyResolve;
            MyMain();
        }

        private static void MyMain() {
            EmbeddedReference.MessageHelper.ShowMessage();
        }

        private static Assembly AppDomain_AssemblyResolve(object sender, ResolveEventArgs args) {
            string manifestResourceName = "EmbeddedReferenceApplication.EmbeddedReference.dll"; // You can also do Assembly.GetExecutingAssembly().GetManifestResourceNames();
            string path = Path.Combine(Application.StartupPath, manifestResourceName.Replace("EmbeddedReferenceApplication.", ""));
            ExtractEmbeddedAssembly(manifestResourceName, path);
            Assembly resolvedAssembly = Assembly.LoadFile(path);
            return resolvedAssembly;
        }

        private static void ExtractEmbeddedAssembly(string manifestResourceName, string path) {
            Assembly assembly = Assembly.GetExecutingAssembly();
            using (Stream stream = assembly.GetManifestResourceStream(manifestResourceName)) {
                byte[] buffer = new byte[stream.Length];
                stream.Read(buffer, 0, buffer.Length);
                using (FileStream fstream = new FileStream(path, FileMode.Create)) {
                    fstream.Write(buffer, 0, buffer.Length);
                }
            }
        }
    }
}

In EmbeddedReference.dll 在EmbeddedReference.dll中

using System;
using System.Collections.Generic;
using System.Text;

namespace EmbeddedReference {
    public static class MessageHelper {
        public static void ShowMessage() {
            Console.WriteLine("Hello World!");
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将 dll 作为引用包含在 nuget 中,当 nuget 添加到项目时,该引用也被复制为引用 - How do I include dll as reference in nuget that also gets copied as reference when nuget added to project 当我在Visual Studio中创建项目引用时,我是否必须在部署时包含这些项目的DLL? - When I make a project reference in Visual Studio, do I then have to include the DLLs for those projects when I deploy? 当项目需要引用其他dll时 - when project needs reference to other dlls 如何运行时链接“插件”参考DLL - How to runtime link “plugin” reference dlls 如何选择与项目相关的IKVM dll子集? - How do I choose the subset of IKVM dlls that are relevant to my project? 将引用添加到项目时的事件 - Event when a reference is added to a project 为什么已经从另一个项目添加了此引用,为什么还要添加相同的引用 - Why I need to add same reference when this reference had been added from another project 添加添加服务参考项目参考时 - When add service reference project references are added 当它似乎被添加到项目时,引用丢失的错误 - Error that reference is missing when it seems to be added to project 我添加了一个项目作为参考,但仍然找不到 - I added a project as reference but it still cannot be found
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM