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

Code before:

        [Fact]
        public void PersistedAssemblyBuilder_CollectibleAssemblyLoadContextIsolation()
        {
            byte[] assemblyImage = BuildAssemblyImageForMetadataInvariants(new AssemblyName("IsolatedLoadContextAssembly"), ["IsolatedType"]);
            TestAssemblyLoadContext context1 = new();
            TestAssemblyLoadContext context2 = new();

            using MemoryStream stream1 = new MemoryStream(assemblyImage);
            using MemoryStream stream2 = new MemoryStream(assemblyImage);
            Assembly assembly1 = context1.LoadFromStream(stream1);
            Assembly assembly2 = context2.LoadFromStream(stream2);
            Type type1 = assembly1.GetType("IsolatedType");
            Type type2 = assembly2.GetType("IsolatedType");

            Assert.NotNull(type1);
            Assert.NotNull(type2);
            Assert.Equal(type1.FullName, type2.FullName);
            Assert.NotSame(type1.Assembly, type2.Assembly);

            context1.Unload();
            context2.Unload();
        }

        [Fact]
        public void PersistedAssemblyBuilder_MetadataInvariantsEquivalentShapeSameOrder()
        {
            byte[] firstAssemblyImage = BuildAssemblyImageForMetadataInvariants(new AssemblyName("MetadataInvariantAssembly1"), ["TypeA", "TypeB"]);
            byte[] secondAssemblyImage = BuildAssemblyImageForMetadataInvariants(new AssemblyName("MetadataInvariantAssembly2"), ["TypeA", "TypeB"]);

            MetadataInvariantData first = ReadMetadataInvariantData(firstAssemblyImage);
            MetadataInvariantData second = ReadMetadataInvariantData(secondAssemblyImage);

            Assert.Equal(first.TypeDefinitionRowCount, second.TypeDefinitionRowCount);
            Assert.Equal(first.MethodDefinitionRowCount, second.MethodDefinitionRowCount);
            Assert.Equal(first.FieldDefinitionRowCount, second.FieldDefinitionRowCount);
            Assert.Equal(first.TypeDefinitionNamesInMetadataOrder, second.TypeDefinitionNamesInMetadataOrder);
            Assert.Equal(first.MethodDefinitionNamesInMetadataOrder, second.MethodDefinitionNamesInMetadataOrder);
        }

        [Fact]
        public void PersistedAssemblyBuilder_MetadataInvariantsEquivalentShapeDifferentOrder()
        {
            byte[] firstAssemblyImage = BuildAssemblyImageForMetadataInvariants(new AssemblyName("MetadataInvariantAssemblyA"), ["TypeA", "TypeB"]);
            byte[] secondAssemblyImage = BuildAssemblyImageForMetadataInvariants(new AssemblyName("MetadataInvariantAssemblyB"), ["TypeB", "TypeA"]);

            MetadataInvariantData first = ReadMetadataInvariantData(firstAssemblyImage);
            MetadataInvariantData second = ReadMetadataInvariantData(secondAssemblyImage);

            Assert.Equal(first.TypeDefinitionRowCount, second.TypeDefinitionRowCount);
            Assert.Equal(first.MethodDefinitionRowCount, second.MethodDefinitionRowCount);

Review comment (suggestion):
The test is validating that emitting several types, methods and fields into assemblies with two different names is going to produce the same number of types, methods and fields.  I am not sure why it is an interesting test case. 

Code after:
                ctorb.GetILGenerator().Emit(OpCodes.Ret);
                // Parameters
                var paramb = ctorb.DefineParameter(1, ParameterAttributes.None, "param1");
                paramb.SetConstant(16);
                paramb.SetCustomAttribute(cattrb);
                paramb = ctorb.DefineParameter(2, ParameterAttributes.Out, "param2");
                paramb.SetCustomAttribute (new CustomAttributeBuilder (typeof (MarshalAsAttribute).GetConstructor ([typeof (UnmanagedType)]), [UnmanagedType.U4]));
                // .cctor
                var ctorb2 = tb5.DefineConstructor(MethodAttributes.Public | MethodAttributes.Static | MethodAttributes.RTSpecialName, CallingConventions.Standard, [typeof(int), typeof(object)]);
                ctorb2.GetILGenerator().Emit(OpCodes.Ret);
                // method
                var mb = tb5.DefineMethod("Method1", MethodAttributes.Public, CallingConventions.Standard, typeof(int), cmodsReq1, cmodsOpt1, [typeof(int), typeof(object)], [cmodsReq1, null], [cmodsOpt1, null]);
                mb.SetImplementationFlags(MethodImplAttributes.NoInlining);
                mb.GetILGenerator().Emit(OpCodes.Ldc_I4_0);
                mb.GetILGenerator().Emit(OpCodes.Ret);
                gParams = mb.DefineGenericParameters("K", "T");
                // Constraints
                gParams[0].SetBaseTypeConstraint(null);
                gParams[0].SetInterfaceConstraints([typeof(IComparable)]);
                paramb = mb.DefineParameter(1, ParameterAttributes.None, "param1");
                paramb.SetConstant(16);
                paramb = mb.DefineParameter(2, ParameterAttributes.Out, "param2");
                paramb.SetCustomAttribute (new CustomAttributeBuilder (typeof (MarshalAsAttribute).GetConstructor ([typeof (UnmanagedType)]), [UnmanagedType.U4]));
                // return value
                paramb = mb.DefineParameter(0, ParameterAttributes.None, "ret");
                paramb.SetCustomAttribute (new CustomAttributeBuilder (typeof (MarshalAsAttribute).GetConstructor ([typeof (UnmanagedType)]), [UnmanagedType.U4]));
                paramb.SetCustomAttribute(cattrb);
                // override method
                tb5.AddInterfaceImplementation(typeof(IComparable));
                mb = tb5.DefineMethod("MethodOverride", MethodAttributes.Public | MethodAttributes.Virtual, CallingConventions.Standard | CallingConventions.HasThis, typeof(int), [typeof(object)]);
                mb.GetILGenerator().Emit(OpCodes.Ldc_I4_1);
                mb.GetILGenerator().Emit(OpCodes.Ret);
                tb5.DefineMethodOverride(mb, typeof(IComparable).GetMethod("CompareTo"));
                tb5.CreateType();

                // Properties
                TypeBuilder tb_properties = module.DefineType("TypeProperties", TypeAttributes.Public, typeof(object));
                var mbGet = tb_properties.DefineMethod("GetMethod1", MethodAttributes.Public, CallingConventions.Standard, typeof(int), Type.EmptyTypes);
                mbGet.GetILGenerator().Emit(OpCodes.Ldc_I4_1);
                mbGet.GetILGenerator().Emit(OpCodes.Ret);
                var mbSet = tb_properties.DefineMethod("SetMethod1", MethodAttributes.Public, CallingConventions.Standard, typeof(void), [typeof(int)]);
                mbSet.GetILGenerator().Emit(OpCodes.Ret);
                var mbOther = tb_properties.DefineMethod("OtherMethod1", MethodAttributes.Public, CallingConventions.Standard, typeof(int), Type.EmptyTypes);
                mbOther.GetILGenerator().Emit(OpCodes.Ldc_I4_1);
                mbOther.GetILGenerator().Emit(OpCodes.Ret);
                var propertyb = tb_properties.DefineProperty("AProperty", PropertyAttributes.HasDefault, typeof(int), [typeof(object)]);
                propertyb.SetCustomAttribute(cattrb);
                propertyb.SetConstant(1);
                propertyb.SetGetMethod(mbGet);
                propertyb.SetSetMethod(mbSet);
                propertyb.AddOtherMethod(mbOther);