Using AssemblyBuilder, how can I make all or any o

2019-04-15 01:55发布

问题:

I have an executing assembly, which generates another assembly dynamically using AssemblyBuilder.

The generated assembly consists of functions which simply test the construction of certain classes in the executing assembly. Since the functions in the generated assembly reference classes in the executing assembly, I want to have the executing assembly embed itself in the assembly it creates.

So far, I've only managed to generate an assembly that links to the executing assembly, but not one that embeds it. It's important that I have just one final assembly (.dll).

Do I need to embed the assembly as a resource? If so, how? I don't want to have to write code to load the embedded assembly dynamically, unless that's the only way. I'm looking for something easy like AssemblyBuilder.EmbedAssembly( GetExecutingAssembly() ).

回答1:

There's no easy way. You have at least two choices:

  • Embed the referenced assemblies as managed resources, and emit an assembly load hook that will load the assemblies from the resource stream on demand.
  • Use a post emit step using ILMerge to merge the assemblies all together.