/*public CompilationResult Compile(RelativeFileInfo fileInfo, string compilationContent) * { * if (fileInfo == null) * { * throw new ArgumentNullException(nameof(fileInfo)); * } * * if (compilationContent == null) * { * throw new ArgumentNullException(nameof(compilationContent)); * } * * var assemblyName = Path.GetRandomFileName(); * * var sourceText = SourceText.From(compilationContent, Encoding.UTF8); * var syntaxTree = CSharpSyntaxTree.ParseText( * sourceText, * path: assemblyName, * options: _parseOptions); * * var compilation = CSharpCompilation.Create( * assemblyName, * options: _compilationOptions, * syntaxTrees: new[] { syntaxTree }, * references: CompilationReferences); * * compilation = Rewrite(compilation); * * var compilationContext = new RoslynCompilationContext(compilation); * _compilationCallback(compilationContext); * compilation = compilationContext.Compilation; * * using (var assemblyStream = new MemoryStream()) * { * using (var pdbStream = new MemoryStream()) * { * var result = compilation.Emit( * assemblyStream, * pdbStream, * options: new EmitOptions(debugInformationFormat: _pdbFormat)); * * if (!result.Success) * { * if (!compilation.References.Any() && !CompilationReferences.Any()) * { * throw new Exception("TODO: Fail!"); * } * * return GetCompilationFailedResult( * fileInfo.RelativePath, * compilationContent, * assemblyName, * result.Diagnostics); * } * * assemblyStream.Seek(0, SeekOrigin.Begin); * pdbStream.Seek(0, SeekOrigin.Begin); * * var assembly = LoadStream(assemblyStream, pdbStream); * var type = assembly.GetExportedTypes().FirstOrDefault(a => !a.IsNested); * * return new CompilationResult(type); * } * } * } * * /// <summary> * /// Internal for unit testing * /// </summary> * /// <param name="relativePath">The relative path</param> * /// <param name="compilationContent">The compilation content</param> * /// <param name="assemblyName">The assemblyName</param> * /// <param name="diagnostics">The diagnostics</param> * /// <returns>The compilation result</returns> * internal CompilationResult GetCompilationFailedResult( * string relativePath, * string compilationContent, * string assemblyName, * IEnumerable<Diagnostic> diagnostics) * { * var diagnosticGroups = diagnostics * .Where(IsError) * .GroupBy(diagnostic => GetFilePath(relativePath, diagnostic), StringComparer.Ordinal); * * var failures = new List<CompilationFailure>(); * foreach (var group in diagnosticGroups) * { * var sourceFilePath = group.Key; * string sourceFileContent; * if (string.Equals(assemblyName, sourceFilePath, StringComparison.Ordinal)) * { * // The error is in the generated code and does not have a mapping line pragma * sourceFileContent = compilationContent; * } * else * { * sourceFileContent = ReadFileContentsSafely(_fileProvider, sourceFilePath); * } * * var compilationFailure = new CompilationFailure( * sourceFilePath, * sourceFileContent, * compilationContent, * group.Select(GetDiagnosticMessage)); * * failures.Add(compilationFailure); * } * * return new CompilationResult(failures); * } */ /// <summary> /// Gets the sequence of <see cref="MetadataReference"/> instances used for compilation. /// </summary> /// <returns>The <see cref="MetadataReference"/> instances.</returns> protected virtual IList <MetadataReference> GetCompilationReferences() { var applicationReferences = new MetadataReference[] { MetadataReference.CreateFromFile(typeof(object).Assembly.Location), MetadataReference.CreateFromFile(typeof(Console).Assembly.Location), MetadataReference.CreateFromFile(typeof(System.Runtime.AssemblyTargetedPatchBandAttribute).Assembly.Location), MetadataReference.CreateFromFile(typeof(Microsoft.CSharp.RuntimeBinder.CSharpArgumentInfo).Assembly.Location), }; if (_additionalMetadataReferences.Count == 0 && ServiceReferences.Count == 0) { return(applicationReferences); } var compilationReferences = new List <MetadataReference>(applicationReferences.Count() + _additionalMetadataReferences.Count + ServiceReferences.Count); compilationReferences.AddRange(applicationReferences); compilationReferences.AddRange(_additionalMetadataReferences); compilationReferences.AddRange(ServiceReferences.Values); return(compilationReferences); }