public async virtual Task <MemoryStream> CreateAssembly(string code) { _mscorlib = _mscorlib ?? MetadataReference.CreateFromStream(new MemoryStream(await _mscorlibProvider.Get())); CSharpCompilation previousCompilation = null; var scriptCompilation = CSharpCompilation.CreateScriptCompilation(Guid.NewGuid().ToString(), CSharpSyntaxTree.ParseText(code, CSharpParseOptions.Default.WithKind(SourceCodeKind.Script).WithLanguageVersion(LanguageVersion.Latest)), references: new[] { _mscorlib }, new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary, optimizationLevel: OptimizationLevel.Release, concurrentBuild: false, usings: new[] { "System", "System.Threading.Tasks" }), previousCompilation); var errorDiagnostics = scriptCompilation.GetDiagnostics().Where(x => x.Severity == DiagnosticSeverity.Error); if (errorDiagnostics.Any()) { Console.WriteLine(string.Join(",\r\n", errorDiagnostics.Select(e => e.GetMessage()))); return(null); } var stream = new MemoryStream(); if (scriptCompilation.Emit(stream).Success) { return(await Task.FromResult(stream)); } return(null); }
public async Task <ActionResult> Index() { var assembly = await _mscorlibProvider.Get(); return(File(assembly.ToArray(), "application/octet-stream")); }