Created
September 6, 2015 11:03
-
-
Save angelovstanton/9a030ee21c4afea64cc9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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