Skip to content

Instantly share code, notes, and snippets.

@angelovstanton
Created September 6, 2015 11:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save angelovstanton/9a030ee21c4afea64cc9 to your computer and use it in GitHub Desktop.
Save angelovstanton/9a030ee21c4afea64cc9 to your computer and use it in GitHub Desktop.
public static void Main(string[] args)
{
var sourceCode = @"class HelloKittyPrinter
{
public void Print()
{
System.Console.WriteLine(""Hello Hello Kitty!"");
}
}";
var compiledAssembly = CompileSourceRoslyn(sourceCode);
ExecuteFromAssembly(compiledAssembly);
}
private static Assembly CompileSourceRoslyn(string sourceCode)
{
using (var memoryStream = new MemoryStream())
{
string assemblyFileName = string.Concat(Guid.NewGuid().ToString(), ".dll");
CSharpCompilation compilation = CSharpCompilation.Create(assemblyFileName,
new[] { CSharpSyntaxTree.ParseText(sourceCode) },
new[]
{
MetadataReference.CreateFromFile(typeof(object).Assembly.Location)
},
new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)
);
compilation.Emit(memoryStream);
Assembly assembly = Assembly.Load(memoryStream.GetBuffer());
return assembly;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment