views:

348

answers:

1

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  A: 

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

Jb Evain
How do I do the first option? I don't want to use ILMerge, but is that first option what ILMerge accomplishes internally?
Triynko
I added links to the appropriate APIs. That's not what ILMerge does. ILMerge really creates one assembly from a set of other assemblies. The first solution would embed multiple assemblies as resources inside another one.
Jb Evain
How can I emit an assembly load hook into a non-executable dll that just has a single class and some methods? I'm also not sure how to embed the referenced dll as a resource.
Triynko
How would you run the tests if the assembly is non executable ? You need to have a way of launching the test, somehow. Then make sure the assembly load hook is either emitted, or that the code which triggers the test register a general assembly load hook that will look for referenced is the assemblies references.As for embedding the dll, just use the linked API.
Jb Evain
The code which triggers/calls the methods is SQL Server, through the CLR integration feature. The generated DLL is deployed to SQL Server and its static methods are registered as CLR user-defined functions. The UDFs are referenced in check constraints, so you can see that I don't have anywhere to hook anything. Could I emit a method that would run and install such a hook when the dll is first loaded by SQL Server?
Triynko
I think a post emit call to execute ILMerge would be acceptable, but what may be better is if there was a way to just have the executing assembly patch itself with a newly emitted class, and saving with a different filename.
Triynko
There's no way to patch SRE emitted assemblies at runtime. ILMerge is the easiest possible solution, but it requires to be done after you've emit the whole thing.
Jb Evain