I've been looking around a bit on the web now for information about how to embed resources into .Net assemblies, and everywhere, everything I find is about Reflection.Emit and about dynamic assemblies.
I want to make an application which creates as it's output another application. This output application should always contain the same code, but different resources - so it's conceptually the same as a self-extracting zip archive. In this, the zip program would not want to programmatically build the entire executable - it would want to use a predefined program and simply embed the zipped data as a resource; precisely what I want to do (except the resource isn't zipped data but other binary data).
From my research so far it seems I need to use an AssemblyBuilder instance to add the resources at run-time. But it seems the only way to instantiate it is to call AppDomain.DefineDynamicAssembly, which takes an AssemblyName as parameter. Its not clear (to me anyway) from the documenation whether this AssemblyName can represent an existing assembly (I'd like to simply load the EXE file with the program, or alternatively a DLL and set the entry point), but I suspect the method creates a new assembly rather than load an existing one, which can then be modified.
The point is to get a single file that is self-contained and can stand alone. I shouldn't have to emit all the program code using Reflection.Emit just because I want to use Reflection.Emit to add resource files.
Does anyone know how to do this

Embedding resources in a static assembly (Reflection.Emit?)
Yozz
Here is a simple way to embed a resource in an assembly. Following code will create an assembly named Embeded.dll which contains an embeded resource called Test.bin where Test.bin is any resource that you want to embed like say a text file, an image or whatever you can think of. This is a demo code so there are no sanity checks whatsoever; consider yourself warned ;-)
using
System;using System.IO;
using
System.Reflection;using
System.Reflection.Emit;using
System.Resources;using
System.Threading;public
class EmbedRes{
public static string Directory{
get{ return Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); }}
public static string OutputDirectory{
get{ return Directory + @"\Output"; }}
public static void Main(string[] args){
string asmFileName = "Embeded.dll";AppDomain currDomain = Thread.GetDomain();
AssemblyName asmName =
new AssemblyName();asmName.Name = "Embeded";
AssemblyBuilder asmBuilder = currDomain.DefineDynamicAssembly(asmName,
AssemblyBuilderAccess.Save, OutputDirectory);
ModuleBuilder modBuilder = asmBuilder.DefineDynamicModule(asmFileName, asmFileName);
ResourceWriter rw = modBuilder.DefineResource("Test.bin", "Test.bin")
as ResourceWriter; rw.AddResource("Test.bin", GetFile());asmBuilder.Save(asmFileName);
}
public static byte[] GetFile(){
string fileName = @"C:\Dev\Dotnet\Test.bin"; byte[] buff = null; using(FileStream stm = new FileStream(fileName, FileMode.Open, FileAccess.Read)){
buff =
new byte[stm.Length];stm.Read(buff, 0, (
int)stm.Length);}
return buff;}
}
The key in the above code is that the name passed to ModuleBuilder.DefineResource and ResourceWriter.AddResource methods must be exactly the same.
Hope that helps!
Gogou
So what do I need to use then, if not Reflection.Emit
Shrikant24227
Hi,
unfortunately you made the same mistake as everyone before you who replied. I appreciate the effort, but this answers a different question than the one that was asked. I want to MODIFY an existing assembly loaded from a file, NOT create a new assembly.
Thanks for the effort. If you do know the answer to my question, please let me know.
Sowbhagya
Please try to use ModuleBuilder.DefineManifestResource.
talay
I'm seeking the same exact answer, and reached the same exact conclusions.
Have you come up with anything yet
Pyush Kumar
Hi,
so how do I get an instance of the ModuleBuilder It seems I have to instantiate an AssemblyBuilder to get a ModuleBuilder, so the problem is the same as I've stated already; this only works for creating a dynamic assembly. I want to load an existing assembly and modify it. (I don't know if one can define a dynamic assembly from loading a static one, but I can't really see why not; "dynamic" after all only means it can be modified.)
Thanks for trying though!
Dave Corun
Reflection.Emit does not support modifying existing assembly, I am afraid.
sk49
try RAIL (Runtime Assembly Instrumentation Library)
http://rail.dei.uc.pt/