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

Code before:

        private readonly Compilation _compilation;
        private readonly IEnumerable<ModuleData>? _dependencies;
        private IModuleSymbol? _lazyModuleSymbol;
        private EmitData? _emitData;
        private readonly Func<IModuleSymbol, CompilationTestData.MethodData, IReadOnlyDictionary<int, string>?, bool, string>? _visualizeRealIL;

        public Compilation Compilation => _compilation;
        public ImmutableArray<byte> EmittedAssemblyData => GetEmitData().EmittedAssemblyData;
        public ImmutableArray<byte> EmittedAssemblyPdb => GetEmitData().EmittedAssemblyPdb;
        public ImmutableArray<Diagnostic> Diagnostics => GetEmitData().Diagnostics;
        internal CompilationTestData TestData => GetEmitData().TestData;

        internal CompilationVerifier(
            Compilation compilation,
            Func<IModuleSymbol, CompilationTestData.MethodData, IReadOnlyDictionary<int, string>?, bool, string>? visualizeRealIL = null,
            IEnumerable<ModuleData>? dependencies = null)
        {
            _compilation = compilation;
            _dependencies = dependencies;
            _visualizeRealIL = visualizeRealIL;
        }

        private EmitData GetEmitData() => _emitData ?? throw new InvalidOperationException("Must call Emit first");

        internal Metadata GetMetadata()
        {
            var emitData = GetEmitData();
            if (_compilation.Options.OutputKind.IsNetModule())
            {
                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;
                }));
            }
        }


Review comment (suggestion):
📝 inspired by `GetCompilation(...).EmitToImageReference` - there was no equivalent for `CompileAndVerify` afaict

Code after:

        private readonly Compilation _compilation;
        private readonly IEnumerable<ModuleData>? _dependencies;
        private IModuleSymbol? _lazyModuleSymbol;
        private EmitData? _emitData;
        private readonly Func<IModuleSymbol, CompilationTestData.MethodData, IReadOnlyDictionary<int, string>?, bool, string>? _visualizeRealIL;

        public Compilation Compilation => _compilation;
        public ImmutableArray<byte> EmittedAssemblyData => GetEmitData().EmittedAssemblyData;
        public ImmutableArray<byte> EmittedAssemblyPdb => GetEmitData().EmittedAssemblyPdb;
        public ImmutableArray<Diagnostic> Diagnostics => GetEmitData().Diagnostics;
        internal CompilationTestData TestData => GetEmitData().TestData;

        internal CompilationVerifier(
            Compilation compilation,
            Func<IModuleSymbol, CompilationTestData.MethodData, IReadOnlyDictionary<int, string>?, bool, string>? visualizeRealIL = null,
            IEnumerable<ModuleData>? dependencies = null)
        {
            _compilation = compilation;
            _dependencies = dependencies;
            _visualizeRealIL = visualizeRealIL;
        }

        private EmitData GetEmitData() => _emitData ?? throw new InvalidOperationException("Must call Emit first");

        internal PortableExecutableReference GetImageReference(
            bool embedInteropTypes = false,
            ImmutableArray<string> aliases = default,
            DocumentationProvider? documentation = null)
        {
            if (Compilation.Options.OutputKind == OutputKind.NetModule)
            {
                return ModuleMetadata.CreateFromImage(EmittedAssemblyData).GetReference(documentation, display: Compilation.MakeSourceModuleName());
            }
            else
            {
                return AssemblyMetadata.CreateFromImage(EmittedAssemblyData).GetReference(documentation, aliases: aliases, embedInteropTypes: embedInteropTypes, display: Compilation.MakeSourceAssemblySimpleName());
            }
        }

        internal Metadata GetMetadata()
        {
            var emitData = GetEmitData();
            if (_compilation.Options.OutputKind.IsNetModule())
            {
                var metadata = ModuleMetadata.CreateFromImage(emitData.EmittedAssemblyData);
                metadata.Module.PretendThereArentNoPiaLocalTypes();
                return metadata;
            }
            else
            {