CSharpCompile
Compiles a string into an assembly. .NET 4
Source
public static CompilerResults CSharpCompile(this string code, string dllName = "dynamicCompile", params string[] additionalAssemblies)
{
var csc = new CSharpCodeProvider(new Dictionary<string, string>() { { "CompilerVersion", "v4.0" } });
var parameters = new CompilerParameters
{
ReferencedAssemblies = {
"mscorlib.dll",
"System.Core.dll",
},
OutputAssembly = dllName,
GenerateExecutable = false,
GenerateInMemory = true,
};
parameters.ReferencedAssemblies.AddRange(additionalAssemblies);
return csc.CompileAssemblyFromSource(parameters, code);
}
Example
var results = @"include System;
public class Example1
{
public string Prop { get; set; }
}".CSharpCompile();