public static CompilationResult GetAssemblyFromCompilation( IAssemblyLoadContext loader, Compilation compilation) { EmitResult result; using (var ms = new MemoryStream()) { using (var pdb = new MemoryStream()) { if (PlatformHelper.IsMono) { result = compilation.Emit(ms, pdbStream: null); } else { result = compilation.Emit(ms, pdbStream: pdb); } if (!result.Success) { var formatter = new DiagnosticFormatter(); var errorMessages = result.Diagnostics .Where(IsError) .Select(d => formatter.Format(d)); return CompilationResult.FromErrorMessages(errorMessages); } ms.Seek(0, SeekOrigin.Begin); Assembly assembly; if (PlatformHelper.IsMono) { assembly = loader.LoadStream(ms, assemblySymbols: null); } else { pdb.Seek(0, SeekOrigin.Begin); assembly = loader.LoadStream(ms, pdb); } return CompilationResult.FromAssembly(assembly); } } }
/// <inheritdoc /> public CompilationResult Compile([NotNull] RelativeFileInfo fileInfo, [NotNull] string compilationContent) { var assemblyName = Path.GetRandomFileName(); var compilationSettings = _compilerOptionsProvider.GetCompilationSettings(_environment); var syntaxTree = SyntaxTreeGenerator.Generate(compilationContent, assemblyName, compilationSettings); var references = _applicationReferences.Value; var compilationOptions = compilationSettings.CompilationOptions .WithOutputKind(OutputKind.DynamicallyLinkedLibrary); var compilation = CSharpCompilation.Create(assemblyName, options: compilationOptions, syntaxTrees: new[] { syntaxTree }, references: references); using (var ms = new MemoryStream()) { using (var pdb = new MemoryStream()) { EmitResult result; if (_supportsPdbGeneration.Value) { result = compilation.Emit(ms, pdbStream: pdb); } else { result = compilation.Emit(ms); } if (!result.Success) { return(GetCompilationFailedResult( fileInfo.RelativePath, compilationContent, assemblyName, result.Diagnostics)); } Assembly assembly; ms.Seek(0, SeekOrigin.Begin); if (_supportsPdbGeneration.Value) { pdb.Seek(0, SeekOrigin.Begin); assembly = _loader.LoadStream(ms, pdb); } else { assembly = _loader.LoadStream(ms, assemblySymbols: null); } var type = assembly.GetExportedTypes() .First(t => t.Name.StartsWith(_classPrefix, StringComparison.Ordinal)); return(UncachedCompilationResult.Successful(type, compilationContent)); } } }
public Assembly Load(IAssemblyLoadContext loadContext) { using (var pdbStream = new MemoryStream()) using (var assemblyStream = new MemoryStream()) { IList <ResourceDescription> resources = CompilationContext.Resources; Trace.TraceInformation("[{0}]: Emitting assembly for {1}", GetType().Name, Name); var sw = Stopwatch.StartNew(); EmitResult emitResult = null; if (_supportsPdbGeneration.Value) { emitResult = CompilationContext.Compilation.Emit(assemblyStream, pdbStream: pdbStream, manifestResources: resources); } else { Trace.TraceWarning("PDB generation is not supported on this platform"); emitResult = CompilationContext.Compilation.Emit(assemblyStream, manifestResources: resources); } sw.Stop(); Trace.TraceInformation("[{0}]: Emitted {1} in {2}ms", GetType().Name, Name, sw.ElapsedMilliseconds); var diagnostics = CompilationContext.Diagnostics.Concat( emitResult.Diagnostics); if (!emitResult.Success || diagnostics.Any(RoslynDiagnosticUtilities.IsError)) { throw new RoslynCompilationException(diagnostics); } Assembly assembly = null; // Rewind the stream assemblyStream.Seek(0, SeekOrigin.Begin); pdbStream.Seek(0, SeekOrigin.Begin); if (pdbStream.Length == 0) { assembly = loadContext.LoadStream(assemblyStream, assemblySymbols: null); } else { assembly = loadContext.LoadStream(assemblyStream, pdbStream); } return(assembly); } }
/// <summary> /// Loads the assembly containing precompiled views. /// </summary> /// <param name="loadContext">The <see cref="IAssemblyLoadContext"/>.</param> /// <returns>The <see cref="Assembly"/> containing precompiled views.</returns> public virtual Assembly LoadAssembly(IAssemblyLoadContext loadContext) { var viewCollectionAssembly = GetType().GetTypeInfo().Assembly; using (var assemblyStream = viewCollectionAssembly.GetManifestResourceStream(AssemblyResourceName)) { if (assemblyStream == null) { var message = Resources.FormatRazorFileInfoCollection_ResourceCouldNotBeFound(AssemblyResourceName, GetType().FullName); throw new InvalidOperationException(message); } Stream symbolsStream = null; if (!string.IsNullOrEmpty(SymbolsResourceName)) { symbolsStream = viewCollectionAssembly.GetManifestResourceStream(SymbolsResourceName); } using (symbolsStream) { return(loadContext.LoadStream(assemblyStream, symbolsStream)); } } }
/// <summary> /// Loads the assembly containing precompiled views. /// </summary> /// <param name="loadContext">The <see cref="IAssemblyLoadContext"/>.</param> /// <returns>The <see cref="Assembly"/> containing precompiled views.</returns> public virtual Assembly LoadAssembly(IAssemblyLoadContext loadContext) { var viewCollectionAssembly = GetType().GetTypeInfo().Assembly; using (var assemblyStream = viewCollectionAssembly.GetManifestResourceStream(AssemblyResourceName)) { if (assemblyStream == null) { var message = Resources.FormatRazorFileInfoCollection_ResourceCouldNotBeFound(AssemblyResourceName, GetType().FullName); throw new InvalidOperationException(message); } Stream symbolsStream = null; if (!string.IsNullOrEmpty(SymbolsResourceName)) { symbolsStream = viewCollectionAssembly.GetManifestResourceStream(SymbolsResourceName); } using (symbolsStream) { return loadContext.LoadStream(assemblyStream, symbolsStream); } } }
public static CompilationResult GetAssemblyFromCompilation( IAssemblyLoadContext loader, Compilation compilation) { EmitResult result; using (var ms = new MemoryStream()) { using (var pdb = new MemoryStream()) { if (PlatformHelper.IsMono) { result = compilation.Emit(ms, pdbStream: null); } else { result = compilation.Emit(ms, pdbStream: pdb); } if (!result.Success) { var formatter = new DiagnosticFormatter(); var errorMessages = result.Diagnostics .Where(IsError) .Select(d => formatter.Format(d)); return(CompilationResult.FromErrorMessages(errorMessages)); } ms.Seek(0, SeekOrigin.Begin); Assembly assembly; if (PlatformHelper.IsMono) { assembly = loader.LoadStream(ms, assemblySymbols: null); } else { pdb.Seek(0, SeekOrigin.Begin); assembly = loader.LoadStream(ms, pdb); } return(CompilationResult.FromAssembly(assembly)); } } }
public Assembly Load(IAssemblyLoadContext loadContext) { if (_response.Errors.Any()) { throw new CompilationException(_response.Errors); } if (_response.AssemblyPath != null) { return(loadContext.LoadFile(_response.AssemblyPath)); } if (_response.PdbBytes == null) { return(loadContext.LoadStream(new MemoryStream(_response.AssemblyBytes), assemblySymbols: null)); } return(loadContext.LoadStream(new MemoryStream(_response.AssemblyBytes), new MemoryStream(_response.PdbBytes))); }
public Assembly Load(AssemblyName assemblyName, IAssemblyLoadContext loadContext) { if (_response.Diagnostics.HasErrors()) { throw new DesignTimeCompilationException(_response.Diagnostics); } if (_response.AssemblyPath != null) { return loadContext.LoadFile(_response.AssemblyPath); } if (_response.PdbBytes == null) { return loadContext.LoadStream(new MemoryStream(_response.AssemblyBytes), assemblySymbols: null); } return loadContext.LoadStream(new MemoryStream(_response.AssemblyBytes), new MemoryStream(_response.PdbBytes)); }
public Assembly Load(AssemblyName assemblyName, IAssemblyLoadContext loadContext) { if (_response.Diagnostics.HasErrors()) { throw new DesignTimeCompilationException(_response.Diagnostics); } if (_response.AssemblyPath != null) { return(loadContext.LoadFile(_response.AssemblyPath)); } if (_response.PdbBytes == null) { return(loadContext.LoadStream(new MemoryStream(_response.AssemblyBytes), assemblySymbols: null)); } return(loadContext.LoadStream(new MemoryStream(_response.AssemblyBytes), new MemoryStream(_response.PdbBytes))); }
public Assembly Load(AssemblyName assemblyName, IAssemblyLoadContext loadContext) { if (!_context.Success) { throw new FSharpCompilationException(_context.Diagnostics); } using (var assembly = new MemoryStream(_context.Assembly)) { if (FSharpCompiler.SupportsPdbGeneration && _context.Pdb != null) { using (var pdb = new MemoryStream(_context.Pdb)) { return(loadContext.LoadStream(assembly, pdb)); } } else { Logger.TraceWarning("PDB generation is not supported on this platform"); return(loadContext.LoadStream(assembly, null)); } } }
public Assembly Load(IAssemblyLoadContext loadContext) { if (!_context.Success) { throw new FSharpCompilationException(_context.Diagnostics); } using (var assembly = new MemoryStream(_context.Assembly)) { if (FSharpCompiler.SupportsPdbGeneration && _context.Pdb != null) { using (var pdb = new MemoryStream(_context.Pdb)) { return loadContext.LoadStream(assembly, pdb); } } else { Logger.TraceWarning("PDB generation is not supported on this platform"); return loadContext.LoadStream(assembly, null); } } }
/// <inheritdoc /> public CompilationResult Compile(RelativeFileInfo fileInfo, string compilationContent) { var assemblyName = Path.GetRandomFileName(); var sourceText = SourceText.From(compilationContent, Encoding.UTF8); var syntaxTree = CSharpSyntaxTree.ParseText( sourceText, path: assemblyName, options: _options.ParseOptions); var references = _applicationReferences.Value; var compilation = CSharpCompilation.Create( assemblyName, options: _options.CompilationOptions, syntaxTrees: new[] { syntaxTree }, references: references); using (var ms = new MemoryStream()) { using (var pdb = new MemoryStream()) { var result = compilation.Emit(ms); if (!result.Success) { return(GetCompilationFailedResult( fileInfo.RelativePath, compilationContent, assemblyName, result.Diagnostics)); } ms.Seek(0, SeekOrigin.Begin); var assembly = _loader.LoadStream(ms, assemblySymbols: null); var type = assembly .GetExportedTypes()[0]; return(new CompilationResult(type)); } } }
private void ExtractAssemblyNeutralInterfaces(Assembly assembly) { // Embedded assemblies end with .dll foreach (var resourceName in assembly.GetManifestResourceNames()) { if (resourceName.StartsWith("AssemblyNeutral/") && resourceName.EndsWith(".dll")) { var assemblyName = Path.GetFileNameWithoutExtension(resourceName); var neutralAssemblyStream = assembly.GetManifestResourceStream(resourceName); try { _defaultContext.LoadStream(neutralAssemblyStream, assemblySymbols: null); } catch (FileLoadException) { // Already loaded } } } }
private TypeInfo[] GetExportedTypesFromCompilation() { using (var stream = new MemoryStream()) { var assemblyName = string.Join(".", _compileContext.Compilation.AssemblyName, nameof(PrecompilationTagHelperTypeResolver), Path.GetRandomFileName()); var emitResult = _compileContext.Compilation .WithAssemblyName(assemblyName) .Emit(stream); if (!emitResult.Success) { // Return an empty sequence. Compilation will fail once precompilation completes. return(new TypeInfo[0]); } stream.Position = 0; var assembly = _loadContext.LoadStream(stream, assemblySymbols: null); return(assembly.ExportedTypes .Select(type => type.GetTypeInfo()) .ToArray()); } }
public Assembly Load(AssemblyName assemblyName, IAssemblyLoadContext loadContext) { using (var pdbStream = new MemoryStream()) using (var assemblyStream = new MemoryStream()) { var afterCompileContext = new AfterCompileContext { ProjectContext = CompilationContext.ProjectContext, Compilation = CompilationContext.Compilation, AssemblyStream = assemblyStream, SymbolStream = pdbStream }; EmitResult emitResult = null; // If assembly is not a satelite assembly or if assembly culture is neutral, then do not generate a resources assembly. if (!string.Equals(Path.GetExtension(assemblyName.Name), ".resources") || ResourcesHelper.IsResourceNeutralCulture(assemblyName)) { var resourcesForCulture = ResourcesForCulture.GetResourcesForCulture(assemblyName.CultureName ?? string.Empty, CompilationContext.Resources); if (resourcesForCulture == null) { // No resources is fine for a main assembly resourcesForCulture = Enumerable.Empty <ResourceDescriptor>(); } var resources = resourcesForCulture .Select(res => new ResourceDescription(res.Name, res.StreamFactory, isPublic: true)); Logger.TraceInformation("[{0}]: Emitting assembly for {1}", GetType().Name, Name); var sw = Stopwatch.StartNew(); bool emitPdb; var emitOptions = GetEmitOptions(out emitPdb); emitResult = CompilationContext.Compilation.Emit(assemblyStream, pdbStream: emitPdb ? pdbStream : null, manifestResources: resources, options: emitOptions); sw.Stop(); Logger.TraceInformation("[{0}]: Emitted {1} in {2}ms", GetType().Name, Name, sw.ElapsedMilliseconds); foreach (var m in CompilationContext.Modules) { m.AfterCompile(afterCompileContext); } } else { var resourcesForCulture = ResourcesForCulture.GetResourcesForCulture(assemblyName.CultureName ?? string.Empty, CompilationContext.Resources); if (resourcesForCulture == null) { return(null); } afterCompileContext.SymbolStream = null; emitResult = EmitResourceAssembly(assemblyName, resourcesForCulture, afterCompileContext.Compilation.Options, afterCompileContext.AssemblyStream); } afterCompileContext.Diagnostics = CompilationContext.Diagnostics.Concat(emitResult.Diagnostics).ToList(); if (!emitResult.Success || afterCompileContext.Diagnostics.Any(RoslynDiagnosticUtilities.IsError)) { throw new RoslynCompilationException(afterCompileContext.Diagnostics, CompilationContext.ProjectContext.TargetFramework); } Assembly assembly = null; // If this is null it'll fail anyways, just don't blow up with // a null reference if (afterCompileContext.AssemblyStream != null) { afterCompileContext.AssemblyStream.Position = 0; } if (afterCompileContext.SymbolStream == null || afterCompileContext.SymbolStream.Length == 0) { assembly = loadContext.LoadStream(afterCompileContext.AssemblyStream, assemblySymbols: null); } else { afterCompileContext.SymbolStream.Position = 0; assembly = loadContext.LoadStream(afterCompileContext.AssemblyStream, afterCompileContext.SymbolStream); } return(assembly); } }
/// <inheritdoc /> public CompilationResult Compile([NotNull] IFileInfo fileInfo, [NotNull] string compilationContent) { // The path passed to SyntaxTreeGenerator.Generate is used by the compiler to generate symbols (pdb) that // map to the source file. If a file does not exist on a physical file system, PhysicalPath will be null. // This prevents files that exist in a non-physical file system from being debugged. var path = fileInfo.PhysicalPath ?? fileInfo.Name; var compilationSettings = _compilerOptionsProvider.GetCompilationSettings(_environment); var syntaxTree = SyntaxTreeGenerator.Generate(compilationContent, path, compilationSettings); var references = _applicationReferences.Value; var assemblyName = Path.GetRandomFileName(); var compilationOptions = compilationSettings.CompilationOptions .WithOutputKind(OutputKind.DynamicallyLinkedLibrary); var compilation = CSharpCompilation.Create(assemblyName, options: compilationOptions, syntaxTrees: new[] { syntaxTree }, references: references); using (var ms = new MemoryStream()) { using (var pdb = new MemoryStream()) { EmitResult result; if (_supportsPdbGeneration.Value) { result = compilation.Emit(ms, pdbStream: pdb); } else { result = compilation.Emit(ms); } if (!result.Success) { var formatter = new DiagnosticFormatter(); var messages = result.Diagnostics .Where(IsError) .Select(d => GetCompilationMessage(formatter, d)) .ToList(); return(CompilationResult.Failed(fileInfo, compilationContent, messages)); } Assembly assembly; ms.Seek(0, SeekOrigin.Begin); if (_supportsPdbGeneration.Value) { pdb.Seek(0, SeekOrigin.Begin); assembly = _loader.LoadStream(ms, pdb); } else { assembly = _loader.LoadStream(ms, assemblySymbols: null); } var type = assembly.GetExportedTypes() .First(t => t.Name.StartsWith(_classPrefix, StringComparison.Ordinal)); return(UncachedCompilationResult.Successful(type, compilationContent)); } } }
public Assembly Load(IAssemblyLoadContext loadContext) { if(_response.Errors.Any()) { throw new CompilationException(_response.Errors); } if (_response.AssemblyPath != null) { return loadContext.LoadFile(_response.AssemblyPath); } if (_response.PdbBytes == null) { return loadContext.LoadStream(new MemoryStream(_response.AssemblyBytes), assemblySymbols: null); } return loadContext.LoadStream(new MemoryStream(_response.AssemblyBytes), new MemoryStream(_response.PdbBytes)); }
public Assembly Load(IAssemblyLoadContext loadContext) { using (var pdbStream = new MemoryStream()) using (var assemblyStream = new MemoryStream()) { IList<ResourceDescription> resources = CompilationContext.Resources; Logger.TraceInformation("[{0}]: Emitting assembly for {1}", GetType().Name, Name); var sw = Stopwatch.StartNew(); EmitResult emitResult = null; if (_supportsPdbGeneration.Value) { emitResult = CompilationContext.Compilation.Emit(assemblyStream, pdbStream: pdbStream, manifestResources: resources); } else { Logger.TraceWarning("PDB generation is not supported on this platform"); emitResult = CompilationContext.Compilation.Emit(assemblyStream, manifestResources: resources); } sw.Stop(); Logger.TraceInformation("[{0}]: Emitted {1} in {2}ms", GetType().Name, Name, sw.ElapsedMilliseconds); var diagnostics = CompilationContext.Diagnostics.Concat( emitResult.Diagnostics); var afterCompileContext = new AfterCompileContext(CompilationContext, diagnostics) { AssemblyStream = assemblyStream, SymbolStream = pdbStream }; foreach (var m in CompilationContext.Modules) { m.AfterCompile(afterCompileContext); } if (!emitResult.Success || afterCompileContext.Diagnostics.Any(RoslynDiagnosticUtilities.IsError)) { throw new RoslynCompilationException(afterCompileContext.Diagnostics); } Assembly assembly = null; // If this is null it'll fail anyways, just don't blow up with // a null reference if (afterCompileContext.AssemblyStream != null) { afterCompileContext.AssemblyStream.Position = 0; } if (afterCompileContext.SymbolStream == null || afterCompileContext.SymbolStream.Length == 0) { assembly = loadContext.LoadStream(afterCompileContext.AssemblyStream, assemblySymbols: null); } else { afterCompileContext.SymbolStream.Position = 0; assembly = loadContext.LoadStream(afterCompileContext.AssemblyStream, afterCompileContext.SymbolStream); } return assembly; } }
public Assembly Load(AssemblyName assemblyName, IAssemblyLoadContext loadContext) { using (var pdbStream = new MemoryStream()) using (var assemblyStream = new MemoryStream()) { var afterCompileContext = new AfterCompileContext { ProjectContext = CompilationContext.ProjectContext, Compilation = CompilationContext.Compilation, AssemblyStream = assemblyStream, SymbolStream = pdbStream }; EmitResult emitResult = null; // If assembly is not a satelite assembly or if assembly culture is neutral, then do not generate a resources assembly. if (!string.Equals(Path.GetExtension(assemblyName.Name), ".resources") || ResourcesHelper.IsResourceNeutralCulture(assemblyName)) { var resourcesForCulture = ResourcesForCulture.GetResourcesForCulture(assemblyName.CultureName ?? string.Empty, CompilationContext.Resources); if (resourcesForCulture == null) { // No resources is fine for a main assembly resourcesForCulture = Enumerable.Empty<ResourceDescriptor>(); } var resources = resourcesForCulture .Select(res => new ResourceDescription(res.Name, res.StreamFactory, isPublic: true)); Logger.TraceInformation("[{0}]: Emitting assembly for {1}", GetType().Name, Name); var sw = Stopwatch.StartNew(); bool emitPdb; var emitOptions = GetEmitOptions(out emitPdb); emitResult = CompilationContext.Compilation.Emit(assemblyStream, pdbStream: emitPdb ? pdbStream : null, manifestResources: resources, options: emitOptions); sw.Stop(); Logger.TraceInformation("[{0}]: Emitted {1} in {2}ms", GetType().Name, Name, sw.ElapsedMilliseconds); foreach (var m in CompilationContext.Modules) { m.AfterCompile(afterCompileContext); } } else { var resourcesForCulture = ResourcesForCulture.GetResourcesForCulture(assemblyName.CultureName ?? string.Empty, CompilationContext.Resources); if (resourcesForCulture == null) { return null; } afterCompileContext.SymbolStream = null; emitResult = EmitResourceAssembly(assemblyName, resourcesForCulture, afterCompileContext.Compilation.Options, afterCompileContext.AssemblyStream); } afterCompileContext.Diagnostics = CompilationContext.Diagnostics.Concat(emitResult.Diagnostics).ToList(); if (!emitResult.Success || afterCompileContext.Diagnostics.Any(RoslynDiagnosticUtilities.IsError)) { throw new RoslynCompilationException(afterCompileContext.Diagnostics, CompilationContext.ProjectContext.TargetFramework); } Assembly assembly = null; // If this is null it'll fail anyways, just don't blow up with // a null reference if (afterCompileContext.AssemblyStream != null) { afterCompileContext.AssemblyStream.Position = 0; } if (afterCompileContext.SymbolStream == null || afterCompileContext.SymbolStream.Length == 0) { assembly = loadContext.LoadStream(afterCompileContext.AssemblyStream, assemblySymbols: null); } else { afterCompileContext.SymbolStream.Position = 0; assembly = loadContext.LoadStream(afterCompileContext.AssemblyStream, afterCompileContext.SymbolStream); } return assembly; } }
/// <inheritdoc /> public CompilationResult Compile(IFileInfo fileInfo, string compilationContent) { var syntaxTrees = new[] { SyntaxTreeGenerator.Generate(compilationContent, fileInfo.PhysicalPath) }; var references = _applicationReferences.Value; var assemblyName = Path.GetRandomFileName(); var compilation = CSharpCompilation.Create(assemblyName, options: new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary), syntaxTrees: syntaxTrees, references: references); using (var ms = new MemoryStream()) { using (var pdb = new MemoryStream()) { EmitResult result; if (_supportsPdbGeneration.Value) { result = compilation.Emit(ms, pdbStream: pdb); } else { result = compilation.Emit(ms); } if (!result.Success) { var formatter = new DiagnosticFormatter(); var messages = result.Diagnostics .Where(IsError) .Select(d => GetCompilationMessage(formatter, d)) .ToList(); return(CompilationResult.Failed(fileInfo, compilationContent, messages)); } Assembly assembly; ms.Seek(0, SeekOrigin.Begin); if (_supportsPdbGeneration.Value) { pdb.Seek(0, SeekOrigin.Begin); assembly = _loader.LoadStream(ms, pdb); } else { assembly = _loader.LoadStream(ms, assemblySymbols: null); } var type = assembly.GetExportedTypes() .First(t => t.Name. StartsWith(_classPrefix, StringComparison.Ordinal)); return(UncachedCompilationResult.Successful(type)); } } }
public Assembly Load(IAssemblyLoadContext loadContext) { using (var pdbStream = new MemoryStream()) using (var assemblyStream = new MemoryStream()) { IList <ResourceDescription> resources = CompilationContext.Resources; Logger.TraceInformation("[{0}]: Emitting assembly for {1}", GetType().Name, Name); var sw = Stopwatch.StartNew(); EmitResult emitResult = null; if (_supportsPdbGeneration.Value) { emitResult = CompilationContext.Compilation.Emit(assemblyStream, pdbStream: pdbStream, manifestResources: resources); } else { Logger.TraceWarning("PDB generation is not supported on this platform"); emitResult = CompilationContext.Compilation.Emit(assemblyStream, manifestResources: resources); } sw.Stop(); Logger.TraceInformation("[{0}]: Emitted {1} in {2}ms", GetType().Name, Name, sw.ElapsedMilliseconds); var diagnostics = CompilationContext.Diagnostics.Concat( emitResult.Diagnostics); var afterCompileContext = new AfterCompileContext { ProjectContext = CompilationContext.ProjectContext, Compilation = CompilationContext.Compilation, AssemblyStream = assemblyStream, SymbolStream = pdbStream, Diagnostics = new List <Diagnostic>(diagnostics) }; foreach (var m in CompilationContext.Modules) { m.AfterCompile(afterCompileContext); } if (!emitResult.Success || afterCompileContext.Diagnostics.Any(RoslynDiagnosticUtilities.IsError)) { throw new RoslynCompilationException(afterCompileContext.Diagnostics, CompilationContext.ProjectContext.TargetFramework); } Assembly assembly = null; // If this is null it'll fail anyways, just don't blow up with // a null reference if (afterCompileContext.AssemblyStream != null) { afterCompileContext.AssemblyStream.Position = 0; } if (afterCompileContext.SymbolStream == null || afterCompileContext.SymbolStream.Length == 0) { assembly = loadContext.LoadStream(afterCompileContext.AssemblyStream, assemblySymbols: null); } else { afterCompileContext.SymbolStream.Position = 0; assembly = loadContext.LoadStream(afterCompileContext.AssemblyStream, afterCompileContext.SymbolStream); } return(assembly); } }
public Assembly Load(IAssemblyLoadContext loadContext) { using (var pdbStream = new MemoryStream()) using (var assemblyStream = new MemoryStream()) { IList<ResourceDescription> resources = CompilationContext.Resources; Trace.TraceInformation("[{0}]: Emitting assembly for {1}", GetType().Name, Name); var sw = Stopwatch.StartNew(); EmitResult emitResult = null; if (_supportsPdbGeneration.Value) { emitResult = CompilationContext.Compilation.Emit(assemblyStream, pdbStream: pdbStream, manifestResources: resources); } else { Trace.TraceWarning("PDB generation is not supported on this platform"); emitResult = CompilationContext.Compilation.Emit(assemblyStream, manifestResources: resources); } sw.Stop(); Trace.TraceInformation("[{0}]: Emitted {1} in {2}ms", GetType().Name, Name, sw.ElapsedMilliseconds); var diagnostics = CompilationContext.Diagnostics.Concat( emitResult.Diagnostics); if (!emitResult.Success || diagnostics.Any(RoslynDiagnosticUtilities.IsError)) { throw new RoslynCompilationException(diagnostics); } Assembly assembly = null; // Rewind the stream assemblyStream.Seek(0, SeekOrigin.Begin); pdbStream.Seek(0, SeekOrigin.Begin); if (pdbStream.Length == 0) { assembly = loadContext.LoadStream(assemblyStream, assemblySymbols: null); } else { assembly = loadContext.LoadStream(assemblyStream, pdbStream); } return assembly; } }