Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
C# in-process compiler
using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Text;
using Microsoft.CSharp;
namespace ActiveMesa.R2P.Infrastructure
internal class CSharpInProcessCompiler
internal Assembly Compile(string sourceCode, string compilerVersion = "v4.0")
CompilerParameters ps = PrepareCompilerParameters();
var po = new Dictionary<string, string>
{"CompilerVersion", compilerVersion}
var p = new CSharpCodeProvider(po);
var results = p.CompileAssemblyFromSource(ps, new[] {sourceCode});
if (results.Errors.HasErrors)
var sb = new StringBuilder();
foreach (var e in results.Errors)
throw new Exception(sb.ToString());
return results.CompiledAssembly;
internal Type CompileAndGetType(string sourceCode, string compilerVersion = "v4.0")
var ass = Compile(sourceCode, compilerVersion);
var types = ass.GetTypes();
if (types == null || types.Length == 0)
throw new Exception("Compiled assembly produced no types");
return types[0];
internal object CompileAndInstantiate(string sourceCode, string compilerVersion = "v4.0")
var mainType = CompileAndGetType(sourceCode, compilerVersion);
return Activator.CreateInstance(mainType);
private CompilerParameters PrepareCompilerParameters()
var ps = new CompilerParameters {GenerateInMemory = true, GenerateExecutable = false};
// add everything in this AppDomain
foreach (var reference in AppDomain.CurrentDomain.GetAssemblies())
catch (Exception ex)
string s = "Cannot add assembly " + reference.FullName + " as reference.";
return ps;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.