public System.Reflection.Assembly Build(Compilation compilation, DiagnosticBag diagnostics, CancellationToken cancellationToken) { using (var peStream = new MemoryStream()) { using (var pdbStream = new MemoryStream()) { var emitResult = compilation.Emit( peStream: peStream, pdbStream: pdbStream, cancellationToken: cancellationToken); diagnostics.AddRange(emitResult.Diagnostics); if (!emitResult.Success) { return(null); } foreach (var referencedAssembly in compilation.References.Select( x => new { Key = x, Value = compilation.GetAssemblyOrModuleSymbol(x) })) { var path = (referencedAssembly.Key as PortableExecutableReference)?.FilePath; if (path != null) { _assemblyLoader.RegisterDependency(((IAssemblySymbol)referencedAssembly.Value).Identity, path); } } peStream.Position = 0; pdbStream.Position = 0; return(_assemblyLoader.LoadAssemblyFromStream(peStream, pdbStream)); } } }
public static System.Reflection.Assembly Build(Compilation compilation, DiagnosticBag diagnostics, CancellationToken cancellationToken) { using (var peStream = new MemoryStream()) using (var pdbStream = new MemoryStream()) { var emitResult = compilation.Emit( peStream: peStream, pdbStream: pdbStream, cancellationToken: cancellationToken); diagnostics.AddRange(emitResult.Diagnostics); if (!emitResult.Success) { return(null); } foreach (var referencedAssembly in compilation.References) { var path = (referencedAssembly as PortableExecutableReference)?.FilePath; if (path != null) { var assemblySymbol = (IAssemblySymbol)compilation.GetAssemblyOrModuleSymbol(referencedAssembly); if (null != assemblySymbol) { _assemblyLoader.RegisterDependency(assemblySymbol.Identity, path); } else { // this can happen if the original reference is to an assembly that is GAC'ed, // in this case the new MetaDataReference points to the assembly in the GAC, so its location // is different from the original one. } } } peStream.Position = 0; pdbStream.Position = 0; return(_assemblyLoader.LoadAssemblyFromStream(peStream, pdbStream)); } }