public static Assembly create_Assembly(this CommonCompilation compilation, string fileToCreate) { fileToCreate.deleteIfExists(); if (fileToCreate.fileExists()) { "[create_Assembly] targetExe could not be deleted".error(); return(null); } fileToCreate.directoryName().createDir(); var pdbFilename = "{0}.pdb".format(fileToCreate); CommonEmitResult emitResult = null; //compilation. using (var ilStream = new FileStream(fileToCreate, FileMode.OpenOrCreate)) using (var pdbStream = new FileStream(pdbFilename, FileMode.OpenOrCreate)) { emitResult = compilation.Emit(ilStream, pdbFilename, pdbStream, null, default(CancellationToken), null, null, null); if (emitResult.Success && fileToCreate.fileExists()) { "[create_Assembly] created assembly: {0}".info(fileToCreate); } else { "[create_Assembly] could not create assembly: {0}".error(fileToCreate); "[create_Assembly] Compilation errors: {1} {0}".error(emitResult.Diagnostics.errors().asString()); } } if (emitResult.Success) { return(Assembly.LoadFrom(fileToCreate)); } return(null); }
internal static CommonEmitResult EmitToMemory(this CommonCompilation compilation) { using (var stream = new MemoryStream()) { return(compilation.Emit(stream)); } }
private bool TryEmitSubmission(CommonCompilation compilation, DiagnosticBag diagnostics, Type delegateType, bool collectible, CancellationToken cancellationToken, out Delegate factory) { // Note that we migth be giving the emitter a collectible module but if the code contains uncollectible features // it will be generated to a separate new uncollectible assembly. var emitResult = compilation.Emit( GetOrCreateDynamicModule(collectible), assemblyLoader: GetAssemblyLoader(collectible), assemblySymbolMapper: symbol => MapAssemblySymbol(symbol, collectible), cancellationToken: cancellationToken ); diagnostics.Add(emitResult.Diagnostics); // emit can fail due to compilation errors or because there is nothing to emit: if (!emitResult.Success) { factory = null; return(!diagnostics.HasAnyErrors()); } Debug.Assert(emitResult.EntryPoint != null); if (emitResult.IsUncollectible) { // Ref.Emit wasn't able to emit the assembly uncollectibleCodeManager.AddFallBackAssembly(emitResult.EntryPoint.DeclaringType.Assembly); } #if DEBUG if (SaveCompiledAssemblies) { this.uncollectibleCodeManager.Save(UncollectibleModuleFileName); this.collectibleCodeManager.Save(CollectibleModuleFileName); } #endif factory = Delegate.CreateDelegate(delegateType, emitResult.EntryPoint); return(true); }