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

Code before:
            Assembly assembly = AssemblyLoadContext.Default.LoadFromStream(stream);
            MethodInfo method = assembly.EntryPoint;
            Assert.Equal("Main", method.Name);
            Assert.Equal(12, method.Invoke(null, null));
        }

        [Fact]
        public void PersistedAssemblyBuilder_GenerateMetadataWithNestedTypeEntryPoint()
        {
            PersistedAssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilder(new AssemblyName("MyAssemblyWithNestedEntryPoint"));
            TypeBuilder outerType = ab.DefineDynamicModule("MyModule").DefineType("OuterType", TypeAttributes.Public | TypeAttributes.Class);
            TypeBuilder nestedType = outerType.DefineNestedType("NestedType", TypeAttributes.NestedPublic | TypeAttributes.Class);
            MethodBuilder entryPoint = nestedType.DefineMethod("Main", MethodAttributes.HideBySig | MethodAttributes.Public | MethodAttributes.Static, typeof(int), null);
            ILGenerator il = entryPoint.GetILGenerator();
            il.Emit(OpCodes.Ldc_I4, 42);
            il.Emit(OpCodes.Ret);
            nestedType.CreateType();
            outerType.CreateType();

            MetadataBuilder metadataBuilder = ab.GenerateMetadata(out BlobBuilder ilStream, out BlobBuilder fieldData);
            ManagedPEBuilder peBuilder = new ManagedPEBuilder(
                header: new PEHeaderBuilder(imageCharacteristics: Characteristics.ExecutableImage, subsystem: Subsystem.WindowsCui),
                metadataRootBuilder: new MetadataRootBuilder(metadataBuilder),
                ilStream: ilStream,
                mappedFieldData: fieldData,
                entryPoint: MetadataTokens.MethodDefinitionHandle(entryPoint.MetadataToken));

            BlobBuilder peBlob = new BlobBuilder();
            peBuilder.Serialize(peBlob);

            using MemoryStream stream = new MemoryStream();
            peBlob.WriteContentTo(stream);
            stream.Position = 0;
            TestAssemblyLoadContext testAssemblyLoadContext = new();
            try
            {
                Assembly assembly = testAssemblyLoadContext.LoadFromStream(stream);
                MethodInfo method = assembly.EntryPoint;
                Assert.Equal("OuterType+NestedType", method.DeclaringType.FullName);
                Assert.Equal(42, method.Invoke(null, null));
            }
            finally
            {
                testAssemblyLoadContext.Unload();
            }
        }

        [Fact]
        public void PersistedAssemblyBuilder_GenerateMetadataWithGenericEntryPoint()
        {
            PersistedAssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilder(new AssemblyName("MyAssemblyWithGenericEntryPoint"));

Review comment (suggestion):
These tests vary `entryPoint` passed into `ManagedPEBuilder`. There are no new unique paths exercised in `PersistedAssemblyBuilder` compared to the existing `PersistedAssemblyBuilder_GenerateMetadataWithEntryPoint` test.

Code after:
                TypeBuilder tbNested = tb1.DefineNestedType("TypeNested", TypeAttributes.NestedPublic, typeof(object));
                tbNested.CreateType();

                // Generics
                TypeBuilder tbg = module.DefineType("GType1", TypeAttributes.Public, typeof(object));
                var gParams = tbg.DefineGenericParameters("K", "T");
                // Constraints
                gParams[0].SetBaseTypeConstraint(typeof(object));
                gParams[0].SetInterfaceConstraints([typeof(IComparable)]);
                gParams[0].SetCustomAttribute(cattrb);
                gParams[1].SetBaseTypeConstraint(tbg);
                // Type param
                tbg.DefineField("FieldGParam", tbg.GetGenericArguments()[0], FieldAttributes.Public | FieldAttributes.Static);
                // Open type
                tbg.DefineField("FieldListOfT", typeof(List<>).MakeGenericType([tbg.GetGenericArguments()[1]]), FieldAttributes.Public | FieldAttributes.Static);
                tbg.CreateType();

                TypeBuilder tbg2 = module.DefineType("GType2", TypeAttributes.Public, typeof(object));
                tbg2.DefineGenericParameters("K", "T");
                tbg2.CreateType();

                TypeBuilder tb3 = module.DefineType("Type3", TypeAttributes.Public, typeof(object));
                // Nested type
                tb3.DefineField("FieldNested", tbNested, FieldAttributes.Public | FieldAttributes.Static);
                // Nested type ref
                tb3.DefineField("FieldNestedRef", typeof(TimeZoneInfo.AdjustmentRule), FieldAttributes.Public | FieldAttributes.Static);
                // Double Nested type ref
                tb3.DefineField("FieldDoubleNestedRef", typeof(Outer.Inner.InnerMostType), FieldAttributes.Public | FieldAttributes.Static);
                // Primitive types
                tb3.DefineField("FieldInt", typeof(int), FieldAttributes.Public | FieldAttributes.Static);
                // Typeref array
                tb3.DefineField("FieldArrayTyperef", typeof(object[]), FieldAttributes.Public | FieldAttributes.Static);
                // Type szarray
                tb3.DefineField("FieldSzArray", tb1.MakeArrayType(), FieldAttributes.Public | FieldAttributes.Static);
                // Multi-dim non szarray
                tb3.DefineField("FieldNonSzArray", Array.CreateInstance(typeof(int), [10], [1]).GetType(), FieldAttributes.Public | FieldAttributes.Static);
                // Multi-dim array
                tb3.DefineField("FieldMultiDimArray", Array.CreateInstance(typeof(int), [10, 10], [1, 1]).GetType(), FieldAttributes.Public | FieldAttributes.Static);
                // Type pointer
                tb3.DefineField("FieldPointer", tb1.MakePointerType(), FieldAttributes.Public | FieldAttributes.Static);
                // Generic instance
                tb3.DefineField("FieldGListOfInt", typeof(List<int>), FieldAttributes.Public | FieldAttributes.Static);
                // Generic instance of tbuilder
                tb3.DefineField("FieldGInstTBuilder", tbg2.MakeGenericType([typeof(int), typeof(string)]), FieldAttributes.Public | FieldAttributes.Static);
                tb3.CreateType();

                // Fields
                TypeBuilder tbFields = module.DefineType("Type4", TypeAttributes.Public, typeof(object));
                // Field with a constant
                tbFields.DefineField("FieldInt", typeof(int), FieldAttributes.Public | FieldAttributes.Static | FieldAttributes.HasDefault | FieldAttributes.Literal).SetConstant(42);
                // Field with an offset