Sebastian Gulinski
Sebastian Gulinski

Reputation: 11

How to create at runtime a .NET exe file assembly from already compiled classes

I need to create executable file assembly at runtime. My main goal is to compile types from the actual Project (.dll) performing compilation task, without using strings keeping the code that needs to be compiled to the output exe file. Maintaining those strings during code development/refactoring would be a nightmare. As shown in the example below, when I refactor class CodeToCompile, I also need to remember to change string codeToCompile in RuntimeCompiler.Compile() method:

using System;
using System.CodeDom.Compiler;

using Microsoft.CSharp;

namespace RuntimeCompiler
{
    public class Compiler
    {
        public void Compile()
        {
            string codeToCompile = 
                @"public class CodeToCompile
                {
                    public static void Main(string[] args)
                    {
                        Console.ReadLine();
                    }
                }";

            CSharpCodeProvider codeProvider = new CSharpCodeProvider();
            ICodeCompiler icc = codeProvider.CreateCompiler();
            CompilerParameters parameters = new CompilerParameters
                                                {
                                                    GenerateExecutable = true,
                                                    OutputAssembly = "Generated.exe"
                                                };

            CompilerResults results = icc.CompileAssemblyFromSource(parameters, codeToCompile);
        }

    }

    public class CodeToCompile
    {
        public static void Main(string[] args)
        {
            Console.ReadLine();
        }
    }
}

What I would like to achive is changing the string with code that needs to be compiled to reference of the type that needs to be compiled into .exe assembly.

string codeToCompile = "..."; //CodeToCompile source code
CompilerResults results = icc.CompileAssemblyFromSource(parameters, codeToCompile);

CompilerResults results = icc.CompileAssemblyFromSource(parameters, typeof(CodeToCompile));

Any ideas what would be the best way to achieve this?

I have easily found information how to dynamically compile C# code from string using System.CodeDom.Compiler namespace, as shown above. Thus any method to get a runtime object uncompiled code as a string would do the job. My research in this direction did not give me solution and I strongly doubt that it is possible to do in an easy way.

My second idea is to use mechanism to "move" compiled type from the running assembly to a newly created one, but unfortunately I also haven't found any solution to do this task.

Upvotes: 1

Views: 1479

Answers (1)

user585968
user585968

Reputation:

...without using strings keeping the code that needs to be compiled to output exe file. Maintaining those strings during code development/refactoring would be a nightmare...

My advice here is to look into .NET Globalization

Globalization involves designing and developing a world-ready app that supports localized interfaces and regional data for users in multiple cultures...The handling of characters and strings is a central focus of globalization, because each culture or region may use different characters and character sets and sort them differently. More...

An alternative to CodeDom.Compiler is .NET's Reflection Emit:

...the System.Reflection.Emit namespace that allow a compiler or tool to emit metadata and Microsoft intermediate language (MSIL) at run time and optionally generate a portable executable (PE) file on disk. Script engines and compilers are the primary users of this namespace. More...

Upvotes: 2

Related Questions