diff --git a/src/libraries/Microsoft.Extensions.DependencyInjection/src/ServiceLookup/ILEmit/ILEmitResolverBuilder.cs b/src/libraries/Microsoft.Extensions.DependencyInjection/src/ServiceLookup/ILEmit/ILEmitResolverBuilder.cs index 101b8e4f78b6e..9768694015720 100644 --- a/src/libraries/Microsoft.Extensions.DependencyInjection/src/ServiceLookup/ILEmit/ILEmitResolverBuilder.cs +++ b/src/libraries/Microsoft.Extensions.DependencyInjection/src/ServiceLookup/ILEmit/ILEmitResolverBuilder.cs @@ -108,7 +108,7 @@ private GeneratedMethod BuildTypeNoCache(ServiceCallSite callSite) var assembly = AssemblyBuilder.DefineDynamicAssembly(new AssemblyName(assemblyName), AssemblyBuilderAccess.RunAndSave); var module = assembly.DefineDynamicModule(assemblyName, fileName); #else - var assembly = AssemblyBuilder.DefinePersistedAssembly(new AssemblyName(assemblyName), typeof(object).Assembly); + var assembly = new PersistedAssemblyBuilder(new AssemblyName(assemblyName), typeof(object).Assembly); var module = assembly.DefineDynamicModule(assemblyName); #endif var type = module.DefineType(callSite.ServiceType.Name + "Resolver"); diff --git a/src/libraries/System.Private.CoreLib/src/Resources/Strings.resx b/src/libraries/System.Private.CoreLib/src/Resources/Strings.resx index b6d8fd00aa404..2f58fae3471e9 100644 --- a/src/libraries/System.Private.CoreLib/src/Resources/Strings.resx +++ b/src/libraries/System.Private.CoreLib/src/Resources/Strings.resx @@ -3346,7 +3346,7 @@ Object type {0} does not match target type {1}. - + Non-static field requires a target. @@ -4304,10 +4304,7 @@ Cannot set initonly static field '{0}' after type '{1}' is initialized. - - This AssemblyBuilder instance doesn't support saving. Use AssemblyBuilder.DefinePersistedAssembly to create an AssemblyBuilder instance that supports saving. - Blocking wait is not supported on the JS interop threads. - + \ No newline at end of file diff --git a/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/AssemblyBuilder.cs b/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/AssemblyBuilder.cs index 8599d39aa4b16..1b1220536b9e3 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/AssemblyBuilder.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/AssemblyBuilder.cs @@ -33,56 +33,8 @@ public ModuleBuilder DefineDynamicModule(string name) return GetDynamicModuleCore(name); } - /// - /// Defines an that can be saved to a file or stream. - /// - /// The name of the assembly. - /// The assembly that denotes the "system assembly" that houses the well-known types such as - /// A collection that contains the attributes of the assembly. - /// An that can be persisted. - /// The or or is null. - /// Currently the persisted assembly doesn't support running, need to save it and load back to run. - public static AssemblyBuilder DefinePersistedAssembly(AssemblyName name, Assembly coreAssembly, IEnumerable? assemblyAttributes = null) - { - ArgumentNullException.ThrowIfNull(name); - ArgumentException.ThrowIfNullOrEmpty(name.Name, "AssemblyName.Name"); - ArgumentNullException.ThrowIfNull(coreAssembly); - - Type assemblyType = Type.GetType("System.Reflection.Emit.AssemblyBuilderImpl, System.Reflection.Emit", throwOnError: true)!; - ConstructorInfo con = assemblyType.GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance, [typeof(AssemblyName), typeof(Assembly), typeof(IEnumerable)])!; - return (AssemblyBuilder)con.Invoke([name, coreAssembly, assemblyAttributes]); - } - protected abstract ModuleBuilder? GetDynamicModuleCore(string name); - /// - /// Serializes the assembly to . - /// - /// The to which the assembly serialized. - /// is null. - /// The AssemblyBuilder instance doesn't support saving. - public void Save(Stream stream) => SaveCore(stream); - - /// - /// Saves the assembly to disk. - /// - /// The file name of the assembly. - /// is null. - /// The AssemblyBuilder instance doesn't support saving. - public void Save(string assemblyFileName) - { - ArgumentNullException.ThrowIfNull(assemblyFileName); - - using var peStream = new FileStream(assemblyFileName, FileMode.Create, FileAccess.Write); - SaveCore(peStream); - } - - /// - /// When implemented in a derived type, serializes the assembly to a stream. - /// - /// The stream to which the assembly serialized. - protected virtual void SaveCore(Stream stream) => throw new NotSupportedException(SR.NotSupported_AssemblySave); - public void SetCustomAttribute(ConstructorInfo con, byte[] binaryAttribute) { ArgumentNullException.ThrowIfNull(con); diff --git a/src/libraries/System.Reflection.Emit/ref/System.Reflection.Emit.cs b/src/libraries/System.Reflection.Emit/ref/System.Reflection.Emit.cs index 1cd9aa71da743..3690bf72a4c87 100644 --- a/src/libraries/System.Reflection.Emit/ref/System.Reflection.Emit.cs +++ b/src/libraries/System.Reflection.Emit/ref/System.Reflection.Emit.cs @@ -24,7 +24,6 @@ protected AssemblyBuilder() { } public static System.Reflection.Emit.AssemblyBuilder DefineDynamicAssembly(System.Reflection.AssemblyName name, System.Reflection.Emit.AssemblyBuilderAccess access) { throw null; } [System.Diagnostics.CodeAnalysis.RequiresDynamicCode("Defining a dynamic assembly requires dynamic code.")] public static System.Reflection.Emit.AssemblyBuilder DefineDynamicAssembly(System.Reflection.AssemblyName name, System.Reflection.Emit.AssemblyBuilderAccess access, System.Collections.Generic.IEnumerable? assemblyAttributes) { throw null; } - public static System.Reflection.Emit.AssemblyBuilder DefinePersistedAssembly(System.Reflection.AssemblyName name, System.Reflection.Assembly coreAssembly, System.Collections.Generic.IEnumerable? assemblyAttributes = null) { throw null; } public System.Reflection.Emit.ModuleBuilder DefineDynamicModule(string name) { throw null; } protected abstract System.Reflection.Emit.ModuleBuilder DefineDynamicModuleCore(string name); public override bool Equals(object? obj) { throw null; } @@ -55,9 +54,6 @@ protected AssemblyBuilder() { } [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("Types might be removed by trimming. If the type name is a string literal, consider using Type.GetType instead.")] public override System.Type? GetType(string name, bool throwOnError, bool ignoreCase) { throw null; } public override bool IsDefined(System.Type attributeType, bool inherit) { throw null; } - public void Save(string assemblyFileName) { throw null; } - public void Save(System.IO.Stream stream) { throw null; } - protected virtual void SaveCore(System.IO.Stream stream) { } public void SetCustomAttribute(System.Reflection.ConstructorInfo con, byte[] binaryAttribute) { } public void SetCustomAttribute(System.Reflection.Emit.CustomAttributeBuilder customBuilder) { } protected abstract void SetCustomAttributeCore(System.Reflection.ConstructorInfo con, System.ReadOnlySpan binaryAttribute); @@ -474,6 +470,24 @@ public void SetCustomAttribute(System.Reflection.ConstructorInfo con, byte[] bin public void SetCustomAttribute(System.Reflection.Emit.CustomAttributeBuilder customBuilder) { } protected abstract void SetCustomAttributeCore(System.Reflection.ConstructorInfo con, System.ReadOnlySpan binaryAttribute); } +#if !BUILDING_CORELIB_REFERENCE + public sealed class PersistedAssemblyBuilder : System.Reflection.Emit.AssemblyBuilder + { + public PersistedAssemblyBuilder(System.Reflection.AssemblyName name, System.Reflection.Assembly coreAssembly, System.Collections.Generic.IEnumerable? assemblyAttributes = null) { } + public override string? FullName { get { throw null; } } + public override bool IsDynamic { get { throw null; } } + public override System.Reflection.Module ManifestModule { get { throw null; } } + [System.Diagnostics.CodeAnalysis.RequiresDynamicCode("Defining a dynamic assembly requires dynamic code.")] + protected override System.Reflection.Emit.ModuleBuilder DefineDynamicModuleCore(string name) { throw null; } + protected override System.Reflection.Emit.ModuleBuilder? GetDynamicModuleCore(string name) { throw null; } + [System.CLSCompliantAttribute(false)] + public System.Reflection.Metadata.Ecma335.MetadataBuilder GenerateMetadata(out System.Reflection.Metadata.BlobBuilder ilStream, out System.Reflection.Metadata.BlobBuilder mappedFieldData) { throw null; } + public override System.Reflection.AssemblyName GetName(bool copiedName) { throw null; } + public void Save(string assemblyFileName) { throw null; } + public void Save(System.IO.Stream stream) { throw null; } + protected override void SetCustomAttributeCore(System.Reflection.ConstructorInfo con, System.ReadOnlySpan binaryAttribute) { throw null; } + } +#endif public abstract partial class PropertyBuilder : System.Reflection.PropertyInfo { protected PropertyBuilder() { } diff --git a/src/libraries/System.Reflection.Emit/ref/System.Reflection.Emit.csproj b/src/libraries/System.Reflection.Emit/ref/System.Reflection.Emit.csproj index 4872ff7f5b6b0..44f0feefe67db 100644 --- a/src/libraries/System.Reflection.Emit/ref/System.Reflection.Emit.csproj +++ b/src/libraries/System.Reflection.Emit/ref/System.Reflection.Emit.csproj @@ -10,5 +10,6 @@ + \ No newline at end of file diff --git a/src/libraries/System.Reflection.Emit/src/ILLink/ILLink.Descriptors.LibraryBuild.xml b/src/libraries/System.Reflection.Emit/src/ILLink/ILLink.Descriptors.LibraryBuild.xml deleted file mode 100644 index 8cf11ab4c01f7..0000000000000 --- a/src/libraries/System.Reflection.Emit/src/ILLink/ILLink.Descriptors.LibraryBuild.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - diff --git a/src/libraries/System.Reflection.Emit/src/Resources/Strings.resx b/src/libraries/System.Reflection.Emit/src/Resources/Strings.resx index 0688551573229..4ab50a83684c0 100644 --- a/src/libraries/System.Reflection.Emit/src/Resources/Strings.resx +++ b/src/libraries/System.Reflection.Emit/src/Resources/Strings.resx @@ -117,8 +117,8 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - Cannot save an assembly multiple times. + + Cannot populate assembly metadata multiple times. Cannot set parent to an interface. @@ -249,9 +249,6 @@ Type passed must be an interface. - - Abstract method '{0}' in type '{1}' does not have an implementation. - Method '{0}' cannot have a method body diff --git a/src/libraries/System.Reflection.Emit/src/System.Reflection.Emit.csproj b/src/libraries/System.Reflection.Emit/src/System.Reflection.Emit.csproj index 3b2f29ec3672f..0c34ef45f4486 100644 --- a/src/libraries/System.Reflection.Emit/src/System.Reflection.Emit.csproj +++ b/src/libraries/System.Reflection.Emit/src/System.Reflection.Emit.csproj @@ -14,7 +14,6 @@ - @@ -24,6 +23,7 @@ + diff --git a/src/libraries/System.Reflection.Emit/src/System/Reflection/Emit/ModuleBuilderImpl.cs b/src/libraries/System.Reflection.Emit/src/System/Reflection/Emit/ModuleBuilderImpl.cs index b95e74c7846c3..c79e95962e3db 100644 --- a/src/libraries/System.Reflection.Emit/src/System/Reflection/Emit/ModuleBuilderImpl.cs +++ b/src/libraries/System.Reflection.Emit/src/System/Reflection/Emit/ModuleBuilderImpl.cs @@ -16,7 +16,7 @@ internal sealed class ModuleBuilderImpl : ModuleBuilder private readonly Assembly _coreAssembly; private readonly string _name; private readonly MetadataBuilder _metadataBuilder; - private readonly AssemblyBuilderImpl _assemblyBuilder; + private readonly PersistedAssemblyBuilder _assemblyBuilder; private readonly TypeBuilderImpl _globalTypeBuilder; private readonly Dictionary _assemblyReferences = new(); private readonly Dictionary _typeReferences = new(); @@ -37,7 +37,7 @@ internal sealed class ModuleBuilderImpl : ModuleBuilder private static readonly Type[] s_coreTypes = { typeof(void), typeof(object), typeof(bool), typeof(char), typeof(sbyte), typeof(byte), typeof(short), typeof(ushort), typeof(int), typeof(uint), typeof(long), typeof(ulong), typeof(float), typeof(double), typeof(string), typeof(nint), typeof(nuint), typeof(TypedReference) }; - internal ModuleBuilderImpl(string name, Assembly coreAssembly, MetadataBuilder builder, AssemblyBuilderImpl assemblyBuilder) + internal ModuleBuilderImpl(string name, Assembly coreAssembly, MetadataBuilder builder, PersistedAssemblyBuilder assemblyBuilder) { _coreAssembly = coreAssembly; _name = name; diff --git a/src/libraries/System.Reflection.Emit/src/System/Reflection/Emit/AssemblyBuilderImpl.cs b/src/libraries/System.Reflection.Emit/src/System/Reflection/Emit/PersistedAssemblyBuilder.cs similarity index 50% rename from src/libraries/System.Reflection.Emit/src/System/Reflection/Emit/AssemblyBuilderImpl.cs rename to src/libraries/System.Reflection.Emit/src/System/Reflection/Emit/PersistedAssemblyBuilder.cs index b52bec9f11c4a..12fbf4704b7ba 100644 --- a/src/libraries/System.Reflection.Emit/src/System/Reflection/Emit/AssemblyBuilderImpl.cs +++ b/src/libraries/System.Reflection.Emit/src/System/Reflection/Emit/PersistedAssemblyBuilder.cs @@ -9,18 +9,34 @@ namespace System.Reflection.Emit { - internal sealed class AssemblyBuilderImpl : AssemblyBuilder + /// + /// AssemblyBuilder implementation that can persist assembly to a disk or stream. + /// + public sealed class PersistedAssemblyBuilder : AssemblyBuilder { private readonly AssemblyName _assemblyName; private readonly Assembly _coreAssembly; private readonly MetadataBuilder _metadataBuilder; private ModuleBuilderImpl? _module; - private bool _previouslySaved; + private bool _isMetadataPopulated; internal List? _customAttributes; - internal AssemblyBuilderImpl(AssemblyName name, Assembly coreAssembly, IEnumerable? assemblyAttributes = null) + /// + /// Creates a instance that can be saved to a file or stream. + /// + /// The name of the assembly. + /// The assembly that denotes the "system assembly" that houses the well-known types such as + /// A collection that contains the attributes of the assembly. + /// An that can be persisted. + /// The or or is null. + /// Currently the persisted assembly doesn't support running, need to save it and load back to run. + public PersistedAssemblyBuilder(AssemblyName name, Assembly coreAssembly, IEnumerable? assemblyAttributes = null) { + ArgumentNullException.ThrowIfNull(name); + ArgumentException.ThrowIfNullOrEmpty(name.Name, "AssemblyName.Name"); + ArgumentNullException.ThrowIfNull(coreAssembly); + _assemblyName = (AssemblyName)name.Clone(); _coreAssembly = coreAssembly; _metadataBuilder = new MetadataBuilder(); @@ -54,20 +70,70 @@ private void WritePEImage(Stream peStream, BlobBuilder ilBuilder, BlobBuilder fi peBlob.WriteContentTo(peStream); } - protected override void SaveCore(Stream stream) + /// + /// Serializes the assembly to . + /// + /// The to which the assembly serialized. + /// is null. + /// A module not defined for the assembly. + /// The metadata already populated for the assembly before. + public void Save(Stream stream) => SaveInternal(stream); + + /// + /// Saves the assembly to disk. + /// + /// The file name of the assembly. + /// is null. + /// A module not defined for the assembly. + /// The metadata already populated for the assembly before. + public void Save(string assemblyFileName) + { + ArgumentNullException.ThrowIfNull(assemblyFileName); + + using var peStream = new FileStream(assemblyFileName, FileMode.Create, FileAccess.Write); + SaveInternal(peStream); + } + + private void SaveInternal(Stream stream) { ArgumentNullException.ThrowIfNull(stream); + PopulateAssemblyMetadata(out BlobBuilder ilStream, out BlobBuilder fieldData); + WritePEImage(stream, ilStream, fieldData); + } + + + /// + /// Generates the metadata for the . + /// + /// Outputs bytes that includes all method's IL (body) emitted. + /// Outputs bytes that includes all field RVA data defined in the assembly. + /// A that includes all members defined in the Assembly. + /// A module not defined for the assembly. + /// The metadata already populated for the assembly before. + [CLSCompliant(false)] + public MetadataBuilder GenerateMetadata(out BlobBuilder ilStream, out BlobBuilder mappedFieldData) + { + PopulateAssemblyMetadata(out ilStream, out mappedFieldData); + + return _metadataBuilder; + } + + private void PopulateAssemblyMetadata(out BlobBuilder ilStream, out BlobBuilder fieldData) + { if (_module == null) { throw new InvalidOperationException(SR.InvalidOperation_AModuleRequired); } - if (_previouslySaved) // Cannot save an assembly multiple times. This is consistent with Save() in .Net Framework. + if (_isMetadataPopulated) // Cannot populate assembly metadata multiple times. This is consistent with Save() in .Net Framework. { - throw new InvalidOperationException(SR.InvalidOperation_CannotSaveMultipleTimes); + throw new InvalidOperationException(SR.InvalidOperation_CannotPopulateMultipleTimes); } + ilStream = new BlobBuilder(); + fieldData = new BlobBuilder(); + // Add assembly metadata AssemblyDefinitionHandle assemblyHandle = _metadataBuilder.AddAssembly( _metadataBuilder.GetOrAddString(value: _assemblyName.Name!), @@ -79,15 +145,10 @@ protected override void SaveCore(Stream stream) hashAlgorithm: (AssemblyHashAlgorithm)_assemblyName.HashAlgorithm #pragma warning restore SYSLIB0037 ); - _module.WriteCustomAttributes(_customAttributes, assemblyHandle); - var ilBuilder = new BlobBuilder(); - var fieldDataBuilder = new BlobBuilder(); - MethodBodyStreamEncoder methodBodyEncoder = new MethodBodyStreamEncoder(ilBuilder); - _module.AppendMetadata(methodBodyEncoder, fieldDataBuilder); - - WritePEImage(stream, ilBuilder, fieldDataBuilder); - _previouslySaved = true; + _module.WriteCustomAttributes(_customAttributes, assemblyHandle); + _module.AppendMetadata(new MethodBodyStreamEncoder(ilStream), fieldData); + _isMetadataPopulated = true; } private static AssemblyFlags AddContentType(AssemblyFlags flags, AssemblyContentType contentType) diff --git a/src/libraries/System.Reflection.Emit/tests/PersistableAssemblyBuilder/AssemblySaveAssemblyBuilder.cs b/src/libraries/System.Reflection.Emit/tests/PersistedAssemblyBuilder/AssemblySaveAssemblyBuilderTests.cs similarity index 85% rename from src/libraries/System.Reflection.Emit/tests/PersistableAssemblyBuilder/AssemblySaveAssemblyBuilder.cs rename to src/libraries/System.Reflection.Emit/tests/PersistedAssemblyBuilder/AssemblySaveAssemblyBuilderTests.cs index 5dbabe683e073..45f926b1a67bd 100644 --- a/src/libraries/System.Reflection.Emit/tests/PersistableAssemblyBuilder/AssemblySaveAssemblyBuilder.cs +++ b/src/libraries/System.Reflection.Emit/tests/PersistedAssemblyBuilder/AssemblySaveAssemblyBuilderTests.cs @@ -5,14 +5,19 @@ using System.Globalization; using System.IO; using System.Linq; +using System.Reflection.Metadata; +using System.Reflection.Metadata.Ecma335; +using System.Reflection.PortableExecutable; using System.Runtime.InteropServices; +using System.Runtime.Loader; using Xunit; namespace System.Reflection.Emit.Tests { [ConditionalClass(typeof(PlatformDetection), nameof(PlatformDetection.IsNotBrowser))] - public class AssemblySaveAssemblyBuilder + public class AssemblySaveAssemblyBuilderTests { + private readonly AssemblyName _assemblyName = new AssemblyName("MyAssembly"); public class Outer { public class Inner @@ -24,6 +29,82 @@ void DoNothing () { } } } + [Fact] + public void PersistedAssemblyBuilder_ConstructorValidations() + { + Assert.Throws("name", () => new PersistedAssemblyBuilder(null, typeof(object).Assembly)); + Assert.Throws("coreAssembly", () => new PersistedAssemblyBuilder(_assemblyName, null)); + Assert.Throws("AssemblyName.Name", () => AssemblySaveTools.PopulateAssemblyBuilder(new AssemblyName())); + } + + [Fact] + public void PersistedAssemblyBuilder_SaveValidations() + { + PersistedAssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilder(_assemblyName); + + Assert.Throws("assemblyFileName", () => ab.Save(assemblyFileName: null)); + Assert.Throws("stream", () => ab.Save(stream: null)); + Assert.Throws(() => ab.Save(assemblyFileName: "File")); // no module defined + } + + [Fact] + public void PersistedAssemblyBuilder_GenerateMetadataValidation() + { + PersistedAssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilder(_assemblyName); + Assert.Throws(() => ab.GenerateMetadata(out var _, out var _)); // no module defined + ab.DefineDynamicModule("MyModule"); + MetadataBuilder metadata = ab.GenerateMetadata(out var ilStream, out var mappedFieldData); + Assert.NotNull(metadata); + Assert.NotNull(ilStream); + Assert.NotNull(mappedFieldData); + Assert.Throws(() => ab.GenerateMetadata(out var _, out var _)); // cannot re-generate metadata + } + + [Fact] + public void PersistedAssemblyBuilder_GenerateMetadataWithEntryPoint() + { + PersistedAssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilder(_assemblyName); + TypeBuilder tb = ab.DefineDynamicModule("MyModule").DefineType("MyType", TypeAttributes.Public | TypeAttributes.Class); + MethodBuilder mb1 = tb.DefineMethod("SumMethod", MethodAttributes.Public | MethodAttributes.Static, typeof(int), [typeof(int), typeof(int)]); + ILGenerator il = mb1.GetILGenerator(); + il.Emit(OpCodes.Ldarg_0); + il.Emit(OpCodes.Ldarg_1); + il.Emit(OpCodes.Add); + il.Emit(OpCodes.Ret); + MethodBuilder entryPoint = tb.DefineMethod("Main", MethodAttributes.HideBySig | MethodAttributes.Public | MethodAttributes.Static, typeof(int), null); + ILGenerator il2 = entryPoint.GetILGenerator(); + il2.Emit(OpCodes.Ldc_I4_S, 10); + il2.Emit(OpCodes.Ldc_I4_2); + il2.Emit(OpCodes.Call, mb1); + il2.Emit(OpCodes.Ret); + tb.CreateType(); + + MetadataBuilder metadataBuilder = ab.GenerateMetadata(out BlobBuilder ilStream, out BlobBuilder fieldData); + PEHeaderBuilder peHeaderBuilder = new PEHeaderBuilder( + imageCharacteristics: Characteristics.ExecutableImage, + subsystem: Subsystem.WindowsCui); + + ManagedPEBuilder peBuilder = new ManagedPEBuilder( + header: peHeaderBuilder, + metadataRootBuilder: new MetadataRootBuilder(metadataBuilder), + ilStream: ilStream, + mappedFieldData: fieldData, + entryPoint: MetadataTokens.MethodDefinitionHandle(entryPoint.MetadataToken)); + + BlobBuilder peBlob = new BlobBuilder(); + peBuilder.Serialize(peBlob); + + // in case saving to a file: + using var stream = new MemoryStream(); + peBlob.WriteContentTo(stream); + + stream.Seek(0, SeekOrigin.Begin); + 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 AssemblyWithDifferentTypes() { @@ -34,7 +115,7 @@ public void AssemblyWithDifferentTypes() aName.CultureInfo = new CultureInfo("en"); aName.Flags = AssemblyNameFlags.Retargetable; - AssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilder(aName); + PersistedAssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilder(aName); ab.SetCustomAttribute(new CustomAttributeBuilder(typeof(AssemblyDelaySignAttribute).GetConstructor([typeof(bool)]), [true])); diff --git a/src/libraries/System.Reflection.Emit/tests/PersistableAssemblyBuilder/AssemblySaveConstructorBuilderTests.cs b/src/libraries/System.Reflection.Emit/tests/PersistedAssemblyBuilder/AssemblySaveConstructorBuilderTests.cs similarity index 94% rename from src/libraries/System.Reflection.Emit/tests/PersistableAssemblyBuilder/AssemblySaveConstructorBuilderTests.cs rename to src/libraries/System.Reflection.Emit/tests/PersistedAssemblyBuilder/AssemblySaveConstructorBuilderTests.cs index 618b01b2a9ae0..34fe5c2aec327 100644 --- a/src/libraries/System.Reflection.Emit/tests/PersistableAssemblyBuilder/AssemblySaveConstructorBuilderTests.cs +++ b/src/libraries/System.Reflection.Emit/tests/PersistedAssemblyBuilder/AssemblySaveConstructorBuilderTests.cs @@ -15,7 +15,7 @@ public void DefineConstructorsTest() { using (TempFile file = TempFile.Create()) { - AssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder type); + PersistedAssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder type); ConstructorBuilder constructor = type.DefineDefaultConstructor(MethodAttributes.Public); ConstructorBuilder constructor2 = type.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, [typeof(int)]); constructor2.DefineParameter(1, ParameterAttributes.None, "parameter1"); @@ -51,7 +51,7 @@ public void DefineConstructorsTest() [Fact] public void DefineDefaultConstructor_WithTypeBuilderParent() { - AssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder type); + PersistedAssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder type); type.CreateType(); TypeBuilder child = ab.GetDynamicModule("MyModule").DefineType("ChildType", TypeAttributes.Public | TypeAttributes.Class); child.SetParent(type); @@ -69,7 +69,7 @@ public void DefineDefaultConstructor_TypesWithGenericParents() { using (TempFile file = TempFile.Create()) { - AssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder type); + PersistedAssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder type); type.DefineGenericParameters("T"); ConstructorBuilder constructor = type.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, Type.EmptyTypes); FieldBuilder field = type.DefineField("TestField", typeof(bool), FieldAttributes.Public | FieldAttributes.Static); @@ -112,7 +112,7 @@ public void DefineDefaultConstructor_TypesWithGenericParents() [Fact] public void DefineDefaultConstructor_Interface_ThrowsInvalidOperationException() { - AssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilder(new AssemblyName("MyAssembly")); + PersistedAssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilder(new AssemblyName("MyAssembly")); TypeBuilder type = ab.DefineDynamicModule("MyModule").DefineType("MyType", TypeAttributes.Public | TypeAttributes.Interface | TypeAttributes.Abstract); Assert.Throws(() => type.DefineDefaultConstructor(MethodAttributes.Public)); } @@ -120,7 +120,7 @@ public void DefineDefaultConstructor_Interface_ThrowsInvalidOperationException() [Fact] public void DefineDefaultConstructor_ThrowsNotSupportedException_IfParentNotCreated() { - AssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder type); + PersistedAssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder type); TypeBuilder child = ab.GetDynamicModule("MyModule").DefineType("MyType", TypeAttributes.Public); child.SetParent(type); Assert.Throws(() => child.DefineDefaultConstructor(MethodAttributes.Public)); @@ -136,7 +136,7 @@ public void DefineDefaultConstructor_StaticVirtual_ThrowsArgumentException() [Fact] public void DefineDefaultConstructor_ParentNoDefaultConstructor_ThrowsNotSupportedException() { - AssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder type); + PersistedAssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder type); FieldBuilder field = type.DefineField("TestField", typeof(int), FieldAttributes.Family); ConstructorBuilder constructor = type.DefineConstructor(MethodAttributes.Public, CallingConventions.HasThis, new[] { typeof(int) }); diff --git a/src/libraries/System.Reflection.Emit/tests/PersistableAssemblyBuilder/AssemblySaveCustomAttributeTests.cs b/src/libraries/System.Reflection.Emit/tests/PersistedAssemblyBuilder/AssemblySaveCustomAttributeTests.cs similarity index 98% rename from src/libraries/System.Reflection.Emit/tests/PersistableAssemblyBuilder/AssemblySaveCustomAttributeTests.cs rename to src/libraries/System.Reflection.Emit/tests/PersistedAssemblyBuilder/AssemblySaveCustomAttributeTests.cs index 35f04f3776cce..1fd2d53188361 100644 --- a/src/libraries/System.Reflection.Emit/tests/PersistableAssemblyBuilder/AssemblySaveCustomAttributeTests.cs +++ b/src/libraries/System.Reflection.Emit/tests/PersistedAssemblyBuilder/AssemblySaveCustomAttributeTests.cs @@ -125,7 +125,7 @@ private static void WriteAssemblyToDisk(AssemblyName assemblyName, Type[] types, List? moduleAttributes = null, List? typeAttributes = null, List? methodAttributes = null, List? fieldAttributes = null) { - AssemblyBuilder assemblyBuilder = AssemblySaveTools.PopulateAssemblyBuilder(assemblyName, assemblyAttributes); + PersistedAssemblyBuilder assemblyBuilder = AssemblySaveTools.PopulateAssemblyBuilder(assemblyName, assemblyAttributes); ModuleBuilder mb = assemblyBuilder.DefineDynamicModule(assemblyName.Name); PopulateMembersForModule(mb, types, moduleAttributes, typeAttributes, methodAttributes, fieldAttributes); assemblyBuilder.Save(fileLocation); @@ -194,7 +194,7 @@ public void CreateStructWithPseudoCustomAttributesTest() new CustomAttributeBuilder(typeof(SpecialNameAttribute).GetConstructor(Type.EmptyTypes), new object[] { }) }; - AssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilder(PopulateAssemblyName()); + PersistedAssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilder(PopulateAssemblyName()); TypeBuilder tb = ab.DefineDynamicModule("Module").DefineType(type.FullName, type.Attributes, type.BaseType); DefineFieldsAndSetAttributes(fieldAttributes.ToList(), type.GetFields(), tb); typeAttributes.ForEach(tb.SetCustomAttribute); @@ -286,7 +286,7 @@ public void InterfacesWithPseudoCustomAttributes() new CustomAttributeBuilder(marshalAsEnumCtor, new object[] { UnmanagedType.CustomMarshaler }, new FieldInfo[] { typeof(MarshalAsAttribute).GetField("MarshalType")}, new object[] { typeof(EmptyTestClass).AssemblyQualifiedName })}; - AssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilder(PopulateAssemblyName()); + PersistedAssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilder(PopulateAssemblyName()); TypeBuilder tb = ab.DefineDynamicModule("Module").DefineType(type.FullName, type.Attributes); typeAttributes.ForEach(tb.SetCustomAttribute); DefineMethodsAndSetAttributes(methodAttributes, tb, type.GetMethods(), parameterAttributes); @@ -439,7 +439,7 @@ public void MarshalAsPseudoCustomAttributesTest(CustomAttributeBuilder attribute using (TempFile file = TempFile.Create()) { Type type = typeof(StructWithFields); - AssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilder(PopulateAssemblyName()); + PersistedAssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilder(PopulateAssemblyName()); TypeBuilder tb = ab.DefineDynamicModule("Module").DefineType(type.FullName, type.Attributes, type.BaseType); FieldInfo stringField = type.GetFields()[1]; FieldBuilder fb = tb.DefineField(stringField.Name, stringField.FieldType, stringField.Attributes); @@ -477,7 +477,7 @@ public void EnumBuilderSetCustomAttributesTest() { using (TempFile file = TempFile.Create()) { - AssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilder(PopulateAssemblyName()); + PersistedAssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilder(PopulateAssemblyName()); EnumBuilder enumBuilder = ab.DefineDynamicModule("Module").DefineEnum("TestEnum", TypeAttributes.Public, typeof(int)); ConstructorInfo attributeConstructor = typeof(BoolAttribute).GetConstructor(new Type[] { typeof(bool) }); diff --git a/src/libraries/System.Reflection.Emit/tests/PersistableAssemblyBuilder/AssemblySaveEnumBuilderTests.cs b/src/libraries/System.Reflection.Emit/tests/PersistedAssemblyBuilder/AssemblySaveEnumBuilderTests.cs similarity index 95% rename from src/libraries/System.Reflection.Emit/tests/PersistableAssemblyBuilder/AssemblySaveEnumBuilderTests.cs rename to src/libraries/System.Reflection.Emit/tests/PersistedAssemblyBuilder/AssemblySaveEnumBuilderTests.cs index 2711ba48db97f..29a94bacd15d8 100644 --- a/src/libraries/System.Reflection.Emit/tests/PersistableAssemblyBuilder/AssemblySaveEnumBuilderTests.cs +++ b/src/libraries/System.Reflection.Emit/tests/PersistedAssemblyBuilder/AssemblySaveEnumBuilderTests.cs @@ -64,7 +64,7 @@ public void DefineLiteral(Type underlyingType, object literalValue) { using (TempFile file = TempFile.Create()) { - EnumBuilder enumBuilder = CreateAssemblyAndDefineEnum(out AssemblyBuilder assemblyBuilder, out TypeBuilder type, underlyingType); + EnumBuilder enumBuilder = CreateAssemblyAndDefineEnum(out PersistedAssemblyBuilder assemblyBuilder, out TypeBuilder type, underlyingType); FieldBuilder literal = enumBuilder.DefineLiteral("FieldOne", literalValue); enumBuilder.CreateTypeInfo(); type.CreateTypeInfo(); @@ -97,7 +97,7 @@ public void SaveArrayTypeSignature(int rank, string name) { using (TempFile file = TempFile.Create()) { - EnumBuilder enumBuilder = CreateAssemblyAndDefineEnum(out AssemblyBuilder ab, out TypeBuilder tb); + EnumBuilder enumBuilder = CreateAssemblyAndDefineEnum(out PersistedAssemblyBuilder ab, out TypeBuilder tb); Type arrayType = rank == 0 ? enumBuilder.MakeArrayType() : enumBuilder.MakeArrayType(rank); MethodBuilder mb = tb.DefineMethod("TestMethod", MethodAttributes.Public); mb.SetReturnType(arrayType); @@ -118,7 +118,7 @@ public void SaveArrayTypeSignature(int rank, string name) } } - private EnumBuilder CreateAssemblyAndDefineEnum(out AssemblyBuilder assemblyBuilder, + private EnumBuilder CreateAssemblyAndDefineEnum(out PersistedAssemblyBuilder assemblyBuilder, out TypeBuilder type, Type? underlyingType = null) { assemblyBuilder = AssemblySaveTools.PopulateAssemblyBuilder(PopulateAssemblyName()); @@ -140,7 +140,7 @@ public void SaveByRefTypeSignature() { using (TempFile file = TempFile.Create()) { - EnumBuilder eb = CreateAssemblyAndDefineEnum(out AssemblyBuilder assemblyBuilder, out TypeBuilder tb); + EnumBuilder eb = CreateAssemblyAndDefineEnum(out PersistedAssemblyBuilder assemblyBuilder, out TypeBuilder tb); Type byrefType = eb.MakeByRefType(); MethodBuilder mb = tb.DefineMethod("TestMethod", MethodAttributes.Public); mb.SetReturnType(byrefType); @@ -173,7 +173,7 @@ public void SavePointerTypeSignature() { using (TempFile file = TempFile.Create()) { - EnumBuilder eb = CreateAssemblyAndDefineEnum(out AssemblyBuilder assemblyBuilder, out TypeBuilder tb); + EnumBuilder eb = CreateAssemblyAndDefineEnum(out PersistedAssemblyBuilder assemblyBuilder, out TypeBuilder tb); Type pointerType = eb.MakePointerType(); MethodBuilder mb = tb.DefineMethod("TestMethod", MethodAttributes.Public); mb.SetReturnType(pointerType); @@ -213,7 +213,7 @@ public void EnumTypeField_DefaultValueShouldMatchUnderlyingType() { using (TempFile file = TempFile.Create()) { - AssemblyBuilder assemblyBuilder = AssemblySaveTools.PopulateAssemblyBuilder(PopulateAssemblyName()); + PersistedAssemblyBuilder assemblyBuilder = AssemblySaveTools.PopulateAssemblyBuilder(PopulateAssemblyName()); ModuleBuilder mb = assemblyBuilder.DefineDynamicModule("My Module"); TypeBuilder tb = mb.DefineType("TestType", TypeAttributes.Class | TypeAttributes.Public); EnumBuilder eb = mb.DefineEnum("TestEnum", TypeAttributes.Public, typeof(int)); diff --git a/src/libraries/System.Reflection.Emit/tests/PersistableAssemblyBuilder/AssemblySaveEventBuilderTests.cs b/src/libraries/System.Reflection.Emit/tests/PersistedAssemblyBuilder/AssemblySaveEventBuilderTests.cs similarity index 97% rename from src/libraries/System.Reflection.Emit/tests/PersistableAssemblyBuilder/AssemblySaveEventBuilderTests.cs rename to src/libraries/System.Reflection.Emit/tests/PersistedAssemblyBuilder/AssemblySaveEventBuilderTests.cs index 65250fb332003..f4692afe19498 100644 --- a/src/libraries/System.Reflection.Emit/tests/PersistableAssemblyBuilder/AssemblySaveEventBuilderTests.cs +++ b/src/libraries/System.Reflection.Emit/tests/PersistedAssemblyBuilder/AssemblySaveEventBuilderTests.cs @@ -17,7 +17,7 @@ public void DefineEventAndItsAccessors() { using (TempFile file = TempFile.Create()) { - AssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder type); + PersistedAssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder type); EventBuilder eventType = type.DefineEvent("TestEvent", EventAttributes.SpecialName, typeof(int)); MethodBuilder addMethod = type.DefineMethod("AddMethod", MethodAttributes.Public | MethodAttributes.SpecialName); MethodBuilder addMethod2 = type.DefineMethod("AddMethod2", MethodAttributes.Public | MethodAttributes.HideBySig, typeof(int), Type.EmptyTypes); @@ -102,7 +102,7 @@ public void ReferenceEventInIL() { using (TempFile file = TempFile.Create()) { - AssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder type); + PersistedAssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder type); TypeBuilder delegateType = ab.GetDynamicModule("MyModule").DefineType("OnMissingString", TypeAttributes.Public | TypeAttributes.Sealed, typeof(MulticastDelegate)); delegateType.DefineMethod("Invoke", MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.NewSlot | MethodAttributes.Virtual, typeof(void), [typeof(string)]).SetImplementationFlags(MethodImplAttributes.Runtime); diff --git a/src/libraries/System.Reflection.Emit/tests/PersistableAssemblyBuilder/AssemblySaveILGeneratorTests.cs b/src/libraries/System.Reflection.Emit/tests/PersistedAssemblyBuilder/AssemblySaveILGeneratorTests.cs similarity index 96% rename from src/libraries/System.Reflection.Emit/tests/PersistableAssemblyBuilder/AssemblySaveILGeneratorTests.cs rename to src/libraries/System.Reflection.Emit/tests/PersistedAssemblyBuilder/AssemblySaveILGeneratorTests.cs index 2fd95fa874cbe..faded2adaa8b3 100644 --- a/src/libraries/System.Reflection.Emit/tests/PersistableAssemblyBuilder/AssemblySaveILGeneratorTests.cs +++ b/src/libraries/System.Reflection.Emit/tests/PersistedAssemblyBuilder/AssemblySaveILGeneratorTests.cs @@ -17,7 +17,7 @@ public void MethodWithEmptyBody() { using (TempFile file = TempFile.Create()) { - AssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder type); + PersistedAssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder type); MethodBuilder methodBuilder = type.DefineMethod("EmptyMethod", MethodAttributes.Public, typeof(void), [typeof(Version)]); ILGenerator il = methodBuilder.GetILGenerator(); il.Emit(OpCodes.Ret); @@ -46,7 +46,7 @@ public void MethodReturning_Int(int size) { using (TempFile file = TempFile.Create()) { - AssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder type); + PersistedAssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder type); MethodBuilder method = type.DefineMethod("TestMethod", MethodAttributes.Public | MethodAttributes.Static, typeof(int), Type.EmptyTypes); ILGenerator ilGenerator = method.GetILGenerator(size); @@ -77,7 +77,7 @@ public void TypeWithTwoMethod_ReferenceMethodArguments(int multiplier) { using (TempFile file = TempFile.Create()) { - AssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder type); + PersistedAssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder type); MethodBuilder multiplyMethod = type.DefineMethod("MultiplyMethod", MethodAttributes.Public | MethodAttributes.Static, typeof(int), [typeof(int)]); multiplyMethod.DefineParameter(1, ParameterAttributes.None, "myParam"); MethodBuilder addMethod = type.DefineMethod("AddMethod", MethodAttributes.Public | MethodAttributes.Static, typeof(int), [typeof(int), typeof(int)]); @@ -123,7 +123,7 @@ public void MultipleTypesWithMultipleMethods() { using (TempFile file = TempFile.Create()) { - AssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder type); + PersistedAssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder type); MethodBuilder multiplyMethod = type.DefineMethod("MultiplyMethod", MethodAttributes.Public, typeof(short), [typeof(short)]); MethodBuilder addMethod = type.DefineMethod("AddMethod", MethodAttributes.Public | MethodAttributes.Static, typeof(double), [typeof(double)]); @@ -187,7 +187,7 @@ public void MultipleTypesWithMultipleMethods() [Fact] public void ILOffset_Test() { - AssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder type); + PersistedAssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder type); MethodBuilder method = type.DefineMethod("Method1", MethodAttributes.Public | MethodAttributes.Static, typeof(Type), Type.EmptyTypes); ILGenerator ilGenerator = method.GetILGenerator(); @@ -201,7 +201,7 @@ public void ILMaxStack_Test() { using (TempFile file = TempFile.Create()) { - AssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder type); + PersistedAssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder type); MethodBuilder method1 = type.DefineMethod("Method1", MethodAttributes.Public, typeof(long), [typeof(int), typeof(long), typeof(short), typeof(byte)]); ILGenerator il1 = method1.GetILGenerator(); @@ -275,7 +275,7 @@ public void Label_ConditionalBranching() { using (TempFile file = TempFile.Create()) { - AssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder type); + PersistedAssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder type); MethodBuilder methodBuilder = type.DefineMethod("Method1", MethodAttributes.Public, typeof(int), [typeof(int), typeof(int)]); ILGenerator il = methodBuilder.GetILGenerator(); Label failed = il.DefineLabel(); @@ -329,7 +329,7 @@ public void Label_SwitchCase() { using (TempFile file = TempFile.Create()) { - AssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder type); + PersistedAssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder type); MethodBuilder methodBuilder = type.DefineMethod("Method1", MethodAttributes.Public, typeof(string), [typeof(int)]); ILGenerator il = methodBuilder.GetILGenerator(); Label defaultCase = il.DefineLabel(); @@ -397,7 +397,7 @@ public void LocalBuilderMultipleLocalsUsage() { using (TempFile file = TempFile.Create()) { - AssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder type); + PersistedAssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder type); MethodBuilder methodBuilder = type.DefineMethod("Method1", MethodAttributes.Public | MethodAttributes.Static, typeof(int), [typeof(int), typeof(string)]); ILGenerator il = methodBuilder.GetILGenerator(); LocalBuilder intLocal = il.DeclareLocal(typeof(int)); @@ -481,7 +481,7 @@ public void LocalBuilderMultipleTypesWithMultipleMethodsWithLocals() { using (TempFile file = TempFile.Create()) { - AssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder type); + PersistedAssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder type); MethodBuilder methodBuilder = type.DefineMethod("Method1", MethodAttributes.Public | MethodAttributes.Static, typeof(string), [typeof(int), typeof(string)]); ILGenerator il = methodBuilder.GetILGenerator(); LocalBuilder intLocal = il.DeclareLocal(typeof(int)); @@ -569,7 +569,7 @@ public void LocalBuilderMultipleTypesWithMultipleMethodsWithLocals() [Fact] public void LocalBuilderExceptions() { - AssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder type); + PersistedAssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder type); ILGenerator il = type.DefineMethod("Method1", MethodAttributes.Public).GetILGenerator(); ILGenerator anotherIL = type.DefineMethod("AnotherMethod", MethodAttributes.Public).GetILGenerator(); LocalBuilder stringLocal = il.DeclareLocal(typeof(string)); @@ -585,7 +585,7 @@ public void ReferenceFieldInIL() { using (TempFile file = TempFile.Create()) { - AssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder tb); + PersistedAssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder tb); MethodBuilder methodBuilder = tb.DefineMethod("Method1", MethodAttributes.Public, typeof(int), [typeof(int)]); FieldBuilder fbNumber = tb.DefineField("_number", typeof(int), FieldAttributes.Private); Assert.Equal(0, fbNumber.MetadataToken); @@ -621,7 +621,7 @@ public void ReferenceFieldAndMethodsInIL() { using (TempFile file = TempFile.Create()) { - AssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder tb); + PersistedAssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder tb); MethodBuilder methodMain = tb.DefineMethod("Main", MethodAttributes.Public, typeof(void), [typeof(int)]); FieldBuilder field = tb.DefineField("_field", typeof(int), FieldAttributes.Private); MethodInfo writeLineString = typeof(Console).GetMethod("WriteLine", [typeof(string)]); @@ -699,7 +699,7 @@ public void ReferenceConstructedGenericMethod() { using (TempFile file = TempFile.Create()) { - AssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder type); + PersistedAssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder type); ConstructorBuilder ctor = type.DefineDefaultConstructor(MethodAttributes.Public); MethodBuilder genericMethod = type.DefineMethod("GM", MethodAttributes.Public | MethodAttributes.Static); GenericTypeParameterBuilder[] methodParams = genericMethod.DefineGenericParameters("U"); @@ -738,7 +738,7 @@ public void ReferenceConstructedGenericMethodFieldOfConstructedType() { using (TempFile file = TempFile.Create()) { - AssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder type); + PersistedAssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder type); GenericTypeParameterBuilder[] typeParams = type.DefineGenericParameters(["T"]); ConstructorBuilder ctor = type.DefineDefaultConstructor(MethodAttributes.PrivateScope | MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.SpecialName | MethodAttributes.RTSpecialName); @@ -833,7 +833,7 @@ public void EmitWriteLineMacroTest() { using (TempFile file = TempFile.Create()) { - AssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder type1); + PersistedAssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder type1); MethodBuilder method = type1.DefineMethod("meth", MethodAttributes.Public, typeof(int), Type.EmptyTypes); FieldBuilder field = type1.DefineField("field", typeof(int), FieldAttributes.Public | FieldAttributes.Static); ILGenerator ilGenerator = method.GetILGenerator(); @@ -881,7 +881,7 @@ public void ReferenceStaticFieldAndMethodsInIL() { using (TempFile file = TempFile.Create()) { - AssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder tb); + PersistedAssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder tb); MethodBuilder methodMain = tb.DefineMethod("Main", MethodAttributes.Public, typeof(int), [typeof(int)]); TypeBuilder anotherType = ab.GetDynamicModule("MyModule").DefineType("AnotherType", TypeAttributes.Public); FieldBuilder field = anotherType.DefineField("StaticField", typeof(int), FieldAttributes.Public | FieldAttributes.Static); @@ -934,7 +934,7 @@ public void ReferenceConstructorInIL() { using (TempFile file = TempFile.Create()) { - AssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder tb); + PersistedAssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder tb); MethodBuilder methodBuilder = tb.DefineMethod("Method1", MethodAttributes.Public, typeof(Version), [typeof(int), typeof(int)]); ConstructorInfo ctor = typeof(Version).GetConstructor([typeof(int), typeof(int)]); @@ -964,7 +964,7 @@ public void ReferenceAType() { using (TempFile file = TempFile.Create()) { - AssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder tb); + PersistedAssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder tb); MethodBuilder method = tb.DefineMethod("meth1", MethodAttributes.Public | MethodAttributes.Static, typeof(bool), Type.EmptyTypes); ILGenerator ilGenerator = method.GetILGenerator(); LocalBuilder lb0 = ilGenerator.DeclareLocal(typeof(ValueTuple)); @@ -993,7 +993,7 @@ public void ReferenceAType() [Fact] public void MemberReferenceExceptions() { - AssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder type); + PersistedAssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder type); MethodBuilder method = type.DefineMethod("Method1", MethodAttributes.Public); ILGenerator il = method.GetILGenerator(); MethodInfo nullMethod = null; @@ -1026,7 +1026,7 @@ public void SimpleTryCatchBlock() { using (TempFile file = TempFile.Create()) { - AssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder tb); + PersistedAssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder tb); MethodBuilder method = tb.DefineMethod("Method", MethodAttributes.Public | MethodAttributes.Static, typeof(float), [typeof(int), typeof(int)]); Type dBZException = typeof(DivideByZeroException); ILGenerator ilGenerator = method.GetILGenerator(); @@ -1080,7 +1080,7 @@ public void TryMultipleCatchBlocks() { using (TempFile file = TempFile.Create()) { - AssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder tb); + PersistedAssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder tb); MethodBuilder method = tb.DefineMethod("Method", MethodAttributes.Public | MethodAttributes.Static, typeof(float), [typeof(int), typeof(int)]); Type dBZException = typeof(DivideByZeroException); Type exception = typeof(Exception); @@ -1156,7 +1156,7 @@ public void TryFilterCatchBlock() { using (TempFile file = TempFile.Create()) { - AssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder tb); + PersistedAssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder tb); MethodBuilder method = tb.DefineMethod("Method", MethodAttributes.Public | MethodAttributes.Static, typeof(float), [typeof(int), typeof(int)]); Type dBZException = typeof(DivideByZeroException); Type exception = typeof(Exception); @@ -1219,7 +1219,7 @@ public void TryCatchFilterCatchBlock() { using (TempFile file = TempFile.Create()) { - AssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder tb); + PersistedAssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder tb); MethodBuilder method = tb.DefineMethod("Method", MethodAttributes.Public | MethodAttributes.Static, typeof(float), [typeof(int), typeof(int)]); Type dBZException = typeof(DivideByZeroException); Type overflowException = typeof(OverflowException); @@ -1294,7 +1294,7 @@ public void TryFinallyBlock() { using (TempFile file = TempFile.Create()) { - AssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder tb); + PersistedAssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder tb); MethodBuilder method = tb.DefineMethod("Method", MethodAttributes.Public | MethodAttributes.Static, typeof(float), [typeof(int), typeof(int)]); ILGenerator ilGenerator = method.GetILGenerator(); LocalBuilder local = ilGenerator.DeclareLocal(typeof(float)); @@ -1341,7 +1341,7 @@ public void TryCatchFinallyBlock() { using (TempFile file = TempFile.Create()) { - AssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder tb); + PersistedAssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder tb); MethodBuilder method = tb.DefineMethod("Method", MethodAttributes.Public | MethodAttributes.Static, typeof(void), [typeof(int), typeof(int)]); Type exception = typeof(Exception); ILGenerator ilGenerator = method.GetILGenerator(); @@ -1379,7 +1379,7 @@ public void TryFilterCatchFinallyBlock() { using (TempFile file = TempFile.Create()) { - AssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder tb); + PersistedAssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder tb); MethodBuilder method = tb.DefineMethod("Method", MethodAttributes.Public | MethodAttributes.Static, typeof(int), [typeof(int), typeof(int)]); Type overflowEType = typeof(OverflowException); ConstructorInfo myConstructorInfo = overflowEType.GetConstructor([typeof(string)]); @@ -1452,7 +1452,7 @@ public void TryFaultBlock() { using (TempFile file = TempFile.Create()) { - AssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder tb); + PersistedAssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder tb); MethodBuilder method = tb.DefineMethod("Method", MethodAttributes.Public | MethodAttributes.Static, typeof(float), [typeof(int), typeof(int)]); ILGenerator ilGenerator = method.GetILGenerator(); Label exBlock = ilGenerator.BeginExceptionBlock(); @@ -1502,7 +1502,7 @@ public void NestedTryCatchBlocks() { using (TempFile file = TempFile.Create()) { - AssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder tb); + PersistedAssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder tb); MethodBuilder method = tb.DefineMethod("Method", MethodAttributes.Public | MethodAttributes.Static, typeof(void), [typeof(int), typeof(int)]); Type exception = typeof(Exception); ILGenerator ilGenerator = method.GetILGenerator(); @@ -1572,7 +1572,7 @@ public void DeeperNestedTryCatchFilterFinallyBlocks() { using (TempFile file = TempFile.Create()) { - AssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder tb); + PersistedAssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder tb); MethodBuilder method = tb.DefineMethod("Method", MethodAttributes.Public | MethodAttributes.Static, typeof(int), [typeof(int), typeof(int)]); Type exception = typeof(Exception); ILGenerator ilGenerator = method.GetILGenerator(); @@ -1678,7 +1678,7 @@ public void EmitCalliBlittable() int a = 1, b = 1, result = 2; using (TempFile file = TempFile.Create()) { - AssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilder(new AssemblyName("EmitCalliBlittable")); + PersistedAssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilder(new AssemblyName("EmitCalliBlittable")); TypeBuilder tb = ab.DefineDynamicModule("MyModule").DefineType("MyType", TypeAttributes.Public | TypeAttributes.Class); Type returnType = typeof(int); MethodBuilder methodBuilder = tb.DefineMethod("F", MethodAttributes.Public | MethodAttributes.Static, returnType, [typeof(IntPtr), typeof(int), typeof(int)]); @@ -1712,7 +1712,7 @@ public void EmitCalliManagedBlittable() int a = 1, b = 1, result = 2; using (TempFile file = TempFile.Create()) { - AssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilder(new AssemblyName("EmitCalliManagedBlittable")); + PersistedAssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilder(new AssemblyName("EmitCalliManagedBlittable")); TypeBuilder tb = ab.DefineDynamicModule("MyModule").DefineType("MyType", TypeAttributes.Public | TypeAttributes.Class); Type returnType = typeof(int); MethodBuilder methodBuilder = tb.DefineMethod("F", MethodAttributes.Public | MethodAttributes.Static, returnType, [typeof(IntPtr), typeof(int), typeof(int)]); @@ -1750,7 +1750,7 @@ public void EmitCalliNonBlittable() string input = "Test string!", result = "!gnirts tseT"; using (TempFile file = TempFile.Create()) { - AssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilder(new AssemblyName("EmitCalliNonBlittable")); + PersistedAssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilder(new AssemblyName("EmitCalliNonBlittable")); TypeBuilder tb = ab.DefineDynamicModule("MyModule").DefineType("MyType", TypeAttributes.Public | TypeAttributes.Class); Type returnType = typeof(string); MethodBuilder methodBuilder = tb.DefineMethod("F", MethodAttributes.Public | MethodAttributes.Static, returnType, [typeof(IntPtr), typeof(string)]); @@ -1783,7 +1783,7 @@ public void EmitCall_VarArgsMethodInIL() { using (TempFile file = TempFile.Create()) { - AssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder tb); + PersistedAssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder tb); MethodBuilder mb1 = tb.DefineMethod("VarArgMethod", MethodAttributes.Public | MethodAttributes.Static, CallingConventions.VarArgs, null, [typeof(string)]); ILGenerator il1 = mb1.GetILGenerator(); LocalBuilder locAi = il1.DeclareLocal(typeof(ArgIterator)); @@ -1872,7 +1872,7 @@ public void Emit_CallBySignature() { using (TempFile file = TempFile.Create()) { - AssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder tb); + PersistedAssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder tb); MethodBuilder mb1 = tb.DefineMethod("VarArgMethod", MethodAttributes.Public | MethodAttributes.Static, CallingConventions.VarArgs, null, [typeof(string)]); ILGenerator il1 = mb1.GetILGenerator(); FieldInfo maxStack = GetMaxStackDepthAndCurrentStackDepthField(out FieldInfo currentStack); @@ -2191,7 +2191,7 @@ public void SimpleForLoopTest() { using (TempFile file = TempFile.Create()) { - AssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder tb); + PersistedAssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder tb); MethodBuilder mb2 = tb.DefineMethod("SumMethod", MethodAttributes.Public | MethodAttributes.Static, typeof(int), [typeof(int)]); ILGenerator il = mb2.GetILGenerator(); LocalBuilder sum = il.DeclareLocal(typeof(int)); @@ -2238,7 +2238,7 @@ public void RecursiveSumTest() { using (TempFile file = TempFile.Create()) { - AssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilder(new AssemblyName("RecursiveSumTest")); + PersistedAssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilder(new AssemblyName("RecursiveSumTest")); TypeBuilder tb = ab.DefineDynamicModule("MyModule").DefineType("MyType", TypeAttributes.Public | TypeAttributes.Class); MethodBuilder mb2 = tb.DefineMethod("RecursiveMethod", MethodAttributes.Public | MethodAttributes.Static, typeof(int), [typeof(int)]); ILGenerator il = mb2.GetILGenerator(); @@ -2277,7 +2277,7 @@ public void CallOpenGenericMembersFromConstructedGenericType() { using (TempFile file = TempFile.Create()) { - AssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder type); + PersistedAssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder type); MethodBuilder method = type.DefineMethod("M1", MethodAttributes.Public, typeof(string), null); ILGenerator ilGenerator = method.GetILGenerator(); @@ -2312,7 +2312,7 @@ public void ReferenceMethodsOfDictionaryFieldInGenericTypeWorks() { using (TempFile file = TempFile.Create()) { - AssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder type); + PersistedAssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder type); TypeBuilder tb = ab.GetDynamicModule("MyModule").DefineType("EnumNameCache", TypeAttributes.NotPublic); GenericTypeParameterBuilder[] param = tb.DefineGenericParameters(["TEnum"]); Type fieldType = typeof(Dictionary<,>).MakeGenericType(param[0], typeof(string)); @@ -2368,7 +2368,7 @@ public void ANestedTypeUsedAsGenericArgumentWorks() { using (TempFile file = TempFile.Create()) { - AssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder type); + PersistedAssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder type); TypeBuilder nested = type.DefineNestedType("Nested", TypeAttributes.NestedPrivate); Type nestedFType = typeof(Dictionary<,>).MakeGenericType(typeof(Type), nested); FieldBuilder nestedField = nested.DefineField("Helpers", nestedFType, FieldAttributes.Static | FieldAttributes.Private); @@ -2425,7 +2425,7 @@ public void ReferenceNestedGenericCollectionsWithTypeBuilderParameterInIL() { using (TempFile file = TempFile.Create()) { - AssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder type); + PersistedAssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder type); TypeBuilder nestedType = type.DefineNestedType("NestedType", TypeAttributes.NestedPublic); Type returnType = typeof(List<>).MakeGenericType(typeof(Dictionary<,>).MakeGenericType(nestedType, typeof(bool))); @@ -2458,7 +2458,7 @@ public void ReferenceNestedGenericTypeWithConstructedTypeBuilderParameterInIL() { using (TempFile file = TempFile.Create()) { - AssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder type); + PersistedAssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder type); string[] genParams = new string[] { "T" }; GenericTypeParameterBuilder[] param = type.DefineGenericParameters(genParams); TypeBuilder nestedItem = type.DefineNestedType("ItemInfo", TypeAttributes.NestedPublic); @@ -2509,7 +2509,7 @@ public void ConstructorOfGenericTypeReferencedCorrectly() { using (TempFile file = TempFile.Create()) { - AssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder type); + PersistedAssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder type); FieldBuilder field = type.DefineField("Field", typeof(int?), FieldAttributes.Public); ConstructorBuilder ctor = type.DefineConstructor(MethodAttributes.Public, CallingConventions.HasThis, Type.EmptyTypes); ILGenerator ctorIL = ctor.GetILGenerator(); diff --git a/src/libraries/System.Reflection.Emit/tests/PersistableAssemblyBuilder/AssemblySaveModuleBuilderTests.cs b/src/libraries/System.Reflection.Emit/tests/PersistedAssemblyBuilder/AssemblySaveModuleBuilderTests.cs similarity index 94% rename from src/libraries/System.Reflection.Emit/tests/PersistableAssemblyBuilder/AssemblySaveModuleBuilderTests.cs rename to src/libraries/System.Reflection.Emit/tests/PersistedAssemblyBuilder/AssemblySaveModuleBuilderTests.cs index 7a009e638f589..959ea0022d5ce 100644 --- a/src/libraries/System.Reflection.Emit/tests/PersistableAssemblyBuilder/AssemblySaveModuleBuilderTests.cs +++ b/src/libraries/System.Reflection.Emit/tests/PersistedAssemblyBuilder/AssemblySaveModuleBuilderTests.cs @@ -15,7 +15,7 @@ public void DefineGlobalMethodAndCreateGlobalFunctionsTest() { using (TempFile file = TempFile.Create()) { - AssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilder(new AssemblyName("MyAssembly")); + PersistedAssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilder(new AssemblyName("MyAssembly")); ModuleBuilder module = ab.DefineDynamicModule("MyModule"); MethodBuilder method = module.DefineGlobalMethod("TestMethod", MethodAttributes.Static | MethodAttributes.Public, null, null); ILGenerator ilGenerator = method.GetILGenerator(); @@ -63,7 +63,7 @@ public void DefineGlobalMethodAndCreateGlobalFunctionsTest() [Fact] public void DefineGlobalMethodAndCreateGlobalFunctions_Validations() { - AssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilder(new AssemblyName("MyAssembly")); + PersistedAssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilder(new AssemblyName("MyAssembly")); ModuleBuilder module = ab.DefineDynamicModule("MyModule"); Assert.Throws(() => module.DefineGlobalMethod("TestMethod", MethodAttributes.Public, null, null)); // must be static MethodBuilder method = module.DefineGlobalMethod("TestMethod", MethodAttributes.Static | MethodAttributes.Public, null, null); @@ -85,7 +85,7 @@ public static void DefinePInvokeMethodTest() { using (TempFile file = TempFile.Create()) { - AssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilder(new AssemblyName("MyAssembly")); + PersistedAssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilder(new AssemblyName("MyAssembly")); DpmParams p = new DpmParams() { MethodName = "A2", LibName = "Foo2.dll", EntrypointName = "Wha2", ReturnType = typeof(int), ParameterTypes = [typeof(int)], NativeCallConv = CallingConvention.Cdecl }; @@ -117,7 +117,7 @@ public static void DefinePInvokeMethodTest() [InlineData(FieldAttributes.Assembly | FieldAttributes.SpecialName)] public void DefineUninitializedDataTest(FieldAttributes attributes) { - AssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilder(new AssemblyName("MyAssembly")); + PersistedAssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilder(new AssemblyName("MyAssembly")); ModuleBuilder module = ab.DefineDynamicModule("MyModule"); foreach (int size in new int[] { 1, 2, 0x003f0000 - 1 }) { @@ -133,7 +133,7 @@ public void DefineUninitializedDataTest(FieldAttributes attributes) [Fact] public void DefineUninitializedData_Validations() { - AssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilder(new AssemblyName("MyAssembly")); + PersistedAssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilder(new AssemblyName("MyAssembly")); ModuleBuilder module = ab.DefineDynamicModule("MyModule"); AssertExtensions.Throws("name", () => module.DefineUninitializedData(null, 1, FieldAttributes.Family)); @@ -154,7 +154,7 @@ public void DefineUninitializedData_Validations() [InlineData(FieldAttributes.Private)] public void DefineInitializedDataTest(FieldAttributes attributes) { - AssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilder(new AssemblyName("MyAssembly")); + PersistedAssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilder(new AssemblyName("MyAssembly")); ModuleBuilder module = ab.DefineDynamicModule("MyModule"); FieldBuilder field = module.DefineInitializedData("MyField", [01, 00, 01], attributes); @@ -167,7 +167,7 @@ public void DefineInitializedDataTest(FieldAttributes attributes) [Fact] public void DefineInitializedData_Validations() { - AssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilder(new AssemblyName("MyAssembly")); + PersistedAssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilder(new AssemblyName("MyAssembly")); ModuleBuilder module = ab.DefineDynamicModule("MyModule"); AssertExtensions.Throws("name", () => module.DefineInitializedData(null, [1, 0, 1], FieldAttributes.Public)); @@ -190,7 +190,7 @@ public void DefineInitializedData_EnsureAlignmentIsMinimumNeededForUseOfCreateSp { using (TempFile file = TempFile.Create()) { - AssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilder(new AssemblyName("MyAssembly")); + PersistedAssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilder(new AssemblyName("MyAssembly")); ModuleBuilder module = ab.DefineDynamicModule("MyModule"); TypeBuilder tb = module.DefineType("MyType", TypeAttributes.Public); // Create static field data in a variety of orders that requires the runtime to actively apply alignment @@ -265,7 +265,7 @@ void CheckMethod(string name, int minAlignmentRequired, byte[] dataToVerify) [ActiveIssue("https://github.com/dotnet/runtime/issues/96389", TestRuntimes.Mono)] public void GetABCMetadataToken_Validations() { - AssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilder(new AssemblyName("MyAssembly")); + PersistedAssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilder(new AssemblyName("MyAssembly")); ModuleBuilder module = ab.DefineDynamicModule("MyModule"); TypeBuilder type = module.DefineType("MyType", TypeAttributes.Public); MethodBuilder method = type.DefineMethod("TestMethod", MethodAttributes.Static | MethodAttributes.Public); @@ -294,7 +294,7 @@ public static void GetArrayMethodTest() { using (TempFile file = TempFile.Create()) { - AssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilder(new AssemblyName("MyAssembly")); + PersistedAssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilder(new AssemblyName("MyAssembly")); ModuleBuilder mb = ab.DefineDynamicModule("MyModule"); TypeBuilder tb = mb.DefineType("TestClass", TypeAttributes.Public); diff --git a/src/libraries/System.Reflection.Emit/tests/PersistableAssemblyBuilder/AssemblySavePropertyBuilderTests.cs b/src/libraries/System.Reflection.Emit/tests/PersistedAssemblyBuilder/AssemblySavePropertyBuilderTests.cs similarity index 98% rename from src/libraries/System.Reflection.Emit/tests/PersistableAssemblyBuilder/AssemblySavePropertyBuilderTests.cs rename to src/libraries/System.Reflection.Emit/tests/PersistedAssemblyBuilder/AssemblySavePropertyBuilderTests.cs index c18eb017c1428..06caf8dbb6a7d 100644 --- a/src/libraries/System.Reflection.Emit/tests/PersistableAssemblyBuilder/AssemblySavePropertyBuilderTests.cs +++ b/src/libraries/System.Reflection.Emit/tests/PersistedAssemblyBuilder/AssemblySavePropertyBuilderTests.cs @@ -18,7 +18,7 @@ public void SetPropertyAccessorsAndOtherValues() { using (TempFile file = TempFile.Create()) { - AssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder type); + PersistedAssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder type); FieldBuilder field = type.DefineField("TestField", typeof(int), FieldAttributes.Private); PropertyBuilder property = type.DefineProperty("TestProperty", PropertyAttributes.SpecialName | PropertyAttributes.HasDefault, typeof(int), null); MethodBuilder getMethod = type.DefineMethod("GetMethod", MethodAttributes.Public | MethodAttributes.HideBySig, typeof(int), null); @@ -84,7 +84,7 @@ public void SetVariousCustomAttributes_ForProperty() PropertyInfo prop = typeof(CustomAttributeBuilder).GetProperty("Data", BindingFlags.NonPublic | BindingFlags.Instance); byte[] binaryData = (byte[])prop.GetValue(customAttrBuilder, null); - AssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder type); + PersistedAssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder type); PropertyBuilder property = type.DefineProperty("TestProperty", PropertyAttributes.HasDefault, typeof(int), null); property.SetCustomAttribute(con, binaryData); property.SetCustomAttribute(new CustomAttributeBuilder(typeof(SpecialNameAttribute).GetConstructor(Type.EmptyTypes), [])); diff --git a/src/libraries/System.Reflection.Emit/tests/PersistableAssemblyBuilder/AssemblySaveTools.cs b/src/libraries/System.Reflection.Emit/tests/PersistedAssemblyBuilder/AssemblySaveTools.cs similarity index 93% rename from src/libraries/System.Reflection.Emit/tests/PersistableAssemblyBuilder/AssemblySaveTools.cs rename to src/libraries/System.Reflection.Emit/tests/PersistedAssemblyBuilder/AssemblySaveTools.cs index dc083e521815b..6c2d65fb7ad40 100644 --- a/src/libraries/System.Reflection.Emit/tests/PersistableAssemblyBuilder/AssemblySaveTools.cs +++ b/src/libraries/System.Reflection.Emit/tests/PersistedAssemblyBuilder/AssemblySaveTools.cs @@ -31,7 +31,7 @@ internal static class AssemblySaveTools internal static void WriteAssemblyToDisk(AssemblyName assemblyName, Type[] types, string fileLocation) { - AssemblyBuilder assemblyBuilder = PopulateAssemblyBuilder(assemblyName); + PersistedAssemblyBuilder assemblyBuilder = PopulateAssemblyBuilder(assemblyName); ModuleBuilder mb = assemblyBuilder.DefineDynamicModule(assemblyName.Name); PopulateMembersForModule(mb, types); @@ -67,7 +67,7 @@ private static void PopulateMembersForModule(ModuleBuilder mb, Type[] types) internal static void WriteAssemblyToStream(AssemblyName assemblyName, Type[] types, Stream stream) { - AssemblyBuilder assemblyBuilder = PopulateAssemblyBuilder(assemblyName); + PersistedAssemblyBuilder assemblyBuilder = PopulateAssemblyBuilder(assemblyName); ModuleBuilder mb = assemblyBuilder.DefineDynamicModule(assemblyName.Name); PopulateMembersForModule(mb, types); @@ -75,15 +75,15 @@ internal static void WriteAssemblyToStream(AssemblyName assemblyName, Type[] typ assemblyBuilder.Save(stream); } - internal static AssemblyBuilder PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder typeBuilder) + internal static PersistedAssemblyBuilder PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder typeBuilder) { - AssemblyBuilder ab = PopulateAssemblyBuilder(s_assemblyName, null); + PersistedAssemblyBuilder ab = PopulateAssemblyBuilder(s_assemblyName, null); typeBuilder = ab.DefineDynamicModule("MyModule").DefineType("MyType", TypeAttributes.Public | TypeAttributes.Class); return ab; } - internal static AssemblyBuilder PopulateAssemblyBuilder(AssemblyName assemblyName, List? assemblyAttributes = null) => - AssemblyBuilder.DefinePersistedAssembly(assemblyName, CoreMetadataAssemblyResolver.s_coreAssembly, assemblyAttributes); + internal static PersistedAssemblyBuilder PopulateAssemblyBuilder(AssemblyName assemblyName, List? assemblyAttributes = null) => + new PersistedAssemblyBuilder(assemblyName, CoreMetadataAssemblyResolver.s_coreAssembly, assemblyAttributes); internal static void AssertAssemblyNameAndModule(AssemblyName sourceAName, AssemblyName aNameFromDisk, Module moduleFromDisk) { diff --git a/src/libraries/System.Reflection.Emit/tests/PersistableAssemblyBuilder/AssemblySaveTypeBuilderAPIsTests.cs b/src/libraries/System.Reflection.Emit/tests/PersistedAssemblyBuilder/AssemblySaveTypeBuilderAPIsTests.cs similarity index 96% rename from src/libraries/System.Reflection.Emit/tests/PersistableAssemblyBuilder/AssemblySaveTypeBuilderAPIsTests.cs rename to src/libraries/System.Reflection.Emit/tests/PersistedAssemblyBuilder/AssemblySaveTypeBuilderAPIsTests.cs index ae84e78cfc24f..e533a23446621 100644 --- a/src/libraries/System.Reflection.Emit/tests/PersistableAssemblyBuilder/AssemblySaveTypeBuilderAPIsTests.cs +++ b/src/libraries/System.Reflection.Emit/tests/PersistedAssemblyBuilder/AssemblySaveTypeBuilderAPIsTests.cs @@ -17,7 +17,7 @@ public void DefineMethodOverride_InterfaceMethod() { using (TempFile file = TempFile.Create()) { - AssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder type); + PersistedAssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder type); MethodBuilder method = type.DefineMethod("MImpl", MethodAttributes.Public | MethodAttributes.Virtual, typeof(int), null); ILGenerator ilGenerator = method.GetILGenerator(); ilGenerator.Emit(OpCodes.Ldc_I4, 2); @@ -49,7 +49,7 @@ public void DefineMethodOverride_BaseTypeImplementation() { using (TempFile file = TempFile.Create()) { - AssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder type); + PersistedAssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder type); type.SetParent(typeof(DefineMethodOverrideClass)); MethodBuilder method = type.DefineMethod("M2", MethodAttributes.Public | MethodAttributes.Virtual, typeof(int), null); ILGenerator ilGenerator = method.GetILGenerator(); @@ -73,7 +73,7 @@ public void DefineMethodOverride_GenericInterface_Succeeds() { using (TempFile file = TempFile.Create()) { - AssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder type); + PersistedAssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder type); type.AddInterfaceImplementation(typeof(GenericInterface)); MethodBuilder method = type.DefineMethod("Method", MethodAttributes.Public | MethodAttributes.Virtual, typeof(string), Type.EmptyTypes); ILGenerator ilGenerator = method.GetILGenerator(); @@ -219,7 +219,7 @@ public interface InterfaceWithMethod [Fact] public void DefineMethodOverride_StaticVirtualInterfaceMethodWorks() { - AssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder type); + PersistedAssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder type); ModuleBuilder module = ab.GetDynamicModule("MyModule"); TypeBuilder interfaceType = module.DefineType("InterfaceType", TypeAttributes.Public | TypeAttributes.Interface | TypeAttributes.Abstract, parent: null); @@ -249,7 +249,7 @@ public abstract class Impl : InterfaceWithMethod [Fact] public void DefineMethodOverride_InterfaceImplementationWithByRefArrayTypes() { - AssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder type); + PersistedAssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder type); ModuleBuilder module = ab.GetDynamicModule("MyModule"); TypeBuilder interfaceType = module.DefineType("InterfaceType", TypeAttributes.Public | TypeAttributes.Interface | TypeAttributes.Abstract); @@ -279,7 +279,7 @@ public void DefineMethodOverride_InterfaceImplementationWithByRefArrayTypes() [Fact] public void TypeBuilderImplementsGenericInterfaceWithTypeBuilderGenericConstraint() { - AssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder type); + PersistedAssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder type); ModuleBuilder module = ab.GetDynamicModule("MyModule"); TypeBuilder ifaceType = module.DefineType("InterfaceType", TypeAttributes.Public | TypeAttributes.Interface | TypeAttributes.Abstract); TypeBuilder implType = module.DefineType("ImplType", TypeAttributes.Public); @@ -302,7 +302,7 @@ public void TypeBuilderImplementsGenericInterfaceWithTypeBuilderGenericConstrain [Fact] public void TypeBuilderImplementsGenericInterfaceWithTypeBuilderArgument() { - AssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder type); + PersistedAssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder type); ModuleBuilder module = ab.GetDynamicModule("MyModule"); Type constructedGenericInterface = typeof(IComparable<>).MakeGenericType(type); @@ -320,7 +320,7 @@ public void TypeBuilderImplementsGenericInterfaceWithTypeBuilderArgument() [Fact] public void TypeBuilderImplementsGenericInterface() { - AssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder type); + PersistedAssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder type); ModuleBuilder module = ab.GetDynamicModule("MyModule"); TypeBuilder implType = module.DefineType("ImplType", TypeAttributes.Public); @@ -341,7 +341,7 @@ public void TypeBuilderImplementsGenericInterface() [Fact] public void TypeBuilderImplementsConstructedGenericInterface() { - AssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder type); + PersistedAssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder type); ModuleBuilder module = ab.GetDynamicModule("MyModule"); TypeBuilder implType = module.DefineType("ImplType", TypeAttributes.Public, parent: typeof(object), [typeof(IComparable)]); @@ -358,7 +358,7 @@ public void TypeBuilderImplementsConstructedGenericInterface() [Fact] public void GetInterfaceMap_WithImplicitOverride_DefineMethodOverride() { - AssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder type); + PersistedAssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder type); ModuleBuilder module = ab.GetDynamicModule("MyModule"); TypeBuilder interfaceType = module.DefineType("InterfaceType", TypeAttributes.Public | TypeAttributes.Interface | TypeAttributes.Abstract, parent: null); @@ -448,7 +448,7 @@ public interface IStaticAbstract [Fact] public void CreateType_ValidateMethods() { - AssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder concreteTypeWithAbstractMethod); + PersistedAssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder concreteTypeWithAbstractMethod); concreteTypeWithAbstractMethod.DefineMethod("AbstractMethod", MethodAttributes.Public | MethodAttributes.Abstract); Assert.Throws(() => concreteTypeWithAbstractMethod.CreateType()); // Type must be declared abstract if any of its methods are abstract. @@ -514,7 +514,7 @@ public void ReturnTypeAndParameterRequiredOptionalCustomModifiers() Type[] cmodsReq2 = [typeof(uint)]; Type[] cmodsOpt1 = [typeof(int)]; Type[] cmodsOpt2 = [typeof(long), typeof(byte), typeof(bool)]; - AssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder type); + PersistedAssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder type); MethodBuilder methodAll = type.DefineMethod("AllModifiers", MethodAttributes.Public | MethodAttributes.Static, CallingConventions.Standard, typeof(string), [typeof(int), typeof(short)], [typeof(Version)], [typeof(int), typeof(long)], [cmodsReq1, cmodsReq2], [cmodsOpt1, cmodsOpt2]); ILGenerator ilGenerator = methodAll.GetILGenerator(); @@ -555,7 +555,7 @@ public static void DefinePInvokeMethodExecution_Windows() using (TempFile file = TempFile.Create()) { - AssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilder(new AssemblyName("DefinePInvokeMethodExecution_Windows")); + PersistedAssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilder(new AssemblyName("DefinePInvokeMethodExecution_Windows")); TypeBuilder tb = ab.DefineDynamicModule("MyModule").DefineType("MyType", TypeAttributes.Public | TypeAttributes.Class); MethodBuilder mb = tb.DefinePInvokeMethod( "GetEnvironmentVariableW", @@ -624,7 +624,7 @@ public static void TestDefinePInvokeMethod(DpmParams p) { using (TempFile file = TempFile.Create()) { - AssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder tb); + PersistedAssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder tb); MethodBuilder mb = tb.DefinePInvokeMethod(p.MethodName, p.LibName, p.EntrypointName, p.Attributes, p.ManagedCallConv, p.ReturnType, p.ReturnTypeReqMods, p.ReturnTypeOptMods, p.ParameterTypes, p.ParameterTypeReqMods, p.ParameterTypeOptMods, p.NativeCallConv, p.Charset); mb.SetImplementationFlags(mb.GetMethodImplementationFlags() | MethodImplAttributes.PreserveSig); @@ -756,7 +756,7 @@ public void DefineTypeInitializer() { using (TempFile file = TempFile.Create()) { - AssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder tb); + PersistedAssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder tb); FieldBuilder greetingField = tb.DefineField("Greeting", typeof(string), FieldAttributes.Private | FieldAttributes.Static); ConstructorBuilder constructor = tb.DefineTypeInitializer(); ILGenerator constructorIlGenerator = constructor.GetILGenerator(); @@ -780,7 +780,7 @@ public static void DefineUninitializedDataTest() { using (TempFile file = TempFile.Create()) { - AssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder tb); + PersistedAssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder tb); FieldBuilder myFieldBuilder = tb.DefineUninitializedData("MyGreeting", 4, FieldAttributes.Public); var loadAddressMethod = tb.DefineMethod("LoadAddress", MethodAttributes.Public | MethodAttributes.Static, typeof(IntPtr), null); var methodIL = loadAddressMethod.GetILGenerator(); @@ -870,7 +870,7 @@ public void AbstractBaseMethodImplementationReturnsDifferentType() { using (TempFile file = TempFile.Create()) { - AssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder type); + PersistedAssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder type); TypeBuilder baseType = ab.GetDynamicModule("MyModule").DefineType("Base", TypeAttributes.Public | TypeAttributes.Abstract); MethodBuilder getBase = baseType.DefineMethod("Get", MethodAttributes.Public | MethodAttributes.Abstract | MethodAttributes.Virtual, baseType, null); type.SetParent(baseType); diff --git a/src/libraries/System.Reflection.Emit/tests/PersistableAssemblyBuilder/AssemblySaveTypeBuilderTests.cs b/src/libraries/System.Reflection.Emit/tests/PersistedAssemblyBuilder/AssemblySaveTypeBuilderTests.cs similarity index 97% rename from src/libraries/System.Reflection.Emit/tests/PersistableAssemblyBuilder/AssemblySaveTypeBuilderTests.cs rename to src/libraries/System.Reflection.Emit/tests/PersistedAssemblyBuilder/AssemblySaveTypeBuilderTests.cs index eb7976a007866..1713d91ba4578 100644 --- a/src/libraries/System.Reflection.Emit/tests/PersistableAssemblyBuilder/AssemblySaveTypeBuilderTests.cs +++ b/src/libraries/System.Reflection.Emit/tests/PersistedAssemblyBuilder/AssemblySaveTypeBuilderTests.cs @@ -100,7 +100,7 @@ public void CreateMembersThatUsesTypeLoadedFromCoreAssemblyTest() { using (TempFile file = TempFile.Create()) { - TypeBuilder tb = CreateAssemblyAndDefineType(out AssemblyBuilder assemblyBuilder); + TypeBuilder tb = CreateAssemblyAndDefineType(out PersistedAssemblyBuilder assemblyBuilder); tb.DefineMethod("TestMethod", MethodAttributes.Public).GetILGenerator().Emit(OpCodes.Ret); tb.CreateType(); assemblyBuilder.Save(file.Path); @@ -124,7 +124,7 @@ public void CreateMembersThatUsesTypeLoadedFromCoreAssemblyTest() } } - private static TypeBuilder CreateAssemblyAndDefineType(out AssemblyBuilder assemblyBuilder) + private static TypeBuilder CreateAssemblyAndDefineType(out PersistedAssemblyBuilder assemblyBuilder) { assemblyBuilder = AssemblySaveTools.PopulateAssemblyBuilder(s_assemblyName); return assemblyBuilder.DefineDynamicModule("MyModule") @@ -136,7 +136,7 @@ public void AddInterfaceImplementationTest() { using (TempFile file = TempFile.Create()) { - AssemblyBuilder assemblyBuilder = AssemblySaveTools.PopulateAssemblyBuilder(s_assemblyName); + PersistedAssemblyBuilder assemblyBuilder = AssemblySaveTools.PopulateAssemblyBuilder(s_assemblyName); ModuleBuilder mb = assemblyBuilder.DefineDynamicModule("My Module"); TypeBuilder tb = mb.DefineType("TestInterface", TypeAttributes.Interface | TypeAttributes.Abstract, null, [typeof(IOneMethod)]); tb.AddInterfaceImplementation(typeof(INoMethod)); @@ -176,7 +176,7 @@ public void SaveGenericTypeParametersForAType(string[] typeParamNames) { using (TempFile file = TempFile.Create()) { - TypeBuilder tb = CreateAssemblyAndDefineType(out AssemblyBuilder assemblyBuilder); + TypeBuilder tb = CreateAssemblyAndDefineType(out PersistedAssemblyBuilder assemblyBuilder); MethodBuilder method = tb.DefineMethod("TestMethod", MethodAttributes.Public); method.GetILGenerator().Emit(OpCodes.Ldarg_0); GenericTypeParameterBuilder[] typeParams = tb.DefineGenericParameters(typeParamNames); @@ -245,7 +245,7 @@ public void SaveGenericTypeParametersForAMethod(string[] typeParamNames) { using (TempFile file = TempFile.Create()) { - TypeBuilder tb = CreateAssemblyAndDefineType(out AssemblyBuilder assemblyBuilder); + TypeBuilder tb = CreateAssemblyAndDefineType(out PersistedAssemblyBuilder assemblyBuilder); MethodBuilder method = tb.DefineMethod("TestMethod", MethodAttributes.Public); GenericTypeParameterBuilder[] typeParams = method.DefineGenericParameters(typeParamNames); method.GetILGenerator().Emit(OpCodes.Ldarg_0); @@ -282,7 +282,7 @@ public void SaveArrayTypeSignature(int rank, string name) { using (TempFile file = TempFile.Create()) { - TypeBuilder tb = CreateAssemblyAndDefineType(out AssemblyBuilder assemblyBuilder); + TypeBuilder tb = CreateAssemblyAndDefineType(out PersistedAssemblyBuilder assemblyBuilder); Type arrayType = rank == 0 ? tb.MakeArrayType() : tb.MakeArrayType(rank); MethodBuilder mb = tb.DefineMethod("TestMethod", MethodAttributes.Public); mb.SetReturnType(arrayType); @@ -320,7 +320,7 @@ public void SaveByRefTypeSignature() { using (TempFile file = TempFile.Create()) { - TypeBuilder tb = CreateAssemblyAndDefineType(out AssemblyBuilder assemblyBuilder); + TypeBuilder tb = CreateAssemblyAndDefineType(out PersistedAssemblyBuilder assemblyBuilder); Type byrefType = tb.MakeByRefType(); MethodBuilder mb = tb.DefineMethod("TestMethod", MethodAttributes.Public); mb.SetReturnType(byrefType); @@ -352,7 +352,7 @@ public void SavePointerTypeSignature() { using (TempFile file = TempFile.Create()) { - TypeBuilder tb = CreateAssemblyAndDefineType(out AssemblyBuilder assemblyBuilder); + TypeBuilder tb = CreateAssemblyAndDefineType(out PersistedAssemblyBuilder assemblyBuilder); Type pointerType = tb.MakePointerType(); MethodBuilder mb = tb.DefineMethod("TestMethod", MethodAttributes.Public); mb.SetReturnType(pointerType); @@ -423,7 +423,7 @@ public void SaveGenericTypeSignature(string[] genericParams, Type[] typeArgument { using (TempFile file = TempFile.Create()) { - TypeBuilder tb = CreateAssemblyAndDefineType(out AssemblyBuilder assemblyBuilder); + TypeBuilder tb = CreateAssemblyAndDefineType(out PersistedAssemblyBuilder assemblyBuilder); GenericTypeParameterBuilder[] typeGenParam = tb.DefineGenericParameters(genericParams); Type genericType = tb.MakeGenericType(typeArguments); MethodBuilder mb = tb.DefineMethod("TestMethod", MethodAttributes.Public); @@ -504,7 +504,7 @@ public void SaveGenericTypeSignatureWithGenericParameter() { using (TempFile file = TempFile.Create()) { - TypeBuilder tb = CreateAssemblyAndDefineType(out AssemblyBuilder assemblyBuilder); + TypeBuilder tb = CreateAssemblyAndDefineType(out PersistedAssemblyBuilder assemblyBuilder); GenericTypeParameterBuilder[] typeParams = tb.DefineGenericParameters(["U", "T", "P"]); MethodBuilder mb = tb.DefineMethod("TestMethod", MethodAttributes.Public); GenericTypeParameterBuilder[] methodParams = mb.DefineGenericParameters(["M", "N"]); @@ -545,7 +545,7 @@ public void SaveMultipleGenericTypeParametersToEnsureSortingWorks() { using (TempFile file = TempFile.Create()) { - AssemblyBuilder assemblyBuilder = AssemblySaveTools.PopulateAssemblyBuilder(s_assemblyName); + PersistedAssemblyBuilder assemblyBuilder = AssemblySaveTools.PopulateAssemblyBuilder(s_assemblyName); ModuleBuilder mb = assemblyBuilder.DefineDynamicModule("My Module"); TypeBuilder tb = mb.DefineType("TestInterface1", TypeAttributes.Interface | TypeAttributes.Abstract); GenericTypeParameterBuilder[] typeParams = tb.DefineGenericParameters(["U", "T"]); @@ -610,7 +610,7 @@ public void SaveMultipleGenericTypeParametersToEnsureSortingWorks() [Fact] public void MethodBuilderGetParametersReturnParameterTest() { - AssemblyBuilder assemblyBuilder = AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder type); + PersistedAssemblyBuilder assemblyBuilder = AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder type); MethodBuilder method1 = type.DefineMethod("Method1", MethodAttributes.Public, typeof(long), [typeof(int), typeof(string)]); MethodBuilder method2 = type.DefineMethod("Method2", MethodAttributes.Static); MethodBuilder method3 = type.DefineMethod("Method1", MethodAttributes.Public, typeof(int), [typeof(string)]); diff --git a/src/libraries/System.Reflection.Emit/tests/System.Reflection.Emit.Tests.csproj b/src/libraries/System.Reflection.Emit/tests/System.Reflection.Emit.Tests.csproj index f70ec0e3d751e..5317b25145a55 100644 --- a/src/libraries/System.Reflection.Emit/tests/System.Reflection.Emit.Tests.csproj +++ b/src/libraries/System.Reflection.Emit/tests/System.Reflection.Emit.Tests.csproj @@ -62,17 +62,17 @@ - - - - - - - - - - - + + + + + + + + + + + diff --git a/src/libraries/System.Text.RegularExpressions/src/System/Text/RegularExpressions/RegexAssemblyCompiler.cs b/src/libraries/System.Text.RegularExpressions/src/System/Text/RegularExpressions/RegexAssemblyCompiler.cs index 2e5dc74f733dc..662d8322e1757 100644 --- a/src/libraries/System.Text.RegularExpressions/src/System/Text/RegularExpressions/RegexAssemblyCompiler.cs +++ b/src/libraries/System.Text.RegularExpressions/src/System/Text/RegularExpressions/RegexAssemblyCompiler.cs @@ -25,7 +25,7 @@ internal sealed class RegexAssemblyCompiler : RegexCompiler /// Type count used to augment generated type names to create unique names. private static int s_typeCount; - private readonly AssemblyBuilder _assembly; + private readonly PersistedAssemblyBuilder _assembly; private readonly ModuleBuilder _module; internal RegexAssemblyCompiler(AssemblyName an, CustomAttributeBuilder[]? attribs, string? resourceFile) @@ -36,7 +36,7 @@ internal RegexAssemblyCompiler(AssemblyName an, CustomAttributeBuilder[]? attrib throw new PlatformNotSupportedException(); } - _assembly = AssemblyBuilder.DefinePersistedAssembly(an, typeof(object).Assembly, attribs is not null ? new List(attribs) : null) ?? + _assembly = new PersistedAssemblyBuilder(an, typeof(object).Assembly, attribs is not null ? new List(attribs) : null) ?? throw new InvalidOperationException("DefinePersistedAssembly returned null"); _module = _assembly.DefineDynamicModule(an.Name + ".dll");