I'm trying to compile code at runtime that is referenced by an object that is also compiled at run-time in C# .NET 4.6.
I'm using CSharpCodeProvider to generate a CompilerResult in memory. The resulting assembly is referenced in the 2nd code fragment that is compiled. Is it possible to add the AssemblyReference to the CompilerParameters before I compile the 2nd piece (otherwise I'd get a missing assembly compiler error).
Currently I see two options:
So my question: Is there an in-memory way to reference a run-time generated assembly in another run-time generated assembly?
CSharpCodeProvider
works on a disk anyway - even when you only generate the assembly "in-memory", it compiles the assembly on disk and loads it to memory (it has to, kind of - all it does is call csc.exe
). The only difference is that the assembly file is a temporary DLL somewhere, rather than a file you specified.
If you want true in-memory compilation of C# code, use the Roslyn compiler.