Repository: dotnet/roslyn
File: src/Compilers/Test/Core/CompilationVerifier.cs (C#)

Code before:
            {
                var metadata = ModuleMetadata.CreateFromImage(emitData.EmittedAssemblyData);
                metadata.Module.PretendThereArentNoPiaLocalTypes();
                return metadata;
            }
            else
            {
                List<ImmutableArray<byte>> images =
                [
                    emitData.EmittedAssemblyData,
                    .. emitData.Modules.Where(m => m.Kind == OutputKind.NetModule).Select(m => m.Image)
                ];

                return AssemblyMetadata.Create(images.Select(image =>
                {
                    var metadata = ModuleMetadata.CreateFromImage(image);
                    metadata.Module.PretendThereArentNoPiaLocalTypes();
                    return metadata;
                }));
            }
        }

        public string DumpIL()
        {
            var output = new ICSharpCode.Decompiler.PlainTextOutput();
            var emitData = Emit(manifestResources: null, EmitOptions.Default);
            using var moduleMetadata = ModuleMetadata.CreateFromImage(emitData.EmittedAssemblyData);
            var peFile = new PEFile(emitData.EmittedModule.Id.FullName, moduleMetadata.Module.PEReaderOpt);
            var disassembler = new ICSharpCode.Decompiler.Disassembler.ReflectionDisassembler(output, default);
            disassembler.WriteModuleContents(peFile);
            return output.ToString();
        }

        public static string DumpAssemblyData(IEnumerable<ModuleData> modules, string dumpBasePath)
        {
            var dumpCount = Interlocked.Increment(ref s_dumpCount);
            var dumpDirectory = Path.Combine(dumpBasePath is "" ? TempRoot.Root : dumpBasePath, "dumps", dumpCount.ToString());
            _ = Directory.CreateDirectory(dumpDirectory);

            // Limit the number of dumps to 10. After 10 we're likely in a bad state and are 
            // dumping lots of unnecessary data to disk.
            if (dumpCount > 10)
            {
                return dumpDirectory;
            }

            var sb = new StringBuilder();
            foreach (var module in modules)
            {
                if (module.InMemoryModule)
                {

Review comment (bug):
Although this was unreferenced, perhaps it was used by folks during debugging?

Code after:
            {
                var metadata = ModuleMetadata.CreateFromImage(emitData.EmittedAssemblyData);
                metadata.Module.PretendThereArentNoPiaLocalTypes();
                return metadata;
            }
            else
            {
                List<ImmutableArray<byte>> images =
                [
                    emitData.EmittedAssemblyData,
                    .. emitData.Modules.Where(m => m.Kind == OutputKind.NetModule).Select(m => m.Image)
                ];

                return AssemblyMetadata.Create(images.Select(image =>
                {
                    var metadata = ModuleMetadata.CreateFromImage(image);
                    metadata.Module.PretendThereArentNoPiaLocalTypes();
                    return metadata;
                }));
            }
        }

        public string Dump(string? methodName = null)
        {
            var emitData = Emit(manifestResources: null, EmitOptions.Default);
            var dumpDir = DumpAssemblyData(emitData.Modules, DumpAssemblyLocation ?? "");
            string extension = emitData.EmittedModule.Kind == OutputKind.ConsoleApplication ? ".exe" : ".dll";
            string modulePath = Path.Combine(dumpDir, emitData.EmittedModule.SimpleName + extension);

            var decompiler = new ICSharpCode.Decompiler.CSharp.CSharpDecompiler(modulePath,
                new ICSharpCode.Decompiler.DecompilerSettings() { AsyncAwait = false });

            if (methodName != null)
            {
                var map = new Dictionary<string, ICSharpCode.Decompiler.TypeSystem.IMethod>();
                listMethods(decompiler.TypeSystem.MainModule.RootNamespace, map);

                if (map.TryGetValue(methodName, out var method))
                {
                    return decompiler.DecompileAsString(method.MetadataToken);
                }
                else
                {
                    throw new Exception($"Didn't find method '{methodName}'. Available/distinguishable methods are: {Environment.NewLine}{string.Join(Environment.NewLine, map.Keys)}");
                }
            }

            return decompiler.DecompileWholeModuleAsString();

            void listMethods(ICSharpCode.Decompiler.TypeSystem.INamespace @namespace, Dictionary<string, ICSharpCode.Decompiler.TypeSystem.IMethod> result)
            {