private static CompileToAssemblyResult CompileToAssembly(CompileToCSharpResult cSharpResult) { if (cSharpResult.Diagnostics.Any(d => d.Severity == DiagnosticSeverity.Error)) { return(new CompileToAssemblyResult { Diagnostics = cSharpResult.Diagnostics, }); } var syntaxTrees = new[] { CSharpSyntaxTree.ParseText(cSharpResult.Code, cSharpParseOptions) }; var compilation = cSharpResult.BaseCompilation.AddSyntaxTrees(syntaxTrees); var diagnostics = compilation .GetDiagnostics() .Where(d => d.Severity > DiagnosticSeverity.Info) .ToList(); var result = new CompileToAssemblyResult { Compilation = compilation, Diagnostics = diagnostics.Select(CompilationDiagnostic.FromCSharpDiagnostic).Concat(cSharpResult.Diagnostics).ToList(), }; if (result.Diagnostics.All(x => x.Severity != DiagnosticSeverity.Error)) { using var peStream = new MemoryStream(); compilation.Emit(peStream); result.AssemblyBytes = peStream.ToArray(); } return(result); }
private static CompileToAssemblyResult CompileToAssembly(IReadOnlyList <CompileToCSharpResult> cSharpResults) { if (cSharpResults.Any(r => r.Diagnostics.Any(d => d.Severity == DiagnosticSeverity.Error))) { return(new CompileToAssemblyResult { Diagnostics = cSharpResults.SelectMany(r => r.Diagnostics).ToList() }); } var syntaxTrees = new SyntaxTree[cSharpResults.Count]; for (var i = 0; i < cSharpResults.Count; i++) { var cSharpResult = cSharpResults[i]; syntaxTrees[i] = CSharpSyntaxTree.ParseText(cSharpResult.Code, cSharpParseOptions, cSharpResult.FilePath); } var finalCompilation = baseCompilation.AddSyntaxTrees(syntaxTrees); var compilationDiagnostics = finalCompilation.GetDiagnostics().Where(d => d.Severity > DiagnosticSeverity.Info); var result = new CompileToAssemblyResult { Compilation = finalCompilation, Diagnostics = compilationDiagnostics .Select(CompilationDiagnostic.FromCSharpDiagnostic) .Concat(cSharpResults.SelectMany(r => r.Diagnostics)) .ToList(), }; if (result.Diagnostics.All(x => x.Severity != DiagnosticSeverity.Error)) { using var peStream = new MemoryStream(); finalCompilation.Emit(peStream); result.AssemblyBytes = peStream.ToArray(); } return(result); }