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

发布于 2024-12-06 03:10:35 字数 267 浏览 1 评论 0原文

我的项目树中有两个 dll 文件(不是引用)。它们作为链接添加,它们是解决方案中其他项目的组件。我正在尝试将其构建操作设置为嵌入式资源,以便我可以将它们导入到 .exe 文件中。我无法编写 using 语句,因此无法在当前项目中引用它们。那怎么办呢?

项目文件

I have two dll files in the project tree(not the references). 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. I can't write using statement, so I can't reference them in current project. How can that be done?

project file

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

喜爱纠缠 2024-12-13 03:10:35

您需要添加对程序集的硬引用并将其 Copy Local 设置为 False,然后在调用程序集之前将程序集从嵌入式资源提取到应用程序目录。您无法像您想要的那样引用链接(快捷方式)。

要点(在此示例中)和 带有示例代码的博客文章

  • EmbeddedReferenceApplication 硬引用 EmbeddedReference.dll
  • EmbeddedReference 引用属性“复制本地”设置为 False
  • 链接程序集(添加为链接)设置为“嵌入”资源

这是一个工作示例。 (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);
                }
            }
        }
    }
}

在 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!");
        }
    }
}

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. 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
  • EmbeddedReference reference property Copy Local is set to False
  • Linked assembly (Add as Link) is set as Embedded Resource

Here is a working example. (EmbeddedReferenceApplication.exe | Console Application)

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

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

namespace EmbeddedReference {
    public static class MessageHelper {
        public static void ShowMessage() {
            Console.WriteLine("Hello World!");
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文