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?
1 Answer
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!");
}
}
}
-
I can't even run it when it's not copied. Can you provide an example? I need to get the dll dynamically somehow. It has to be build everytime. Commented Sep 21, 2011 at 20:34
-
You would have to extract the assemblies before any code calls on them, the best place to do this would be in your Program.cs class before any code is run. Otherwise when your program runs a line of code that makes a call that references that assembly and it cannot be resolved, your application will crash. Commented Sep 21, 2011 at 20:38
-
-
I will write a blog post on it with a Visual Studio solution for download and post the article back here when I'm done. Commented Sep 21, 2011 at 20:51
-
1You add a hard reference to the actual DLL itself from the same location you added it as a link. You are just setting Copy Local to false so its not physically copied on disk. Your added as link copies set to embedded resources are what will ensure they get compiled into the output binary. Take a look at the blog if you havn't and download the example project, it is a fully functional example Commented Sep 22, 2011 at 7:11