Skip to content

Instantly share code, notes, and snippets.

@nesteruk
Created August 23, 2012 11:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save nesteruk/3435834 to your computer and use it in GitHub Desktop.
Save nesteruk/3435834 to your computer and use it in GitHub Desktop.
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)
sb.AppendLine(e.ToString());
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");
else
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())
{
try
{
ps.ReferencedAssemblies.Add(reference.Location);
}
catch (Exception ex)
{
string s = "Cannot add assembly " + reference.FullName + " as reference.";
Debug.WriteLine(s);
}
}
return ps;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment