Repository: dotnet/runtime
File: src/libraries/System.Reflection.Emit/tests/PersistedAssemblyBuilder/AssemblySaveAssemblyBuilderTests.cs (C#)

Code before:
                {
                    il.EmitCalli(OpCodes.Calli, CallingConventions.HasThis,
                        returnType: typeof(Guid), parameterTypes: null, null);
                }

                il.Emit(OpCodes.Ret);
                typeBuilder.CreateType();
                MetadataBuilder metadataBuilder = ab.GenerateMetadata(out BlobBuilder ilStream, out BlobBuilder _);
                ManagedPEBuilder peBuilder = new ManagedPEBuilder(PEHeaderBuilder.CreateLibraryHeader(), new MetadataRootBuilder(metadataBuilder), ilStream);
                BlobBuilder blob = new BlobBuilder();
                peBuilder.Serialize(blob);
                return blob.ToArray();
            }
        }

        private class MyClassWithGuidProperty
        {
            public MyClassWithGuidProperty(Guid guid)
            {
                MyGuid = guid;
            }

            public Guid MyGuid { get; init; }
        }

        private static PersistedAssemblyBuilder CreateSimplePersistedAssembly(AssemblyName assemblyName)
        {
            PersistedAssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilder(assemblyName);
            TypeBuilder typeBuilder = ab.DefineDynamicModule("MyModule").DefineType("MyType", TypeAttributes.Public | TypeAttributes.Class);
            typeBuilder.CreateType();
            return ab;
        }

        void CheckCattr(IList<CustomAttributeData> attributes)
        {
            CustomAttributeData cattr = attributes.First(a => a.AttributeType.Name == nameof(AttributeUsageAttribute));
            Assert.Equal(1, cattr.ConstructorArguments.Count);
            Assert.Equal(AttributeTargets.Class, (AttributeTargets)cattr.ConstructorArguments[0].Value);
            Assert.Equal(1, cattr.NamedArguments.Count);
            Assert.Equal("AllowMultiple", cattr.NamedArguments[0].MemberName);
            Assert.True((bool)cattr.NamedArguments[0].TypedValue.Value);
        }

        private static void CheckMarshallAttribute(IList<CustomAttributeData> attributes, UnmanagedType ctorValue, string? namedArgument = null, object? naValue = null)
        {
            CustomAttributeData cattr = attributes.First(a => a.AttributeType.Name == nameof(MarshalAsAttribute));
            Assert.Equal(1, cattr.ConstructorArguments.Count);
            Assert.Equal(ctorValue, (UnmanagedType)cattr.ConstructorArguments[0].Value);
            if (namedArgument != null)
            {
                CustomAttributeNamedArgument namedArg = cattr.NamedArguments.First(a => a.MemberName == namedArgument);

Review comment (refactor):
@copilot This is nearly identical to the existing PopulateAssemblyBuilderAndTypeBuilder helper. Delete this method and replace it with the existing one

Code after:
                }
                else
                {
                    il.EmitCalli(OpCodes.Calli, CallingConventions.HasThis,
                        returnType: typeof(Guid), parameterTypes: null, null);
                }

                il.Emit(OpCodes.Ret);
                typeBuilder.CreateType();
                MetadataBuilder metadataBuilder = ab.GenerateMetadata(out BlobBuilder ilStream, out BlobBuilder _);
                ManagedPEBuilder peBuilder = new ManagedPEBuilder(PEHeaderBuilder.CreateLibraryHeader(), new MetadataRootBuilder(metadataBuilder), ilStream);
                BlobBuilder blob = new BlobBuilder();
                peBuilder.Serialize(blob);
                return blob.ToArray();
            }
        }

        private class MyClassWithGuidProperty
        {
            public MyClassWithGuidProperty(Guid guid)
            {
                MyGuid = guid;
            }

            public Guid MyGuid { get; init; }
        }

        void CheckCattr(IList<CustomAttributeData> attributes)
        {
            CustomAttributeData cattr = attributes.First(a => a.AttributeType.Name == nameof(AttributeUsageAttribute));
            Assert.Equal(1, cattr.ConstructorArguments.Count);
            Assert.Equal(AttributeTargets.Class, (AttributeTargets)cattr.ConstructorArguments[0].Value);
            Assert.Equal(1, cattr.NamedArguments.Count);
            Assert.Equal("AllowMultiple", cattr.NamedArguments[0].MemberName);
            Assert.True((bool)cattr.NamedArguments[0].TypedValue.Value);
        }

        private static void CheckMarshallAttribute(IList<CustomAttributeData> attributes, UnmanagedType ctorValue, string? namedArgument = null, object? naValue = null)
        {
            CustomAttributeData cattr = attributes.First(a => a.AttributeType.Name == nameof(MarshalAsAttribute));
            Assert.Equal(1, cattr.ConstructorArguments.Count);
            Assert.Equal(ctorValue, (UnmanagedType)cattr.ConstructorArguments[0].Value);
            if (namedArgument != null)
            {
                CustomAttributeNamedArgument namedArg = cattr.NamedArguments.First(a => a.MemberName == namedArgument);
                Assert.Equal(namedArgument, namedArg.MemberName);
                Assert.Equal(naValue, namedArg.TypedValue.Value.ToString());
            }
        }

        void CheckAssembly(Assembly a)