Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compile C# code extension at runtime

I have a system that compiles C# code at runtime. I would like the generated assemblies to be linked to the system itself. Here's some example code that I am using:

CSharpCodeProvider provider = new CSharpCodeProvider(new Dictionary<String, String> { { "CompilerVersion", "v3.5" } });
CompilerParameters compilerparams = new CompilerParameters();
compilerparams.GenerateExecutable = false;
compilerparams.GenerateInMemory = true;
foreach (string name in linkedreferences)
    compilerparams.ReferencedAssemblies.Add(name + ".dll");
Assembly result = provider.CompileAssemblyFromFile(compilerparams, filename);

What I would like to do is also add a reference to the main compiler program as well, so the newly compiled extension can use library routines from the compiler program.

Assembly entryasm = Assembly.GetEntryAssembly();

So the question is this: How do I add a reference to entryasm in the compiled Assembly result?

like image 980
Whiteknight Avatar asked Sep 30 '09 14:09

Whiteknight


People also ask

What does it mean to compile in C?

Compiling a C Program. Compiling is the transformation from Source Code (human readable) into machine code (computer executable). A compiler is a program.

How do I compile and run C code?

Step 1: Open turbo C IDE(Integrated Development Environment), click on File and then click on New. Step 2: Write the C program code. Step 3: Click on Compile or press Alt + F9 to compile the code. Step 4: Click on Run or press Ctrl + F9 to run the code.

Why do we compile in C?

Compiling a C program:- Behind the Scenes. C is a mid-level language and it needs a compiler to convert it into an executable code so that the program can be run on our machine.


1 Answers

Have you tried this?

   compilerparams.ReferencedAssemblies.Add(entryasm.Location);
like image 110
Ben M Avatar answered Oct 12 '22 23:10

Ben M