tags:

views:

79

answers:

2

I'd like to create an assembly in memory, using an using the classes in Reflection.Emit

Currently, I can create the assembly and get it's bytes using something like

AssemblyBuilder builder =
   AppDomain.CurrentDomain.DefineDynamicAssembly(..., 
      AssemblyBuilderAccess.Save);

... create the assembly ...

builder.Save(targetFileName);

using(FileStream fs = File.Open(targetFileName, FileMode.Open))
{
   ... read the bytes from the file stream ...
}

However, it does so by creating a file on the local filesystem. I don't actually need the file, just the bytes that would be in the file.

Is it possible to create the assembly without writing any files?

+2  A: 

This is not possible at this point in time. The C# team is planning a "compiler as a service" feature that hopefully will be included in C# 5.0.

Stephen Cleary
A: 

How about calling AssemblyBuilder.GetObjectData which gives you the serialized assembly data, rather than looking for the bytes? This is probably good enough.

MSDN: http://msdn.microsoft.com/en-us/library/system.reflection.assembly.getobjectdata.aspx

Assembly..::.GetObjectData Method
Gets serialization information with all of the data needed to reinstantiate this assembly.

code4life