[英]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 关键点(在此示例中)和博客文章与示例代码
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.