diff --git a/src/coreclr/System.Private.CoreLib/System.Private.CoreLib.csproj b/src/coreclr/System.Private.CoreLib/System.Private.CoreLib.csproj index 1dd98e1293225..3c43082e8efd8 100644 --- a/src/coreclr/System.Private.CoreLib/System.Private.CoreLib.csproj +++ b/src/coreclr/System.Private.CoreLib/System.Private.CoreLib.csproj @@ -159,26 +159,26 @@ - - - - - - - - - + + + + + + + + + + - diff --git a/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/CustomAttributeBuilder.cs b/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/CustomAttributeBuilder.cs index ad84b7e9d37b9..65e5c67132a8e 100644 --- a/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/CustomAttributeBuilder.cs +++ b/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/CustomAttributeBuilder.cs @@ -152,14 +152,14 @@ public CustomAttributeBuilder(ConstructorInfo con, object?[] constructorArgs, Pr // Might have failed check because one type is a XXXBuilder // and the other is not. Deal with these special cases // separately. - if (!TypeBuilder.IsTypeEqual(property.DeclaringType, con.DeclaringType)) + if (!RuntimeTypeBuilder.IsTypeEqual(property.DeclaringType, con.DeclaringType)) { // IsSubclassOf is overloaded to do the right thing if // the constructor is a TypeBuilder, but we still need // to deal with the case where the property's declaring // type is one. if (!(property.DeclaringType is TypeBuilder) || - !con.DeclaringType.IsSubclassOf(((TypeBuilder)property.DeclaringType).BakedRuntimeType)) + !con.DeclaringType.IsSubclassOf(((RuntimeTypeBuilder)property.DeclaringType).BakedRuntimeType)) throw new ArgumentException(SR.Argument_BadPropertyForConstructorBuilder); } } @@ -206,14 +206,14 @@ public CustomAttributeBuilder(ConstructorInfo con, object?[] constructorArgs, Pr // Might have failed check because one type is a XXXBuilder // and the other is not. Deal with these special cases // separately. - if (!TypeBuilder.IsTypeEqual(namedField.DeclaringType, con.DeclaringType)) + if (!RuntimeTypeBuilder.IsTypeEqual(namedField.DeclaringType, con.DeclaringType)) { // IsSubclassOf is overloaded to do the right thing if // the constructor is a TypeBuilder, but we still need // to deal with the case where the field's declaring // type is one. if (!(namedField.DeclaringType is TypeBuilder) || - !con.DeclaringType.IsSubclassOf(((TypeBuilder)namedFields[i].DeclaringType!).BakedRuntimeType)) + !con.DeclaringType.IsSubclassOf(((RuntimeTypeBuilder)namedFields[i].DeclaringType!).BakedRuntimeType)) throw new ArgumentException(SR.Argument_BadFieldForConstructorBuilder); } } @@ -517,9 +517,9 @@ private static void EmitValue(BinaryWriter writer, Type type, object? value) } // return the byte interpretation of the custom attribute - internal void CreateCustomAttribute(ModuleBuilder mod, int tkOwner) + internal void CreateCustomAttribute(RuntimeModuleBuilder mod, int tkOwner) { - TypeBuilder.DefineCustomAttribute(mod, tkOwner, mod.GetConstructorToken(m_con), m_blob); + RuntimeTypeBuilder.DefineCustomAttribute(mod, tkOwner, mod.GetMethodMetadataToken(m_con), m_blob); } } } diff --git a/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/DynamicILGenerator.cs b/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/DynamicILGenerator.cs index cd3eff4708cc2..7e228e0c94238 100644 --- a/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/DynamicILGenerator.cs +++ b/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/DynamicILGenerator.cs @@ -440,7 +440,7 @@ private int GetMemberRefToken(MethodInfo methodInfo, Type[]? optionalParameterTy } SignatureHelper sig = GetMethodSigHelper(methodInfo.CallingConvention, - MethodBuilder.GetMethodBaseReturnType(methodInfo), + RuntimeMethodBuilder.GetMethodBaseReturnType(methodInfo), parameterTypes, requiredCustomModifiers, optionalCustomModifiers, diff --git a/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/ILGenerator.cs b/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/ILGenerator.cs index 7b25020361231..0bb7b8d574097 100644 --- a/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/ILGenerator.cs +++ b/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/ILGenerator.cs @@ -94,7 +94,7 @@ internal ILGenerator(MethodInfo methodBuilder, int size) m_methodBuilder = methodBuilder; // initialize local signature - MethodBuilder? mb = m_methodBuilder as MethodBuilder; + RuntimeMethodBuilder? mb = m_methodBuilder as RuntimeMethodBuilder; m_localSignature = SignatureHelper.GetLocalVarSigHelper(mb?.GetTypeBuilder().Module); } @@ -168,7 +168,7 @@ internal void UpdateStackSize(OpCode opcode, int stackchange) private int GetMethodToken(MethodBase method, Type[]? optionalParameterTypes, bool useMethodDef) { - return ((ModuleBuilder)m_methodBuilder.Module).GetMethodTokenInternal(method, optionalParameterTypes, useMethodDef); + return ((RuntimeModuleBuilder)m_methodBuilder.Module).GetMethodTokenInternal(method, optionalParameterTypes, useMethodDef); } internal SignatureHelper GetMemberRefSignature( @@ -188,7 +188,7 @@ private int GetMethodToken(MethodBase method, Type[]? optionalParameterTypes, bo private SignatureHelper GetMemberRefSignature(CallingConventions call, Type? returnType, Type[]? parameterTypes, Type[][]? requiredCustomModifiers, Type[][]? optionalCustomModifiers, Type[]? optionalParameterTypes, int cGenericParameters) { - return ((ModuleBuilder)m_methodBuilder.Module).GetMemberRefSignature(call, returnType, parameterTypes, requiredCustomModifiers, optionalCustomModifiers, optionalParameterTypes, cGenericParameters); + return ((RuntimeModuleBuilder)m_methodBuilder.Module).GetMemberRefSignature(call, returnType, parameterTypes, requiredCustomModifiers, optionalCustomModifiers, optionalParameterTypes, cGenericParameters); } internal byte[]? BakeByteArray() @@ -570,7 +570,7 @@ public virtual void Emit(OpCode opcode, MethodInfo meth) UpdateStackSize(OpCodes.Calli, stackchange); RecordTokenFixup(); - PutInteger4(modBuilder.GetSignatureToken(sig)); + PutInteger4(modBuilder.GetSignatureMetadataToken(sig)); } public virtual void EmitCalli(OpCode opcode, CallingConvention unmanagedCallConv, Type? returnType, Type[]? parameterTypes) @@ -613,7 +613,7 @@ public virtual void EmitCalli(OpCode opcode, CallingConvention unmanagedCallConv EnsureCapacity(7); Emit(OpCodes.Calli); RecordTokenFixup(); - PutInteger4(modBuilder.GetSignatureToken(sig)); + PutInteger4(modBuilder.GetSignatureMetadataToken(sig)); } public virtual void EmitCall(OpCode opcode, MethodInfo methodInfo, Type[]? optionalParameterTypes) @@ -656,7 +656,7 @@ public virtual void Emit(OpCode opcode, SignatureHelper signature) int stackchange = 0; ModuleBuilder modBuilder = (ModuleBuilder)m_methodBuilder.Module; - int sig = modBuilder.GetSignatureToken(signature); + int sig = modBuilder.GetSignatureMetadataToken(signature); int tempVal = sig; @@ -729,19 +729,9 @@ public virtual void Emit(OpCode opcode, Type cls) // by cls. The location of cls is recorded so that the token can be // patched if necessary when persisting the module to a PE. - int tempVal; - ModuleBuilder modBuilder = (ModuleBuilder)m_methodBuilder.Module; - if (opcode == OpCodes.Ldtoken && cls != null && cls.IsGenericTypeDefinition) - { - // This gets the token for the generic type definition if cls is one. - tempVal = modBuilder.GetTypeToken(cls); - } - else - { - // This gets the token for the generic type instantiated on the formal parameters - // if cls is a generic type definition. - tempVal = modBuilder.GetTypeTokenInternal(cls!); - } + RuntimeModuleBuilder modBuilder = (RuntimeModuleBuilder)m_methodBuilder.Module; + bool getGenericDefinition = (opcode == OpCodes.Ldtoken && cls != null && cls.IsGenericTypeDefinition); + int tempVal = modBuilder.GetTypeTokenInternal(cls!, getGenericDefinition); EnsureCapacity(7); InternalEmit(opcode); @@ -824,7 +814,7 @@ public virtual void Emit(OpCode opcode, Label[] labels) public virtual void Emit(OpCode opcode, FieldInfo field) { ModuleBuilder modBuilder = (ModuleBuilder)m_methodBuilder.Module; - int tempVal = modBuilder.GetFieldToken(field); + int tempVal = modBuilder.GetFieldMetadataToken(field); EnsureCapacity(7); InternalEmit(opcode); RecordTokenFixup(); @@ -838,7 +828,7 @@ public virtual void Emit(OpCode opcode, string str) // fixups if the module is persisted to a PE. ModuleBuilder modBuilder = (ModuleBuilder)m_methodBuilder.Module; - int tempVal = modBuilder.GetStringConstant(str); + int tempVal = modBuilder.GetStringMetadataToken(str); EnsureCapacity(7); InternalEmit(opcode); PutInteger4(tempVal); @@ -1307,8 +1297,7 @@ public virtual LocalBuilder DeclareLocal(Type localType, bool pinned) // Declare a local of type "local". The current active lexical scope // will be the scope that local will live. - MethodBuilder? methodBuilder = m_methodBuilder as MethodBuilder; - if (methodBuilder == null) + if (m_methodBuilder is not RuntimeMethodBuilder methodBuilder) throw new NotSupportedException(); if (methodBuilder.IsTypeCreated()) @@ -1337,8 +1326,7 @@ public virtual void UsingNamespace(string usingNamespace) ArgumentException.ThrowIfNullOrEmpty(usingNamespace); - MethodBuilder? methodBuilder = m_methodBuilder as MethodBuilder; - if (methodBuilder == null) + if (m_methodBuilder is not RuntimeMethodBuilder methodBuilder) throw new NotSupportedException(); int index = methodBuilder.GetILGenerator().m_ScopeTree.GetCurrentActiveScopeIndex(); diff --git a/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/ParameterBuilder.cs b/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/ParameterBuilder.cs index 3eef9c642eb86..be7ab2c7df7d5 100644 --- a/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/ParameterBuilder.cs +++ b/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/ParameterBuilder.cs @@ -10,7 +10,7 @@ public class ParameterBuilder // Set the default value of the parameter public virtual void SetConstant(object? defaultValue) { - TypeBuilder.SetConstantValue( + RuntimeTypeBuilder.SetConstantValue( _methodBuilder.GetModuleBuilder(), _token, _position == 0 ? _methodBuilder.ReturnType : _methodBuilder.m_parameterTypes![_position - 1], @@ -23,10 +23,10 @@ public void SetCustomAttribute(ConstructorInfo con, byte[] binaryAttribute) ArgumentNullException.ThrowIfNull(con); ArgumentNullException.ThrowIfNull(binaryAttribute); - TypeBuilder.DefineCustomAttribute( + RuntimeTypeBuilder.DefineCustomAttribute( _methodBuilder.GetModuleBuilder(), _token, - ((ModuleBuilder)_methodBuilder.GetModule()).GetConstructorToken(con), + ((RuntimeModuleBuilder)_methodBuilder.GetModule()).GetMethodMetadataToken(con), binaryAttribute); } @@ -35,11 +35,11 @@ public void SetCustomAttribute(CustomAttributeBuilder customBuilder) { ArgumentNullException.ThrowIfNull(customBuilder); - customBuilder.CreateCustomAttribute((ModuleBuilder)(_methodBuilder.GetModule()), _token); + customBuilder.CreateCustomAttribute((RuntimeModuleBuilder)(_methodBuilder.GetModule()), _token); } internal ParameterBuilder( - MethodBuilder methodBuilder, + RuntimeMethodBuilder methodBuilder, int sequence, ParameterAttributes attributes, string? paramName) // can be NULL string @@ -48,8 +48,8 @@ public void SetCustomAttribute(CustomAttributeBuilder customBuilder) _name = paramName; _methodBuilder = methodBuilder; _attributes = attributes; - ModuleBuilder module = _methodBuilder.GetModuleBuilder(); - _token = TypeBuilder.SetParamInfo( + RuntimeModuleBuilder module = _methodBuilder.GetModuleBuilder(); + _token = RuntimeTypeBuilder.SetParamInfo( new QCallModule(ref module), _methodBuilder.MetadataToken, sequence, @@ -77,7 +77,7 @@ internal int GetToken() private readonly string? _name; private readonly int _position; private readonly ParameterAttributes _attributes; - private MethodBuilder _methodBuilder; + private RuntimeMethodBuilder _methodBuilder; private int _token; } } diff --git a/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/AssemblyBuilder.cs b/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeAssemblyBuilder.cs similarity index 74% rename from src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/AssemblyBuilder.cs rename to src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeAssemblyBuilder.cs index ee01fd51383ea..dddc0c4fee188 100644 --- a/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/AssemblyBuilder.cs +++ b/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeAssemblyBuilder.cs @@ -17,13 +17,62 @@ namespace System.Reflection.Emit { - public sealed partial class AssemblyBuilder : Assembly + public partial class AssemblyBuilder + { + [RequiresDynamicCode("Defining a dynamic assembly requires dynamic code.")] + [DynamicSecurityMethod] // Required to make Assembly.GetCallingAssembly reliable. + public static AssemblyBuilder DefineDynamicAssembly(AssemblyName name, AssemblyBuilderAccess access) + => DefineDynamicAssembly(name, access, null, Assembly.GetCallingAssembly()); + + [RequiresDynamicCode("Defining a dynamic assembly requires dynamic code.")] + [DynamicSecurityMethod] // Required to make Assembly.GetCallingAssembly reliable. + public static AssemblyBuilder DefineDynamicAssembly( + AssemblyName name, + AssemblyBuilderAccess access, + IEnumerable? assemblyAttributes) + => DefineDynamicAssembly(name, access, assemblyAttributes, Assembly.GetCallingAssembly()); + + private static AssemblyBuilder DefineDynamicAssembly( + AssemblyName name, + AssemblyBuilderAccess access, + IEnumerable? assemblyAttributes, + Assembly? callingAssembly) + { + ArgumentNullException.ThrowIfNull(name); + + if (access != AssemblyBuilderAccess.Run && access != AssemblyBuilderAccess.RunAndCollect) + { + throw new ArgumentException(SR.Format(SR.Arg_EnumIllegalVal, (int)access), nameof(access)); + } + + if (callingAssembly == null) + { + // Called either from interop or async delegate invocation. Rejecting because we don't + // know how to set the correct context of the new dynamic assembly. + throw new InvalidOperationException(); + } + + EnsureDynamicCodeSupported(); + + AssemblyLoadContext? assemblyLoadContext = + AssemblyLoadContext.CurrentContextualReflectionContext ?? AssemblyLoadContext.GetLoadContext(callingAssembly); + + if (assemblyLoadContext == null) + { + throw new InvalidOperationException(); + } + + return new RuntimeAssemblyBuilder(name, access, assemblyLoadContext, assemblyAttributes); + } + } + + internal sealed partial class RuntimeAssemblyBuilder : AssemblyBuilder { #region Internal Data Members internal readonly AssemblyBuilderAccess _access; private readonly RuntimeAssembly _internalAssembly; - private readonly ModuleBuilder _manifestModuleBuilder; + private readonly RuntimeModuleBuilder _manifestModuleBuilder; // Set to true if the manifest module was returned by code:DefineDynamicModule to the user private bool _isManifestModuleUsedAsDefinedModule; @@ -37,37 +86,23 @@ public sealed partial class AssemblyBuilder : Assembly #region Constructor - internal AssemblyBuilder(AssemblyName name, + internal RuntimeAssemblyBuilder(AssemblyName name, AssemblyBuilderAccess access, - Assembly? callingAssembly, - AssemblyLoadContext? assemblyLoadContext, + AssemblyLoadContext assemblyLoadContext, IEnumerable? assemblyAttributes) { - ArgumentNullException.ThrowIfNull(name); - - if (access != AssemblyBuilderAccess.Run && access != AssemblyBuilderAccess.RunAndCollect) - { - throw new ArgumentException(SR.Format(SR.Arg_EnumIllegalVal, (int)access), nameof(access)); - } - if (callingAssembly == null) - { - // Called either from interop or async delegate invocation. Rejecting because we don't - // know how to set the correct context of the new dynamic assembly. - throw new InvalidOperationException(); - } - - EnsureDynamicCodeSupported(); + Debug.Assert(name is not null); _access = access; - _internalAssembly = CreateDynamicAssembly(assemblyLoadContext ?? AssemblyLoadContext.GetLoadContext(callingAssembly)!, name, access); + _internalAssembly = CreateDynamicAssembly(assemblyLoadContext, name, access); // Make sure that ManifestModule is properly initialized // We need to do this before setting any CustomAttribute // Note that this ModuleBuilder cannot be used for RefEmit yet // because it hasn't been initialized. // However, it can be used to set the custom attribute on the Assembly - _manifestModuleBuilder = new ModuleBuilder(this, (RuntimeModule)InternalAssembly.ManifestModule); + _manifestModuleBuilder = new RuntimeModuleBuilder(this, (RuntimeModule)InternalAssembly.ManifestModule); if (assemblyAttributes != null) { @@ -82,31 +117,6 @@ public sealed partial class AssemblyBuilder : Assembly #region DefineDynamicAssembly - [RequiresDynamicCode("Defining a dynamic assembly requires dynamic code.")] - [DynamicSecurityMethod] // Required to make Assembly.GetCallingAssembly reliable. - public static AssemblyBuilder DefineDynamicAssembly(AssemblyName name, AssemblyBuilderAccess access) - { - return InternalDefineDynamicAssembly(name, - access, - Assembly.GetCallingAssembly(), - AssemblyLoadContext.CurrentContextualReflectionContext, - null); - } - - [RequiresDynamicCode("Defining a dynamic assembly requires dynamic code.")] - [DynamicSecurityMethod] // Required to make Assembly.GetCallingAssembly reliable. - public static AssemblyBuilder DefineDynamicAssembly( - AssemblyName name, - AssemblyBuilderAccess access, - IEnumerable? assemblyAttributes) - { - return InternalDefineDynamicAssembly(name, - access, - Assembly.GetCallingAssembly(), - AssemblyLoadContext.CurrentContextualReflectionContext, - assemblyAttributes); - } - [LibraryImport(RuntimeHelpers.QCall, EntryPoint = "AppDomain_CreateDynamicAssembly")] private static unsafe partial void CreateDynamicAssembly(ObjectHandleOnStack assemblyLoadContext, NativeAssemblyNameParts* pAssemblyName, @@ -149,19 +159,17 @@ private static unsafe RuntimeAssembly CreateDynamicAssembly(AssemblyLoadContext private static readonly object s_assemblyBuilderLock = new object(); - internal static AssemblyBuilder InternalDefineDynamicAssembly( + internal static RuntimeAssemblyBuilder InternalDefineDynamicAssembly( AssemblyName name, AssemblyBuilderAccess access, - Assembly? callingAssembly, - AssemblyLoadContext? assemblyLoadContext, + AssemblyLoadContext assemblyLoadContext, IEnumerable? assemblyAttributes) { lock (s_assemblyBuilderLock) { // We can only create dynamic assemblies in the current domain - return new AssemblyBuilder(name, + return new RuntimeAssemblyBuilder(name, access, - callingAssembly, assemblyLoadContext, assemblyAttributes); } @@ -175,32 +183,21 @@ private static unsafe RuntimeAssembly CreateDynamicAssembly(AssemblyLoadContext /// modules within an Assembly with the same name. This dynamic module is /// a transient module. /// - public ModuleBuilder DefineDynamicModule(string name) + protected override ModuleBuilder DefineDynamicModuleCore(string _) { lock (SyncRoot) { - return DefineDynamicModuleInternalNoLock(name); - } - } + // Create the dynamic module- only one ModuleBuilder per AssemblyBuilder can be created. + if (_isManifestModuleUsedAsDefinedModule) + { + throw new InvalidOperationException(SR.InvalidOperation_NoMultiModuleAssembly); + } - private ModuleBuilder DefineDynamicModuleInternalNoLock(string name) - { - ArgumentException.ThrowIfNullOrEmpty(name); - if (name[0] == '\0') - { - throw new ArgumentException(SR.Argument_InvalidName, nameof(name)); - } + // We are reusing manifest module as user-defined dynamic module + _isManifestModuleUsedAsDefinedModule = true; - // Create the dynamic module- only one ModuleBuilder per AssemblyBuilder can be created. - if (_isManifestModuleUsedAsDefinedModule) - { - throw new InvalidOperationException(SR.InvalidOperation_NoMultiModuleAssembly); + return _manifestModuleBuilder; } - - // We are reusing manifest module as user-defined dynamic module - _isManifestModuleUsedAsDefinedModule = true; - - return _manifestModuleBuilder; } #endregion @@ -213,10 +210,6 @@ internal void CheckTypeNameConflict(string strTypeName, TypeBuilder? enclosingTy _manifestModuleBuilder.CheckTypeNameConflict(strTypeName, enclosingType); } - public override bool Equals(object? obj) => base.Equals(obj); - - public override int GetHashCode() => base.GetHashCode(); - #region ICustomAttributeProvider Members public override object[] GetCustomAttributes(bool inherit) => InternalAssembly.GetCustomAttributes(inherit); @@ -275,22 +268,11 @@ internal void CheckTypeNameConflict(string strTypeName, TypeBuilder? enclosingTy /// The name of module for the look up. /// Dynamic module with the specified name. - public ModuleBuilder? GetDynamicModule(string name) + protected override ModuleBuilder? GetDynamicModuleCore(string name) { - lock (SyncRoot) - { - return GetDynamicModuleNoLock(name); - } - } - - /// The name of module for the look up. - private ModuleBuilder? GetDynamicModuleNoLock(string name) - { - ArgumentException.ThrowIfNullOrEmpty(name); - if (_isManifestModuleUsedAsDefinedModule) { - if (ModuleBuilder.ManifestModuleName == name) + if (RuntimeModuleBuilder.ManifestModuleName == name) { return _manifestModuleBuilder; } @@ -301,17 +283,14 @@ internal void CheckTypeNameConflict(string strTypeName, TypeBuilder? enclosingTy /// /// Use this function if client decides to form the custom attribute blob themselves. /// - public void SetCustomAttribute(ConstructorInfo con, byte[] binaryAttribute) + protected override void SetCustomAttributeCore(ConstructorInfo con, byte[] binaryAttribute) { - ArgumentNullException.ThrowIfNull(con); - ArgumentNullException.ThrowIfNull(binaryAttribute); - lock (SyncRoot) { - TypeBuilder.DefineCustomAttribute( + RuntimeTypeBuilder.DefineCustomAttribute( _manifestModuleBuilder, // pass in the in-memory assembly module AssemblyDefToken, - _manifestModuleBuilder.GetConstructorToken(con), + _manifestModuleBuilder.GetMethodMetadataToken(con), binaryAttribute); } } @@ -319,10 +298,8 @@ public void SetCustomAttribute(ConstructorInfo con, byte[] binaryAttribute) /// /// Use this function if client wishes to build CustomAttribute using CustomAttributeBuilder. /// - public void SetCustomAttribute(CustomAttributeBuilder customBuilder) + protected override void SetCustomAttributeCore(CustomAttributeBuilder customBuilder) { - ArgumentNullException.ThrowIfNull(customBuilder); - lock (SyncRoot) { customBuilder.CreateCustomAttribute(_manifestModuleBuilder, AssemblyDefToken); diff --git a/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/ConstructorBuilder.cs b/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeConstructorBuilder.cs similarity index 78% rename from src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/ConstructorBuilder.cs rename to src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeConstructorBuilder.cs index cdef535524ac7..86b20be8e7ff0 100644 --- a/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/ConstructorBuilder.cs +++ b/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeConstructorBuilder.cs @@ -6,17 +6,17 @@ namespace System.Reflection.Emit { - public sealed class ConstructorBuilder : ConstructorInfo + internal sealed class RuntimeConstructorBuilder : ConstructorBuilder { - private readonly MethodBuilder m_methodBuilder; + private readonly RuntimeMethodBuilder m_methodBuilder; internal bool m_isDefaultConstructor; #region Constructor - internal ConstructorBuilder(string name, MethodAttributes attributes, CallingConventions callingConvention, - Type[]? parameterTypes, Type[][]? requiredCustomModifiers, Type[][]? optionalCustomModifiers, ModuleBuilder mod, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] TypeBuilder type) + internal RuntimeConstructorBuilder(string name, MethodAttributes attributes, CallingConventions callingConvention, + Type[]? parameterTypes, Type[][]? requiredCustomModifiers, Type[][]? optionalCustomModifiers, RuntimeModuleBuilder mod, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] RuntimeTypeBuilder type) { - m_methodBuilder = new MethodBuilder(name, attributes, callingConvention, null, null, null, + m_methodBuilder = new RuntimeMethodBuilder(name, attributes, callingConvention, null, null, null, parameterTypes, requiredCustomModifiers, optionalCustomModifiers, mod, type); type.m_listMethods!.Add(m_methodBuilder); @@ -26,8 +26,8 @@ public sealed class ConstructorBuilder : ConstructorInfo _ = m_methodBuilder.MetadataToken; // Doubles as "CreateMethod" for MethodBuilder -- analogous to CreateType() } - internal ConstructorBuilder(string name, MethodAttributes attributes, CallingConventions callingConvention, - Type[]? parameterTypes, ModuleBuilder mod, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] TypeBuilder type) : + internal RuntimeConstructorBuilder(string name, MethodAttributes attributes, CallingConventions callingConvention, + Type[]? parameterTypes, RuntimeModuleBuilder mod, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] RuntimeTypeBuilder type) : this(name, attributes, callingConvention, parameterTypes, null, null, mod, type) { } @@ -41,7 +41,7 @@ internal override Type[] GetParameterTypes() } [return: DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] - private TypeBuilder GetTypeBuilder() + private RuntimeTypeBuilder GetTypeBuilder() { return m_methodBuilder.GetTypeBuilder(); } @@ -122,7 +122,7 @@ public override bool IsDefined(Type attributeType, bool inherit) #endregion #region Public Members - public ParameterBuilder DefineParameter(int iSequence, ParameterAttributes attributes, string? strParamName) + protected override ParameterBuilder DefineParameterCore(int iSequence, ParameterAttributes attributes, string? strParamName) { // Theoretically we shouldn't allow iSequence to be 0 because in reflection ctors don't have // return parameters. But we'll allow it for backward compatibility with V2. The attributes @@ -133,15 +133,7 @@ public ParameterBuilder DefineParameter(int iSequence, ParameterAttributes attri return m_methodBuilder.DefineParameter(iSequence, attributes, strParamName); } - public ILGenerator GetILGenerator() - { - if (m_isDefaultConstructor) - throw new InvalidOperationException(SR.InvalidOperation_DefaultConstructorILGen); - - return m_methodBuilder.GetILGenerator(); - } - - public ILGenerator GetILGenerator(int streamSize) + protected override ILGenerator GetILGeneratorCore(int streamSize) { if (m_isDefaultConstructor) throw new InvalidOperationException(SR.InvalidOperation_DefaultConstructorILGen); @@ -165,22 +157,22 @@ internal override Type GetReturnType() return m_methodBuilder.ReturnType; } - public void SetCustomAttribute(ConstructorInfo con, byte[] binaryAttribute) + protected override void SetCustomAttributeCore(ConstructorInfo con, byte[] binaryAttribute) { m_methodBuilder.SetCustomAttribute(con, binaryAttribute); } - public void SetCustomAttribute(CustomAttributeBuilder customBuilder) + protected override void SetCustomAttributeCore(CustomAttributeBuilder customBuilder) { m_methodBuilder.SetCustomAttribute(customBuilder); } - public void SetImplementationFlags(MethodImplAttributes attributes) + protected override void SetImplementationFlagsCore(MethodImplAttributes attributes) { m_methodBuilder.SetImplementationFlags(attributes); } - public bool InitLocals + protected override bool InitLocalsCore { get => m_methodBuilder.InitLocals; set => m_methodBuilder.InitLocals = value; diff --git a/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/EnumBuilder.cs b/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeEnumBuilder.cs similarity index 90% rename from src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/EnumBuilder.cs rename to src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeEnumBuilder.cs index 0ed9ab0867784..de3a31ada9e47 100644 --- a/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/EnumBuilder.cs +++ b/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeEnumBuilder.cs @@ -17,7 +17,7 @@ namespace System.Reflection.Emit { - public sealed class EnumBuilder : TypeInfo + internal sealed class RuntimeEnumBuilder : EnumBuilder { public override bool IsAssignableFrom([NotNullWhen(true)] TypeInfo? typeInfo) { @@ -27,7 +27,7 @@ public override bool IsAssignableFrom([NotNullWhen(true)] TypeInfo? typeInfo) // Define literal for enum - public FieldBuilder DefineLiteral(string literalName, object? literalValue) + protected override FieldBuilder DefineLiteralCore(string literalName, object? literalValue) { // Define the underlying field for the enum. It will be a non-static, private field with special name bit set. FieldBuilder fieldBuilder = m_typeBuilder.DefineField( @@ -38,25 +38,18 @@ public FieldBuilder DefineLiteral(string literalName, object? literalValue) return fieldBuilder; } - [return: DynamicallyAccessedMembersAttribute(DynamicallyAccessedMemberTypes.All)] - public TypeInfo CreateTypeInfo() + [return: DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] + protected override TypeInfo CreateTypeInfoCore() { return m_typeBuilder.CreateTypeInfo(); } - // CreateType cause EnumBuilder to be baked. - [return: DynamicallyAccessedMembersAttribute(DynamicallyAccessedMemberTypes.All)] - public Type CreateType() - { - return m_typeBuilder.CreateType(); - } - // Get the internal metadata token for this class. internal int TypeToken => m_typeBuilder.TypeToken; // return the underlying field for the enum - public FieldBuilder UnderlyingField => m_underlyingField; + protected override FieldBuilder UnderlyingFieldCore => m_underlyingField; public override string Name => m_typeBuilder.Name; @@ -282,13 +275,13 @@ public override object[] GetCustomAttributes(Type attributeType, bool inherit) // Use this function if client decides to form the custom attribute blob themselves - public void SetCustomAttribute(ConstructorInfo con, byte[] binaryAttribute) + protected override void SetCustomAttributeCore(ConstructorInfo con, byte[] binaryAttribute) { m_typeBuilder.SetCustomAttribute(con, binaryAttribute); } // Use this function if client wishes to build CustomAttribute using CustomAttributeBuilder - public void SetCustomAttribute(CustomAttributeBuilder customBuilder) + protected override void SetCustomAttributeCore(CustomAttributeBuilder customBuilder) { m_typeBuilder.SetCustomAttribute(customBuilder); } @@ -323,13 +316,13 @@ public override Type MakeByRefType() return SymbolType.FormCompoundType("&", this, 0)!; } - [RequiresDynamicCodeAttribute("The code for an array of the specified type might not be available.")] + [RequiresDynamicCode("The code for an array of the specified type might not be available.")] public override Type MakeArrayType() { return SymbolType.FormCompoundType("[]", this, 0)!; } - [RequiresDynamicCodeAttribute("The code for an array of the specified type might not be available.")] + [RequiresDynamicCode("The code for an array of the specified type might not be available.")] public override Type MakeArrayType(int rank) { string s = GetRankString(rank); @@ -341,16 +334,16 @@ public override Type MakeArrayType(int rank) // EnumBuilder can only be a top-level (not nested) enum type. [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2064:UnrecognizedReflectionPattern", Justification = "Reflection.Emit is not subject to trimming")] - internal EnumBuilder( + internal RuntimeEnumBuilder( string name, // name of type Type underlyingType, // underlying type for an Enum TypeAttributes visibility, // any bits on TypeAttributes.VisibilityMask) - ModuleBuilder module) // module containing this type + RuntimeModuleBuilder module) // module containing this type { // Client should not set any bits other than the visibility bits. if ((visibility & ~TypeAttributes.VisibilityMask) != 0) throw new ArgumentException(SR.Argument_ShouldOnlySetVisibilityFlags, nameof(name)); - m_typeBuilder = new TypeBuilder(name, visibility | TypeAttributes.Sealed, typeof(System.Enum), null, module, PackingSize.Unspecified, TypeBuilder.UnspecifiedTypeSize, null); + m_typeBuilder = new RuntimeTypeBuilder(name, visibility | TypeAttributes.Sealed, typeof(System.Enum), null, module, PackingSize.Unspecified, TypeBuilder.UnspecifiedTypeSize, null); // Define the underlying field for the enum. It will be a non-static, private field with special name bit set. m_underlyingField = m_typeBuilder.DefineField("value__", underlyingType, FieldAttributes.Public | FieldAttributes.SpecialName | FieldAttributes.RTSpecialName); @@ -362,7 +355,7 @@ public override Type MakeArrayType(int rank) * */ [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] - internal TypeBuilder m_typeBuilder; + internal RuntimeTypeBuilder m_typeBuilder; private FieldBuilder m_underlyingField; } diff --git a/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/EventBuilder.cs b/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeEventBuilder.cs similarity index 63% rename from src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/EventBuilder.cs rename to src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeEventBuilder.cs index c28109acf2d12..4e990511d9436 100644 --- a/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/EventBuilder.cs +++ b/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeEventBuilder.cs @@ -20,16 +20,15 @@ namespace System.Reflection.Emit // A EventBuilder is always associated with a TypeBuilder. The TypeBuilder.DefineEvent // method will return a new EventBuilder to a client. // - public sealed class EventBuilder + internal sealed class RuntimeEventBuilder : EventBuilder { - // Constructs a EventBuilder. + // Constructs a RuntimeEventBuilder. // - internal EventBuilder( - ModuleBuilder mod, // the module containing this EventBuilder - string name, // Event name - EventAttributes attr, // event attribute such as Public, Private, and Protected defined above - // int eventType, // event type - TypeBuilder type, // containing type + internal RuntimeEventBuilder( + RuntimeModuleBuilder mod, // the module containing this EventBuilder + string name, // Event name + EventAttributes attr, // event attribute such as Public, Private, and Protected defined above + RuntimeTypeBuilder type, // containing type int evToken) { m_name = name; @@ -51,55 +50,50 @@ private void SetMethodSemantics(MethodBuilder mdBuilder, MethodSemanticsAttribut ArgumentNullException.ThrowIfNull(mdBuilder); m_type.ThrowIfCreated(); - ModuleBuilder module = m_module; - TypeBuilder.DefineMethodSemantics( + RuntimeModuleBuilder module = m_module; + RuntimeTypeBuilder.DefineMethodSemantics( new QCallModule(ref module), m_evToken, semantics, mdBuilder.MetadataToken); } - public void SetAddOnMethod(MethodBuilder mdBuilder) + protected override void SetAddOnMethodCore(MethodBuilder mdBuilder) { SetMethodSemantics(mdBuilder, MethodSemanticsAttributes.AddOn); } - public void SetRemoveOnMethod(MethodBuilder mdBuilder) + protected override void SetRemoveOnMethodCore(MethodBuilder mdBuilder) { SetMethodSemantics(mdBuilder, MethodSemanticsAttributes.RemoveOn); } - public void SetRaiseMethod(MethodBuilder mdBuilder) + protected override void SetRaiseMethodCore(MethodBuilder mdBuilder) { SetMethodSemantics(mdBuilder, MethodSemanticsAttributes.Fire); } - public void AddOtherMethod(MethodBuilder mdBuilder) + protected override void AddOtherMethodCore(MethodBuilder mdBuilder) { SetMethodSemantics(mdBuilder, MethodSemanticsAttributes.Other); } // Use this function if client decides to form the custom attribute blob themselves - public void SetCustomAttribute(ConstructorInfo con, byte[] binaryAttribute) + protected override void SetCustomAttributeCore(ConstructorInfo con, byte[] binaryAttribute) { - ArgumentNullException.ThrowIfNull(con); - ArgumentNullException.ThrowIfNull(binaryAttribute); - m_type.ThrowIfCreated(); - TypeBuilder.DefineCustomAttribute( + RuntimeTypeBuilder.DefineCustomAttribute( m_module, m_evToken, - m_module.GetConstructorToken(con), + m_module.GetMethodMetadataToken(con), binaryAttribute); } // Use this function if client wishes to build CustomAttribute using CustomAttributeBuilder - public void SetCustomAttribute(CustomAttributeBuilder customBuilder) + protected override void SetCustomAttributeCore(CustomAttributeBuilder customBuilder) { - ArgumentNullException.ThrowIfNull(customBuilder); - m_type.ThrowIfCreated(); customBuilder.CreateCustomAttribute(m_module, m_evToken); } @@ -107,8 +101,8 @@ public void SetCustomAttribute(CustomAttributeBuilder customBuilder) // These are package private so that TypeBuilder can access them. private string m_name; // The name of the event private int m_evToken; // The token of this event - private ModuleBuilder m_module; + private RuntimeModuleBuilder m_module; private EventAttributes m_attributes; - private TypeBuilder m_type; + private RuntimeTypeBuilder m_type; } } diff --git a/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/FieldBuilder.cs b/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeFieldBuilder.cs similarity index 74% rename from src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/FieldBuilder.cs rename to src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeFieldBuilder.cs index a5c2723524a67..9fcaa7d75385e 100644 --- a/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/FieldBuilder.cs +++ b/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeFieldBuilder.cs @@ -6,18 +6,18 @@ namespace System.Reflection.Emit { - public sealed class FieldBuilder : FieldInfo + internal sealed class RuntimeFieldBuilder : FieldBuilder { #region Private Data Members private int m_fieldTok; - private TypeBuilder m_typeBuilder; + private RuntimeTypeBuilder m_typeBuilder; private string m_fieldName; private FieldAttributes m_Attributes; private Type m_fieldType; #endregion #region Constructor - internal FieldBuilder(TypeBuilder typeBuilder, string fieldName, Type type, + internal RuntimeFieldBuilder(RuntimeTypeBuilder typeBuilder, string fieldName, Type type, Type[]? requiredCustomModifiers, Type[]? optionalCustomModifiers, FieldAttributes attributes) { ArgumentException.ThrowIfNullOrEmpty(fieldName); @@ -40,8 +40,8 @@ public sealed class FieldBuilder : FieldInfo byte[] signature = sigHelp.InternalGetSignature(out int sigLength); - ModuleBuilder module = m_typeBuilder.GetModuleBuilder(); - m_fieldTok = TypeBuilder.DefineField(new QCallModule(ref module), + RuntimeModuleBuilder module = m_typeBuilder.GetModuleBuilder(); + m_fieldTok = RuntimeTypeBuilder.DefineField(new QCallModule(ref module), typeBuilder.TypeToken, fieldName, signature, sigLength, m_Attributes); } @@ -50,8 +50,8 @@ public sealed class FieldBuilder : FieldInfo #region Internal Members internal void SetData(byte[]? data, int size) { - ModuleBuilder module = m_typeBuilder.GetModuleBuilder(); - ModuleBuilder.SetFieldRVAContent(new QCallModule(ref module), m_fieldTok, data, size); + RuntimeModuleBuilder module = m_typeBuilder.GetModuleBuilder(); + RuntimeModuleBuilder.SetFieldRVAContent(new QCallModule(ref module), m_fieldTok, data, size); } #endregion @@ -131,16 +131,16 @@ public override bool IsDefined(Type attributeType, bool inherit) #endregion - #region Public Members - public void SetOffset(int iOffset) + #region Protected Members Overrides + protected override void SetOffsetCore(int iOffset) { m_typeBuilder.ThrowIfCreated(); - ModuleBuilder module = m_typeBuilder.GetModuleBuilder(); - TypeBuilder.SetFieldLayoutOffset(new QCallModule(ref module), m_fieldTok, iOffset); + RuntimeModuleBuilder module = m_typeBuilder.GetModuleBuilder(); + RuntimeTypeBuilder.SetFieldLayoutOffset(new QCallModule(ref module), m_fieldTok, iOffset); } - public void SetConstant(object? defaultValue) + protected override void SetConstantCore(object? defaultValue) { m_typeBuilder.ThrowIfCreated(); @@ -151,31 +151,24 @@ public void SetConstant(object? defaultValue) throw new ArgumentException(SR.Argument_ConstantNull); } - TypeBuilder.SetConstantValue(m_typeBuilder.GetModuleBuilder(), m_fieldTok, m_fieldType, defaultValue); + RuntimeTypeBuilder.SetConstantValue(m_typeBuilder.GetModuleBuilder(), m_fieldTok, m_fieldType, defaultValue); } - public void SetCustomAttribute(ConstructorInfo con, byte[] binaryAttribute) + protected override void SetCustomAttributeCore(ConstructorInfo con, byte[] binaryAttribute) { - ArgumentNullException.ThrowIfNull(con); - ArgumentNullException.ThrowIfNull(binaryAttribute); - - ModuleBuilder module = (m_typeBuilder.Module as ModuleBuilder)!; + RuntimeModuleBuilder moduleBuilder = (RuntimeModuleBuilder)m_typeBuilder.Module; m_typeBuilder.ThrowIfCreated(); - TypeBuilder.DefineCustomAttribute(module, - m_fieldTok, module.GetConstructorToken(con), binaryAttribute); + RuntimeTypeBuilder.DefineCustomAttribute(moduleBuilder, + m_fieldTok, moduleBuilder.GetMethodMetadataToken(con), binaryAttribute); } - public void SetCustomAttribute(CustomAttributeBuilder customBuilder) + protected override void SetCustomAttributeCore(CustomAttributeBuilder customBuilder) { - ArgumentNullException.ThrowIfNull(customBuilder); - m_typeBuilder.ThrowIfCreated(); - ModuleBuilder? module = m_typeBuilder.Module as ModuleBuilder; - - customBuilder.CreateCustomAttribute(module!, m_fieldTok); + customBuilder.CreateCustomAttribute((RuntimeModuleBuilder)m_typeBuilder.Module, m_fieldTok); } #endregion diff --git a/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/GenericTypeParameterBuilder.cs b/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeGenericTypeParameterBuilder.cs similarity index 91% rename from src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/GenericTypeParameterBuilder.cs rename to src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeGenericTypeParameterBuilder.cs index 94eac80d7c32f..6301e2ab4c509 100644 --- a/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/GenericTypeParameterBuilder.cs +++ b/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeGenericTypeParameterBuilder.cs @@ -6,7 +6,7 @@ namespace System.Reflection.Emit { - public sealed class GenericTypeParameterBuilder : TypeInfo + internal sealed class RuntimeGenericTypeParameterBuilder : GenericTypeParameterBuilder { public override bool IsAssignableFrom([NotNullWhen(true)] TypeInfo? typeInfo) { @@ -15,11 +15,11 @@ public override bool IsAssignableFrom([NotNullWhen(true)] TypeInfo? typeInfo) } #region Private Data Members - internal TypeBuilder m_type; + internal RuntimeTypeBuilder m_type; #endregion #region Constructor - internal GenericTypeParameterBuilder(TypeBuilder type) + internal RuntimeGenericTypeParameterBuilder(RuntimeTypeBuilder type) { m_type = type; } @@ -32,12 +32,12 @@ public override string ToString() } public override bool Equals(object? o) { - GenericTypeParameterBuilder? g = o as GenericTypeParameterBuilder; + if (o is RuntimeGenericTypeParameterBuilder g) + { + return ReferenceEquals(g.m_type, m_type); + } - if (g == null) - return false; - - return object.ReferenceEquals(g.m_type, m_type); + return false; } public override int GetHashCode() { return m_type.GetHashCode(); } #endregion @@ -214,28 +214,28 @@ public override Type MakeArrayType(int rank) public override bool IsDefined(Type attributeType, bool inherit) { throw new NotSupportedException(); } #endregion - #region Public Members - public void SetCustomAttribute(ConstructorInfo con, byte[] binaryAttribute) + #region Protected Members Overrides + protected override void SetCustomAttributeCore(ConstructorInfo con, byte[] binaryAttribute) { m_type.SetGenParamCustomAttribute(con, binaryAttribute); } - public void SetCustomAttribute(CustomAttributeBuilder customBuilder) + protected override void SetCustomAttributeCore(CustomAttributeBuilder customBuilder) { m_type.SetGenParamCustomAttribute(customBuilder); } - public void SetBaseTypeConstraint([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type? baseTypeConstraint) + protected override void SetBaseTypeConstraintCore([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type? baseTypeConstraint) { m_type.SetParent(baseTypeConstraint); } - public void SetInterfaceConstraints(params Type[]? interfaceConstraints) + protected override void SetInterfaceConstraintsCore(params Type[]? interfaceConstraints) { m_type.SetInterfaces(interfaceConstraints); } - public void SetGenericParameterAttributes(GenericParameterAttributes genericParameterAttributes) + protected override void SetGenericParameterAttributesCore(GenericParameterAttributes genericParameterAttributes) { m_type.SetGenParamAttributes(genericParameterAttributes); } diff --git a/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/MethodBuilder.cs b/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeMethodBuilder.cs similarity index 90% rename from src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/MethodBuilder.cs rename to src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeMethodBuilder.cs index c4892500d1746..6b2cdad3cc258 100644 --- a/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/MethodBuilder.cs +++ b/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeMethodBuilder.cs @@ -11,16 +11,16 @@ namespace System.Reflection.Emit { - public sealed class MethodBuilder : MethodInfo + internal sealed class RuntimeMethodBuilder : MethodBuilder { #region Private Data Members // Identity internal string m_strName; // The name of the method private int m_token; // The token of this method - private readonly ModuleBuilder m_module; + private readonly RuntimeModuleBuilder m_module; [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] - internal TypeBuilder m_containingType; + internal RuntimeTypeBuilder m_containingType; // IL private int[]? m_mdMethodFixups; // The location of all of the token fixups. Null means no fixups. @@ -50,16 +50,16 @@ public sealed class MethodBuilder : MethodInfo private Type[][]? m_parameterTypeOptionalCustomModifiers; // Generics - private GenericTypeParameterBuilder[]? m_inst; + private RuntimeGenericTypeParameterBuilder[]? m_inst; private bool m_bIsGenMethDef; #endregion #region Constructor - internal MethodBuilder(string name, MethodAttributes attributes, CallingConventions callingConvention, + internal RuntimeMethodBuilder(string name, MethodAttributes attributes, CallingConventions callingConvention, Type? returnType, Type[]? returnTypeRequiredCustomModifiers, Type[]? returnTypeOptionalCustomModifiers, Type[]? parameterTypes, Type[][]? parameterTypeRequiredCustomModifiers, Type[][]? parameterTypeOptionalCustomModifiers, - ModuleBuilder mod, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] TypeBuilder type) + RuntimeModuleBuilder mod, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] RuntimeTypeBuilder type) { ArgumentException.ThrowIfNullOrEmpty(name); @@ -146,7 +146,7 @@ internal void CreateMethodBodyHelper(ILGenerator il) int[] type; int numCatch; int start, end; - ModuleBuilder dynMod = (ModuleBuilder)m_module; + RuntimeModuleBuilder dynMod = m_module; m_containingType.ThrowIfCreated(); @@ -346,12 +346,12 @@ internal bool IsTypeCreated() } [return: DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] - internal TypeBuilder GetTypeBuilder() + internal RuntimeTypeBuilder GetTypeBuilder() { return m_containingType; } - internal ModuleBuilder GetModuleBuilder() + internal RuntimeModuleBuilder GetModuleBuilder() { return m_module; } @@ -360,21 +360,21 @@ internal ModuleBuilder GetModuleBuilder() #region Object Overrides public override bool Equals(object? obj) { - if (!(obj is MethodBuilder)) + if (obj is not RuntimeMethodBuilder rmBuilder) { return false; } - if (!m_strName.Equals(((MethodBuilder)obj).m_strName)) + if (!m_strName.Equals(rmBuilder.m_strName)) { return false; } - if (m_iAttributes != (((MethodBuilder)obj).m_iAttributes)) + if (m_iAttributes != rmBuilder.m_iAttributes) { return false; } - SignatureHelper thatSig = ((MethodBuilder)obj).GetMethodSignature(); + SignatureHelper thatSig = rmBuilder.GetMethodSignature(); if (thatSig.Equals(GetMethodSignature())) { return true; @@ -514,13 +514,8 @@ public override MethodInfo MakeGenericMethod(params Type[] typeArguments) return MethodBuilderInstantiation.MakeGenericMethod(this, typeArguments); } - public GenericTypeParameterBuilder[] DefineGenericParameters(params string[] names) + protected override GenericTypeParameterBuilder[] DefineGenericParametersCore(params string[] names) { - ArgumentNullException.ThrowIfNull(names); - - if (names.Length == 0) - throw new ArgumentException(SR.Arg_EmptyArray, nameof(names)); - if (m_inst != null) throw new InvalidOperationException(SR.InvalidOperation_GenericParametersAlreadySet); @@ -531,9 +526,9 @@ public GenericTypeParameterBuilder[] DefineGenericParameters(params string[] nam throw new InvalidOperationException(SR.InvalidOperation_MethodBuilderBaked); m_bIsGenMethDef = true; - m_inst = new GenericTypeParameterBuilder[names.Length]; + m_inst = new RuntimeGenericTypeParameterBuilder[names.Length]; for (int i = 0; i < names.Length; i++) - m_inst[i] = new GenericTypeParameterBuilder(new TypeBuilder(names[i], i, this)); + m_inst[i] = new RuntimeGenericTypeParameterBuilder(new RuntimeTypeBuilder(names[i], i, this)); return m_inst; } @@ -541,7 +536,7 @@ public GenericTypeParameterBuilder[] DefineGenericParameters(params string[] nam internal void ThrowIfGeneric() { if (IsGenericMethod && !IsGenericMethodDefinition) throw new InvalidOperationException(); } #endregion - #region Public Members + #region Private Members private int GetToken() { // We used to always "tokenize" a MethodBuilder when it is constructed. After change list 709498 @@ -559,7 +554,7 @@ private int GetToken() return m_token; } - MethodBuilder? currentMethod = null; + RuntimeMethodBuilder? currentMethod = null; int currentToken = 0; int i; @@ -598,31 +593,23 @@ private int GetTokenNoLock() Debug.Assert(m_token == 0, "m_token should not have been initialized"); byte[] sigBytes = GetMethodSignature().InternalGetSignature(out int sigLength); - ModuleBuilder module = m_module; + RuntimeModuleBuilder module = m_module; - int token = TypeBuilder.DefineMethod(new QCallModule(ref module), m_containingType.MetadataToken, m_strName, sigBytes, sigLength, Attributes); + int token = RuntimeTypeBuilder.DefineMethod(new QCallModule(ref module), m_containingType.MetadataToken, m_strName, sigBytes, sigLength, Attributes); m_token = token; if (m_inst != null) - foreach (GenericTypeParameterBuilder tb in m_inst) + foreach (RuntimeGenericTypeParameterBuilder tb in m_inst) if (!tb.m_type.IsCreated()) tb.m_type.CreateType(); - TypeBuilder.SetMethodImpl(new QCallModule(ref module), token, m_dwMethodImplFlags); + RuntimeTypeBuilder.SetMethodImpl(new QCallModule(ref module), token, m_dwMethodImplFlags); return m_token; } + #endregion - public void SetParameters(params Type[] parameterTypes) - { - SetSignature(null, null, null, parameterTypes, null, null); - } - - public void SetReturnType(Type? returnType) - { - SetSignature(returnType, null, null, null, null, null); - } - - public void SetSignature( + #region Protected Members Overrides + protected override void SetSignatureCore( Type? returnType, Type[]? returnTypeRequiredCustomModifiers, Type[]? returnTypeOptionalCustomModifiers, Type[]? parameterTypes, Type[][]? parameterTypeRequiredCustomModifiers, Type[][]? parameterTypeOptionalCustomModifiers) { @@ -650,10 +637,8 @@ public void SetReturnType(Type? returnType) m_parameterTypeOptionalCustomModifiers = parameterTypeOptionalCustomModifiers; } - public ParameterBuilder DefineParameter(int position, ParameterAttributes attributes, string? strParamName) + protected override ParameterBuilder DefineParameterCore(int position, ParameterAttributes attributes, string? strParamName) { - ArgumentOutOfRangeException.ThrowIfNegative(position); - ThrowIfGeneric(); m_containingType.ThrowIfCreated(); @@ -664,29 +649,20 @@ public ParameterBuilder DefineParameter(int position, ParameterAttributes attrib return new ParameterBuilder(this, position, attributes, strParamName); } - public void SetImplementationFlags(MethodImplAttributes attributes) + protected override void SetImplementationFlagsCore(MethodImplAttributes attributes) { ThrowIfGeneric(); - m_containingType.ThrowIfCreated(); m_dwMethodImplFlags = attributes; m_canBeRuntimeImpl = true; - ModuleBuilder module = m_module; - TypeBuilder.SetMethodImpl(new QCallModule(ref module), MetadataToken, attributes); + RuntimeModuleBuilder module = m_module; + RuntimeTypeBuilder.SetMethodImpl(new QCallModule(ref module), MetadataToken, attributes); } - public ILGenerator GetILGenerator() - { - ThrowIfGeneric(); - ThrowIfShouldNotHaveBody(); - - return m_ilGenerator ??= new ILGenerator(this); - } - - public ILGenerator GetILGenerator(int size) + protected override ILGenerator GetILGeneratorCore(int size) { ThrowIfGeneric(); ThrowIfShouldNotHaveBody(); @@ -707,7 +683,7 @@ private void ThrowIfShouldNotHaveBody() } } - public bool InitLocals + protected override bool InitLocalsCore { // Property is set to true if user wishes to have zero initialized stack frame for this method. Default to false. get { ThrowIfGeneric(); return m_fInitLocals; } @@ -719,28 +695,21 @@ internal Module GetModule() return GetModuleBuilder(); } - public void SetCustomAttribute(ConstructorInfo con, byte[] binaryAttribute) + protected override void SetCustomAttributeCore(ConstructorInfo con, byte[] binaryAttribute) { - ArgumentNullException.ThrowIfNull(con); - ArgumentNullException.ThrowIfNull(binaryAttribute); - ThrowIfGeneric(); - - TypeBuilder.DefineCustomAttribute(m_module, MetadataToken, - ((ModuleBuilder)m_module).GetConstructorToken(con), + RuntimeTypeBuilder.DefineCustomAttribute(m_module, MetadataToken, + m_module.GetMethodMetadataToken(con), binaryAttribute); if (IsKnownCA(con)) ParseCA(con); } - public void SetCustomAttribute(CustomAttributeBuilder customBuilder) + protected override void SetCustomAttributeCore(CustomAttributeBuilder customBuilder) { - ArgumentNullException.ThrowIfNull(customBuilder); - ThrowIfGeneric(); - - customBuilder.CreateCustomAttribute((ModuleBuilder)m_module, MetadataToken); + customBuilder.CreateCustomAttribute(m_module, MetadataToken); if (IsKnownCA(customBuilder.m_con)) ParseCA(customBuilder.m_con); diff --git a/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/ModuleBuilder.cs b/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeModuleBuilder.cs similarity index 83% rename from src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/ModuleBuilder.cs rename to src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeModuleBuilder.cs index 514736b2778ea..d607dc939782a 100644 --- a/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/ModuleBuilder.cs +++ b/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeModuleBuilder.cs @@ -4,13 +4,12 @@ using System.Collections.Generic; using System.Diagnostics; using System.Diagnostics.CodeAnalysis; -using System.Diagnostics.SymbolStore; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; namespace System.Reflection.Emit { - public partial class ModuleBuilder : Module + internal sealed partial class RuntimeModuleBuilder : ModuleBuilder { internal static string UnmangleTypeName(string typeName) { @@ -54,8 +53,8 @@ internal static string UnmangleTypeName(string typeName) internal readonly RuntimeModule _internalModule; // This is the "external" AssemblyBuilder // only the "external" ModuleBuilder has this set - private readonly AssemblyBuilder _assemblyBuilder; - internal AssemblyBuilder ContainingAssemblyBuilder => _assemblyBuilder; + private readonly RuntimeAssemblyBuilder _assemblyBuilder; + internal RuntimeAssemblyBuilder ContainingAssemblyBuilder => _assemblyBuilder; internal const string ManifestModuleName = "RefEmit_InMemoryManifestModule"; @@ -63,12 +62,12 @@ internal static string UnmangleTypeName(string typeName) #region Constructor - internal ModuleBuilder(AssemblyBuilder assemblyBuilder, RuntimeModule internalModule) + internal RuntimeModuleBuilder(RuntimeAssemblyBuilder assemblyBuilder, RuntimeModule internalModule) { _internalModule = internalModule; _assemblyBuilder = assemblyBuilder; - _globalTypeBuilder = new TypeBuilder(this); + _globalTypeBuilder = new RuntimeTypeBuilder(this); _typeBuilderDict = new Dictionary(); } @@ -107,7 +106,7 @@ internal void CheckTypeNameConflict(string strTypeName, Type? enclosingType) private int GetMemberRef(Module? refedModule, int tr, int defToken) { - ModuleBuilder thisModule = this; + RuntimeModuleBuilder thisModule = this; RuntimeModule refedRuntimeModule = GetRuntimeModuleFromModule(refedModule); return GetMemberRef(new QCallModule(ref thisModule), new QCallModule(ref refedRuntimeModule), tr, defToken); @@ -118,7 +117,7 @@ private int GetMemberRef(Module? refedModule, int tr, int defToken) private int GetMemberRefFromSignature(int tr, string methodName, byte[] signature, int length) { - ModuleBuilder thisModule = this; + RuntimeModuleBuilder thisModule = this; return GetMemberRefFromSignature(new QCallModule(ref thisModule), tr, methodName, signature, length); } @@ -129,7 +128,7 @@ private int GetMemberRefOfMethodInfo(int tr, RuntimeMethodInfo method) { Debug.Assert(method != null); - ModuleBuilder thisModule = this; + RuntimeModuleBuilder thisModule = this; int result = GetMemberRefOfMethodInfo(new QCallModule(ref thisModule), tr, ((IRuntimeMethodInfo)method).Value); GC.KeepAlive(method); return result; @@ -139,7 +138,7 @@ private int GetMemberRefOfMethodInfo(int tr, RuntimeConstructorInfo method) { Debug.Assert(method != null); - ModuleBuilder thisModule = this; + RuntimeModuleBuilder thisModule = this; int result = GetMemberRefOfMethodInfo(new QCallModule(ref thisModule), tr, ((IRuntimeMethodInfo)method).Value); GC.KeepAlive(method); return result; @@ -152,7 +151,7 @@ private int GetMemberRefOfFieldInfo(int tkType, RuntimeTypeHandle declaringType, { Debug.Assert(runtimeField != null); - ModuleBuilder thisModule = this; + RuntimeModuleBuilder thisModule = this; return GetMemberRefOfFieldInfo(new QCallModule(ref thisModule), tkType, new QCallTypeHandle(ref declaringType), runtimeField.MetadataToken); } @@ -161,7 +160,7 @@ private int GetMemberRefOfFieldInfo(int tkType, RuntimeTypeHandle declaringType, private int GetTokenFromTypeSpec(byte[] signature, int length) { - ModuleBuilder thisModule = this; + RuntimeModuleBuilder thisModule = this; return GetTokenFromTypeSpec(new QCallModule(ref thisModule), signature, length); } @@ -178,7 +177,7 @@ private int GetTokenFromTypeSpec(byte[] signature, int length) #region Internal Members - internal virtual Type? FindTypeBuilderWithName(string strTypeName, bool ignoreCase) + internal Type? FindTypeBuilderWithName(string strTypeName, bool ignoreCase) { if (ignoreCase) { @@ -217,47 +216,43 @@ private int GetTypeRefNested(Type type, Module? refedModule) Debug.Assert(!type.IsByRef, "Must not be ByRef. Get token from TypeSpec."); Debug.Assert(!type.IsGenericType || type.IsGenericTypeDefinition, "Must not have generic arguments."); - ModuleBuilder thisModule = this; + RuntimeModuleBuilder thisModule = this; RuntimeModule refedRuntimeModule = GetRuntimeModuleFromModule(refedModule); return GetTypeRef(new QCallModule(ref thisModule), typeName, new QCallModule(ref refedRuntimeModule), tkResolution); } - internal int InternalGetConstructorToken(ConstructorInfo con, bool usingRef) + public override int GetMethodMetadataToken(ConstructorInfo constructor) { - ArgumentNullException.ThrowIfNull(con); - - // Helper to get constructor token. If usingRef is true, we will never use the def token + // Helper to get constructor token. int tr; int mr; - if (con is ConstructorBuilder conBuilder) + if (constructor is ConstructorBuilder conBuilder) { - if (!usingRef && conBuilder.Module.Equals(this)) + if (conBuilder.Module.Equals(this)) return conBuilder.MetadataToken; // constructor is defined in a different module - tr = GetTypeTokenInternal(con.ReflectedType!); - mr = GetMemberRef(con.ReflectedType!.Module, tr, conBuilder.MetadataToken); + tr = GetTypeTokenInternal(constructor.ReflectedType!); + mr = GetMemberRef(constructor.ReflectedType!.Module, tr, conBuilder.MetadataToken); } - else if (con is ConstructorOnTypeBuilderInstantiation conOnTypeBuilderInst) + else if (constructor is ConstructorOnTypeBuilderInstantiation conOnTypeBuilderInst) { - if (usingRef) throw new InvalidOperationException(); - - tr = GetTypeTokenInternal(con.DeclaringType!); - mr = GetMemberRef(con.DeclaringType!.Module, tr, conOnTypeBuilderInst.MetadataToken); + tr = GetTypeTokenInternal(constructor.DeclaringType!); + mr = GetMemberRef(constructor.DeclaringType!.Module, tr, conOnTypeBuilderInst.MetadataToken); } - else if (con is RuntimeConstructorInfo rtCon && !con.ReflectedType!.IsArray) + else if (constructor is RuntimeConstructorInfo rtCon && !constructor.ReflectedType!.IsArray) { // constructor is not a dynamic field // We need to get the TypeRef tokens - tr = GetTypeTokenInternal(con.ReflectedType); + tr = GetTypeTokenInternal(constructor.ReflectedType); mr = GetMemberRefOfMethodInfo(tr, rtCon); } else { // some user derived ConstructorInfo // go through the slower code path, i.e. retrieve parameters and form signature helper. - ParameterInfo[] parameters = con.GetParameters(); + ParameterInfo[] parameters = constructor.GetParameters(); if (parameters == null) { throw new ArgumentException(SR.Argument_InvalidConstructorInfo); @@ -279,11 +274,11 @@ internal int InternalGetConstructorToken(ConstructorInfo con, bool usingRef) optionalCustomModifiers[i] = parameters[i].GetOptionalCustomModifiers(); } - tr = GetTypeTokenInternal(con.ReflectedType!); + tr = GetTypeTokenInternal(constructor.ReflectedType!); - SignatureHelper sigHelp = SignatureHelper.GetMethodSigHelper(this, con.CallingConvention, null, null, null, parameterTypes, requiredCustomModifiers, optionalCustomModifiers); + SignatureHelper sigHelp = SignatureHelper.GetMethodSigHelper(this, constructor.CallingConvention, null, null, null, parameterTypes, requiredCustomModifiers, optionalCustomModifiers); byte[] sigBytes = sigHelp.InternalGetSignature(out int length); - mr = GetMemberRefFromSignature(tr, con.Name, sigBytes, length); + mr = GetMemberRefFromSignature(tr, constructor.Name, sigBytes, length); } return mr; @@ -301,8 +296,7 @@ internal int InternalGetConstructorToken(ConstructorInfo con, bool usingRef) internal static RuntimeModule GetRuntimeModuleFromModule(Module? m) { - ModuleBuilder? mb = m as ModuleBuilder; - if (mb != null) + if (m is RuntimeModuleBuilder mb) { return mb.InternalModule; } @@ -364,15 +358,15 @@ private int GetMemberRefToken(MethodBase method, Type[]? optionalParameterTypes) else if (!method.Module.Equals(this)) { // Use typeRef as parent because the method's declaringType lives in a different assembly - tkParent = GetTypeToken(method.DeclaringType); + tkParent = GetTypeMetadataToken(method.DeclaringType); } else { // Use methodDef as parent because the method lives in this assembly and its declaringType has no generic arguments if (masmi != null) - tkParent = GetMethodToken(masmi); + tkParent = GetMethodMetadataToken(masmi); else - tkParent = GetConstructorToken((method as ConstructorInfo)!); + tkParent = GetMethodMetadataToken((method as ConstructorInfo)!); } return GetMemberRefFromSignature(tkParent, method.Name, sigBytes, sigLength); @@ -447,16 +441,16 @@ internal SignatureHelper GetMemberRefSignature(MethodBase? method, int cGenericP { switch (method) { - case MethodBuilder methodBuilder: + case RuntimeMethodBuilder methodBuilder: return methodBuilder.GetMethodSignature(); - case ConstructorBuilder constructorBuilder: + case RuntimeConstructorBuilder constructorBuilder: return constructorBuilder.GetMethodSignature(); - case MethodOnTypeBuilderInstantiation motbi when motbi.m_method is MethodBuilder methodBuilder: + case MethodOnTypeBuilderInstantiation motbi when motbi.m_method is RuntimeMethodBuilder methodBuilder: return methodBuilder.GetMethodSignature(); case MethodOnTypeBuilderInstantiation motbi: method = motbi.m_method; break; - case ConstructorOnTypeBuilderInstantiation cotbi when cotbi.m_ctor is ConstructorBuilder constructorBuilder: + case ConstructorOnTypeBuilderInstantiation cotbi when cotbi.m_ctor is RuntimeConstructorBuilder constructorBuilder: return constructorBuilder.GetMethodSignature(); case ConstructorOnTypeBuilderInstantiation cotbi: method = cotbi.m_ctor; @@ -530,13 +524,12 @@ internal Type[] GetTypesNoLock() foreach (Type builder in _typeBuilderDict.Values) { - EnumBuilder? enumBldr = builder as EnumBuilder; - TypeBuilder tmpTypeBldr; + RuntimeTypeBuilder tmpTypeBldr; - if (enumBldr != null) + if (builder is RuntimeEnumBuilder enumBldr) tmpTypeBldr = enumBldr.m_typeBuilder; else - tmpTypeBldr = (TypeBuilder)builder; + tmpTypeBldr = (RuntimeTypeBuilder)builder; // We should not return TypeBuilders. // Otherwise anyone can emit code in it. @@ -749,19 +742,11 @@ public override MethodInfo[] GetMethods(BindingFlags bindingFlags) #region Define Type - public TypeBuilder DefineType(string name, TypeAttributes attr, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type? parent, PackingSize packingSize, int typesize) + protected override TypeBuilder DefineTypeCore(string name, TypeAttributes attr, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type? parent, Type[]? interfaces, PackingSize packingSize, int typesize) { lock (SyncRoot) { - return new TypeBuilder(name, attr, parent, null, this, packingSize, typesize, null); - } - } - - public TypeBuilder DefineType(string name, TypeAttributes attr, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type? parent, Type[]? interfaces) - { - lock (SyncRoot) - { - return new TypeBuilder(name, attr, parent, interfaces, this, PackingSize.Unspecified, TypeBuilder.UnspecifiedTypeSize, null); + return new RuntimeTypeBuilder(name, attr, parent, interfaces, this, packingSize, typesize, null); } } @@ -771,11 +756,11 @@ public TypeBuilder DefineType(string name, TypeAttributes attr, [DynamicallyAcce // This API can only be used to construct a top-level (not nested) enum type. // Nested enum types can be defined manually using ModuleBuilder.DefineType. - public EnumBuilder DefineEnum(string name, TypeAttributes visibility, Type underlyingType) + protected override EnumBuilder DefineEnumCore(string name, TypeAttributes visibility, Type underlyingType) { lock (SyncRoot) { - EnumBuilder enumBuilder = new EnumBuilder(name, underlyingType, visibility, this); + RuntimeEnumBuilder enumBuilder = new RuntimeEnumBuilder(name, underlyingType, visibility, this); // This enum is not generic, nested, and cannot have any element type. // Replace the TypeBuilder object in _typeBuilderDict with this EnumBuilder object. @@ -790,9 +775,9 @@ public EnumBuilder DefineEnum(string name, TypeAttributes visibility, Type under #region Define Global Method [RequiresUnreferencedCode("P/Invoke marshalling may dynamically access members that could be trimmed.")] - public MethodBuilder DefinePInvokeMethod(string name, string dllName, string entryName, MethodAttributes attributes, - CallingConventions callingConvention, Type? returnType, Type[]? parameterTypes, CallingConvention nativeCallConv, - CharSet nativeCharSet) + protected override MethodBuilder DefinePInvokeMethodCore(string name, string dllName, string entryName, + MethodAttributes attributes, CallingConventions callingConvention, Type? returnType, + Type[]? parameterTypes, CallingConvention nativeCallConv, CharSet nativeCharSet) { lock (SyncRoot) { @@ -806,7 +791,7 @@ public EnumBuilder DefineEnum(string name, TypeAttributes visibility, Type under } } - public MethodBuilder DefineGlobalMethod(string name, MethodAttributes attributes, CallingConventions callingConvention, + protected override MethodBuilder DefineGlobalMethodCore(string name, MethodAttributes attributes, CallingConventions callingConvention, Type? returnType, Type[]? requiredReturnTypeCustomModifiers, Type[]? optionalReturnTypeCustomModifiers, Type[]? parameterTypes, Type[][]? requiredParameterTypeCustomModifiers, Type[][]? optionalParameterTypeCustomModifiers) { @@ -826,7 +811,7 @@ public EnumBuilder DefineEnum(string name, TypeAttributes visibility, Type under { throw new InvalidOperationException(SR.InvalidOperation_GlobalsHaveBeenCreated); } - ArgumentException.ThrowIfNullOrEmpty(name); + if ((attributes & MethodAttributes.Static) == 0) { throw new ArgumentException(SR.Argument_GlobalMembersMustBeStatic); @@ -837,74 +822,55 @@ public EnumBuilder DefineEnum(string name, TypeAttributes visibility, Type under parameterTypes, requiredParameterTypeCustomModifiers, optionalParameterTypeCustomModifiers); } - public void CreateGlobalFunctions() + protected override void CreateGlobalFunctionsCore() { lock (SyncRoot) { - CreateGlobalFunctionsNoLock(); - } - } - - private void CreateGlobalFunctionsNoLock() - { - if (_hasGlobalBeenCreated) - { - // cannot create globals twice - throw new InvalidOperationException(SR.InvalidOperation_NotADebugModule); + if (_hasGlobalBeenCreated) + { + // cannot create globals twice + throw new InvalidOperationException(SR.InvalidOperation_NotADebugModule); + } + _globalTypeBuilder.CreateType(); + _hasGlobalBeenCreated = true; } - _globalTypeBuilder.CreateTypeInfoImpl(); - _hasGlobalBeenCreated = true; } #endregion #region Define Data - public FieldBuilder DefineInitializedData(string name, byte[] data, FieldAttributes attributes) + protected override FieldBuilder DefineInitializedDataCore(string name, byte[] data, FieldAttributes attributes) { - // This method will define an initialized Data in .sdata. - // We will create a fake TypeDef to represent the data with size. This TypeDef - // will be the signature for the Field. - lock (SyncRoot) { - return DefineInitializedDataNoLock(name, data, attributes); - } - } + // This method will define an initialized Data in .sdata. + // We will create a fake TypeDef to represent the data with size. This TypeDef + // will be the signature for the Field. + if (_hasGlobalBeenCreated) + { + throw new InvalidOperationException(SR.InvalidOperation_GlobalsHaveBeenCreated); + } - private FieldBuilder DefineInitializedDataNoLock(string name, byte[] data, FieldAttributes attributes) - { - // This method will define an initialized Data in .sdata. - // We will create a fake TypeDef to represent the data with size. This TypeDef - // will be the signature for the Field. - if (_hasGlobalBeenCreated) - { - throw new InvalidOperationException(SR.InvalidOperation_GlobalsHaveBeenCreated); + return _globalTypeBuilder.DefineInitializedData(name, data, attributes); } - - return _globalTypeBuilder.DefineInitializedData(name, data, attributes); } - public FieldBuilder DefineUninitializedData(string name, int size, FieldAttributes attributes) + protected override FieldBuilder DefineUninitializedDataCore(string name, int size, FieldAttributes attributes) { lock (SyncRoot) { - return DefineUninitializedDataNoLock(name, size, attributes); - } - } + // This method will define an uninitialized Data in .sdata. + // We will create a fake TypeDef to represent the data with size. This TypeDef + // will be the signature for the Field. - private FieldBuilder DefineUninitializedDataNoLock(string name, int size, FieldAttributes attributes) - { - // This method will define an uninitialized Data in .sdata. - // We will create a fake TypeDef to represent the data with size. This TypeDef - // will be the signature for the Field. + if (_hasGlobalBeenCreated) + { + throw new InvalidOperationException(SR.InvalidOperation_GlobalsHaveBeenCreated); + } - if (_hasGlobalBeenCreated) - { - throw new InvalidOperationException(SR.InvalidOperation_GlobalsHaveBeenCreated); + return _globalTypeBuilder.DefineUninitializedData(name, size, attributes); } - - return _globalTypeBuilder.DefineUninitializedData(name, size, attributes); } #endregion @@ -915,12 +881,7 @@ private FieldBuilder DefineUninitializedDataNoLock(string name, int size, FieldA // 1. GetTypeToken // 2. ldtoken (see ILGenerator) // For all other occasions we should return the generic type instantiated on its formal parameters. - internal int GetTypeTokenInternal(Type type) - { - return GetTypeTokenInternal(type, getGenericDefinition: false); - } - - private int GetTypeTokenInternal(Type type, bool getGenericDefinition) + internal int GetTypeTokenInternal(Type type, bool getGenericDefinition = false) { lock (SyncRoot) { @@ -928,7 +889,7 @@ private int GetTypeTokenInternal(Type type, bool getGenericDefinition) } } - internal int GetTypeToken(Type type) + public override int GetTypeMetadataToken(Type type) { return GetTypeTokenInternal(type, getGenericDefinition: true); } @@ -962,10 +923,9 @@ private int GetTypeTokenWorkerNoLock(Type type, bool getGenericDefinition) if (refedModule.Equals(this)) { // no need to do anything additional other than defining the TypeRef Token - TypeBuilder? typeBuilder; + RuntimeTypeBuilder? typeBuilder; - EnumBuilder? enumBuilder = type as EnumBuilder; - typeBuilder = enumBuilder != null ? enumBuilder.m_typeBuilder : type as TypeBuilder; + typeBuilder = type is RuntimeEnumBuilder enumBuilder ? enumBuilder.m_typeBuilder : type as RuntimeTypeBuilder; if (typeBuilder != null) { @@ -983,7 +943,7 @@ private int GetTypeTokenWorkerNoLock(Type type, bool getGenericDefinition) return GetTypeRefNested(type, refedModule); } - internal int GetMethodToken(MethodInfo method) + public override int GetMethodMetadataToken(MethodInfo method) { lock (SyncRoot) { @@ -1018,7 +978,7 @@ private int GetMethodTokenNoLock(MethodInfo method, bool getGenericTypeDefinitio } // method is defined in a different module - tr = getGenericTypeDefinition ? GetTypeToken(method.DeclaringType) : GetTypeTokenInternal(method.DeclaringType); + tr = getGenericTypeDefinition ? GetTypeMetadataToken(method.DeclaringType) : GetTypeTokenInternal(method.DeclaringType); mr = GetMemberRef(method.DeclaringType.Module, tr, methodToken); } else if (method is MethodOnTypeBuilderInstantiation) @@ -1057,7 +1017,7 @@ private int GetMethodTokenNoLock(MethodInfo method, bool getGenericTypeDefinitio } else if (method is RuntimeMethodInfo rtMeth) { - tr = getGenericTypeDefinition ? GetTypeToken(declaringType) : GetTypeTokenInternal(declaringType); + tr = getGenericTypeDefinition ? GetTypeMetadataToken(declaringType) : GetTypeTokenInternal(declaringType); mr = GetMemberRefOfMethodInfo(tr, rtMeth); } else @@ -1077,7 +1037,7 @@ private int GetMethodTokenNoLock(MethodInfo method, bool getGenericTypeDefinitio optionalCustomModifiers[i] = parameters[i].GetOptionalCustomModifiers(); } - tr = getGenericTypeDefinition ? GetTypeToken(declaringType) : GetTypeTokenInternal(declaringType); + tr = getGenericTypeDefinition ? GetTypeMetadataToken(declaringType) : GetTypeTokenInternal(declaringType); SignatureHelper sigHelp; @@ -1128,7 +1088,7 @@ internal int GetMethodTokenInternal(MethodBase method, Type[]? optionalParameter } else { - tk = GetMethodToken(methodInfoUnbound); + tk = GetMethodMetadataToken(methodInfoUnbound); } // For Ldtoken, Ldftn, and Ldvirtftn, we should emit the method def/ref token for a generic method definition. @@ -1141,8 +1101,8 @@ internal int GetMethodTokenInternal(MethodBase method, Type[]? optionalParameter // Create MethodSepc M with parent G?.M byte[] sigBytes = SignatureHelper.GetMethodSpecSigHelper( this, methodInfo.GetGenericArguments()).InternalGetSignature(out int sigLength); - ModuleBuilder thisModule = this; - tk = TypeBuilder.DefineMethodSpec(new QCallModule(ref thisModule), tk, sigBytes, sigLength); + RuntimeModuleBuilder thisModule = this; + tk = RuntimeTypeBuilder.DefineMethodSpec(new QCallModule(ref thisModule), tk, sigBytes, sigLength); } else { @@ -1151,11 +1111,11 @@ internal int GetMethodTokenInternal(MethodBase method, Type[]? optionalParameter { if (methodInfo != null) { - tk = GetMethodToken(methodInfo); + tk = GetMethodMetadataToken(methodInfo); } else { - tk = GetConstructorToken((method as ConstructorInfo)!); + tk = GetMethodMetadataToken((method as ConstructorInfo)!); } } else @@ -1179,8 +1139,6 @@ internal int GetMethodTokenInternal(MethodBase method, Type[]? optionalParameter private int GetArrayMethodTokenNoLock(Type arrayClass, string methodName, CallingConventions callingConvention, Type? returnType, Type[]? parameterTypes) { - ArgumentNullException.ThrowIfNull(arrayClass); - ArgumentException.ThrowIfNullOrEmpty(methodName); if (!arrayClass.IsArray) { throw new ArgumentException(SR.Argument_HasToBeArrayClass); @@ -1193,12 +1151,12 @@ internal int GetMethodTokenInternal(MethodBase method, Type[]? optionalParameter byte[] sigBytes = sigHelp.InternalGetSignature(out int length); int typeSpec = GetTypeTokenInternal(arrayClass); - ModuleBuilder thisModule = this; + RuntimeModuleBuilder thisModule = this; return GetArrayMethodToken(new QCallModule(ref thisModule), typeSpec, methodName, sigBytes, length); } - public MethodInfo GetArrayMethod(Type arrayClass, string methodName, CallingConventions callingConvention, + protected override MethodInfo GetArrayMethodCore(Type arrayClass, string methodName, CallingConventions callingConvention, Type? returnType, Type[]? parameterTypes) { // GetArrayMethod is useful when you have an array of a type whose definition has not been completed and @@ -1211,13 +1169,7 @@ internal int GetMethodTokenInternal(MethodBase method, Type[]? optionalParameter return new SymbolMethod(this, token, arrayClass, methodName, callingConvention, returnType, parameterTypes); } - internal int GetConstructorToken(ConstructorInfo con) - { - // Return a token for the ConstructorInfo relative to the Module. - return InternalGetConstructorToken(con, false); - } - - internal int GetFieldToken(FieldInfo field) + public override int GetFieldMetadataToken(FieldInfo field) { lock (SyncRoot) { @@ -1300,26 +1252,26 @@ private int GetFieldTokenNoLock(FieldInfo field) return mr; } - internal int GetStringConstant(string str) + public override int GetStringMetadataToken(string stringConstant) { - ArgumentNullException.ThrowIfNull(str); + ArgumentNullException.ThrowIfNull(stringConstant); // Returns a token representing a String constant. If the string // value has already been defined, the existing token will be returned. - ModuleBuilder thisModule = this; - return GetStringConstant(new QCallModule(ref thisModule), str, str.Length); + RuntimeModuleBuilder thisModule = this; + return GetStringConstant(new QCallModule(ref thisModule), stringConstant, stringConstant.Length); } - internal int GetSignatureToken(SignatureHelper sigHelper) + public override int GetSignatureMetadataToken(SignatureHelper signature) { - ArgumentNullException.ThrowIfNull(sigHelper); + ArgumentNullException.ThrowIfNull(signature); // Define signature token given a signature helper. This will define a metadata // token for the signature described by SignatureHelper. // Get the signature in byte form. - byte[] sigBytes = sigHelper.InternalGetSignature(out int sigLength); - ModuleBuilder thisModule = this; - return TypeBuilder.GetTokenFromSig(new QCallModule(ref thisModule), sigBytes, sigLength); + byte[] sigBytes = signature.InternalGetSignature(out int sigLength); + RuntimeModuleBuilder thisModule = this; + return RuntimeTypeBuilder.GetTokenFromSig(new QCallModule(ref thisModule), sigBytes, sigLength); } internal int GetSignatureToken(byte[] sigBytes, int sigLength) @@ -1329,30 +1281,25 @@ internal int GetSignatureToken(byte[] sigBytes, int sigLength) byte[] localSigBytes = new byte[sigBytes.Length]; Buffer.BlockCopy(sigBytes, 0, localSigBytes, 0, sigBytes.Length); - ModuleBuilder thisModule = this; - return TypeBuilder.GetTokenFromSig(new QCallModule(ref thisModule), localSigBytes, sigLength); + RuntimeModuleBuilder thisModule = this; + return RuntimeTypeBuilder.GetTokenFromSig(new QCallModule(ref thisModule), localSigBytes, sigLength); } #endregion #region Other - public void SetCustomAttribute(ConstructorInfo con, byte[] binaryAttribute) + protected override void SetCustomAttributeCore(ConstructorInfo con, byte[] binaryAttribute) { - ArgumentNullException.ThrowIfNull(con); - ArgumentNullException.ThrowIfNull(binaryAttribute); - - TypeBuilder.DefineCustomAttribute( + RuntimeTypeBuilder.DefineCustomAttribute( this, 1, // This is hard coding the module token to 1 - GetConstructorToken(con), + GetMethodMetadataToken(con), binaryAttribute); } - public void SetCustomAttribute(CustomAttributeBuilder customBuilder) + protected override void SetCustomAttributeCore(CustomAttributeBuilder customBuilder) { - ArgumentNullException.ThrowIfNull(customBuilder); - customBuilder.CreateCustomAttribute(this, 1); // This is hard coding the module token to 1 } diff --git a/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/PropertyBuilder.cs b/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/RuntimePropertyBuilder.cs similarity index 85% rename from src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/PropertyBuilder.cs rename to src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/RuntimePropertyBuilder.cs index 323b6dd4c9371..cc3f25cf55599 100644 --- a/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/PropertyBuilder.cs +++ b/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/RuntimePropertyBuilder.cs @@ -21,17 +21,17 @@ namespace System.Reflection.Emit // A PropertyBuilder is always associated with a TypeBuilder. The TypeBuilder.DefineProperty // method will return a new PropertyBuilder to a client. // - public sealed class PropertyBuilder : PropertyInfo + internal sealed class RuntimePropertyBuilder : PropertyBuilder { // Constructs a PropertyBuilder. // - internal PropertyBuilder( - ModuleBuilder mod, // the module containing this PropertyBuilder + internal RuntimePropertyBuilder( + RuntimeModuleBuilder mod, // the module containing this PropertyBuilder string name, // property name PropertyAttributes attr, // property attribute such as DefaultProperty, Bindable, DisplayBind, etc Type returnType, // return type of the property. int prToken, // the metadata token for this property - TypeBuilder containingType) // the containing type + RuntimeTypeBuilder containingType) // the containing type { ArgumentException.ThrowIfNullOrEmpty(name); if (name[0] == '\0') @@ -48,11 +48,11 @@ public sealed class PropertyBuilder : PropertyInfo /// /// Set the default value of the Property /// - public void SetConstant(object? defaultValue) + protected override void SetConstantCore(object? defaultValue) { m_containingType.ThrowIfCreated(); - TypeBuilder.SetConstantValue( + RuntimeTypeBuilder.SetConstantValue( m_moduleBuilder, m_tkProperty, m_returnType, @@ -71,51 +71,46 @@ private void SetMethodSemantics(MethodBuilder mdBuilder, MethodSemanticsAttribut ArgumentNullException.ThrowIfNull(mdBuilder); m_containingType.ThrowIfCreated(); - ModuleBuilder module = m_moduleBuilder; - TypeBuilder.DefineMethodSemantics( + RuntimeModuleBuilder module = m_moduleBuilder; + RuntimeTypeBuilder.DefineMethodSemantics( new QCallModule(ref module), m_tkProperty, semantics, mdBuilder.MetadataToken); } - public void SetGetMethod(MethodBuilder mdBuilder) + protected override void SetGetMethodCore(MethodBuilder mdBuilder) { SetMethodSemantics(mdBuilder, MethodSemanticsAttributes.Getter); m_getMethod = mdBuilder; } - public void SetSetMethod(MethodBuilder mdBuilder) + protected override void SetSetMethodCore(MethodBuilder mdBuilder) { SetMethodSemantics(mdBuilder, MethodSemanticsAttributes.Setter); m_setMethod = mdBuilder; } - public void AddOtherMethod(MethodBuilder mdBuilder) + protected override void AddOtherMethodCore(MethodBuilder mdBuilder) { SetMethodSemantics(mdBuilder, MethodSemanticsAttributes.Other); } // Use this function if client decides to form the custom attribute blob themselves - public void SetCustomAttribute(ConstructorInfo con, byte[] binaryAttribute) + protected override void SetCustomAttributeCore(ConstructorInfo con, byte[] binaryAttribute) { - ArgumentNullException.ThrowIfNull(con); - ArgumentNullException.ThrowIfNull(binaryAttribute); - m_containingType.ThrowIfCreated(); - TypeBuilder.DefineCustomAttribute( + RuntimeTypeBuilder.DefineCustomAttribute( m_moduleBuilder, m_tkProperty, - m_moduleBuilder.GetConstructorToken(con), + m_moduleBuilder.GetMethodMetadataToken(con), binaryAttribute); } // Use this function if client wishes to build CustomAttribute using CustomAttributeBuilder - public void SetCustomAttribute(CustomAttributeBuilder customBuilder) + protected override void SetCustomAttributeCore(CustomAttributeBuilder customBuilder) { - ArgumentNullException.ThrowIfNull(customBuilder); - m_containingType.ThrowIfCreated(); customBuilder.CreateCustomAttribute(m_moduleBuilder, m_tkProperty); } @@ -209,11 +204,11 @@ public override bool IsDefined(Type attributeType, bool inherit) // These are package private so that TypeBuilder can access them. private string m_name; // The name of the property private int m_tkProperty; // The token of this property - private ModuleBuilder m_moduleBuilder; + private RuntimeModuleBuilder m_moduleBuilder; private PropertyAttributes m_attributes; // property's attribute flags private Type m_returnType; // property's return type private MethodInfo? m_getMethod; private MethodInfo? m_setMethod; - private TypeBuilder m_containingType; + private RuntimeTypeBuilder m_containingType; } } diff --git a/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilder.cs b/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeTypeBuilder.cs similarity index 82% rename from src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilder.cs rename to src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeTypeBuilder.cs index 088d22b5a1983..834818169e940 100644 --- a/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilder.cs +++ b/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeTypeBuilder.cs @@ -10,53 +10,8 @@ namespace System.Reflection.Emit { - public sealed partial class TypeBuilder : TypeInfo + public abstract partial class TypeBuilder { - public override bool IsAssignableFrom([NotNullWhen(true)] TypeInfo? typeInfo) - { - if (typeInfo == null) return false; - return IsAssignableFrom(typeInfo.AsType()); - } - - #region Declarations - private sealed class CustAttr - { - private readonly ConstructorInfo? m_con; - private readonly byte[]? m_binaryAttribute; - private readonly CustomAttributeBuilder? m_customBuilder; - - public CustAttr(ConstructorInfo con, byte[] binaryAttribute) - { - ArgumentNullException.ThrowIfNull(con); - ArgumentNullException.ThrowIfNull(binaryAttribute); - - m_con = con; - m_binaryAttribute = binaryAttribute; - } - - public CustAttr(CustomAttributeBuilder customBuilder) - { - ArgumentNullException.ThrowIfNull(customBuilder); - - m_customBuilder = customBuilder; - } - - public void Bake(ModuleBuilder module, int token) - { - if (m_customBuilder == null) - { - Debug.Assert(m_con != null); - DefineCustomAttribute(module, token, module.GetConstructorToken(m_con), - m_binaryAttribute); - } - else - { - m_customBuilder.CreateCustomAttribute(module, token); - } - } - } - #endregion - #region Public Static Methods [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2055:UnrecognizedReflectionPattern", Justification = "MakeGenericType is only called on a TypeBuilder which is not subject to trimming")] @@ -139,6 +94,54 @@ public static FieldInfo GetField(Type type, FieldInfo field) return FieldOnTypeBuilderInstantiation.GetField(field, typeBuilderInstantiation); } #endregion + } + + internal sealed partial class RuntimeTypeBuilder : TypeBuilder + { + public override bool IsAssignableFrom([NotNullWhen(true)] TypeInfo? typeInfo) + { + if (typeInfo == null) return false; + return IsAssignableFrom(typeInfo.AsType()); + } + + #region Declarations + private sealed class CustAttr + { + private readonly ConstructorInfo? m_con; + private readonly byte[]? m_binaryAttribute; + private readonly CustomAttributeBuilder? m_customBuilder; + + public CustAttr(ConstructorInfo con, byte[] binaryAttribute) + { + ArgumentNullException.ThrowIfNull(con); + ArgumentNullException.ThrowIfNull(binaryAttribute); + + m_con = con; + m_binaryAttribute = binaryAttribute; + } + + public CustAttr(CustomAttributeBuilder customBuilder) + { + ArgumentNullException.ThrowIfNull(customBuilder); + + m_customBuilder = customBuilder; + } + + public void Bake(RuntimeModuleBuilder module, int token) + { + if (m_customBuilder == null) + { + Debug.Assert(m_con != null); + DefineCustomAttribute(module, token, module.GetMethodMetadataToken(m_con), + m_binaryAttribute); + } + else + { + m_customBuilder.CreateCustomAttribute(module, token); + } + } + } + #endregion #region Private Static FCalls [LibraryImport(RuntimeHelpers.QCall, EntryPoint = "TypeBuilder_SetParentType")] @@ -172,7 +175,7 @@ public static FieldInfo GetField(Type type, FieldInfo field) private static partial void DefineCustomAttribute(QCallModule module, int tkAssociate, int tkConstructor, byte[]? attr, int attrLength); - internal static void DefineCustomAttribute(ModuleBuilder module, int tkAssociate, int tkConstructor, + internal static void DefineCustomAttribute(RuntimeModuleBuilder module, int tkAssociate, int tkConstructor, byte[]? attr) { byte[]? localAttr = null; @@ -231,15 +234,15 @@ internal static bool IsTypeEqual(Type? t1, Type? t2) // Maybe we are lucky that they are equal in the first place if (t1 == t2) return true; - TypeBuilder? tb1 = null; - TypeBuilder? tb2 = null; + RuntimeTypeBuilder? tb1 = null; + RuntimeTypeBuilder? tb2 = null; Type? runtimeType1; Type? runtimeType2; // set up the runtimeType and TypeBuilder type corresponding to t1 and t2 - if (t1 is TypeBuilder) + if (t1 is RuntimeTypeBuilder) { - tb1 = (TypeBuilder)t1; + tb1 = (RuntimeTypeBuilder)t1; // This will be null if it is not baked. runtimeType1 = tb1.m_bakedRuntimeType; } @@ -248,9 +251,9 @@ internal static bool IsTypeEqual(Type? t1, Type? t2) runtimeType1 = t1; } - if (t2 is TypeBuilder) + if (t2 is RuntimeTypeBuilder) { - tb2 = (TypeBuilder)t2; + tb2 = (RuntimeTypeBuilder)t2; // This will be null if it is not baked. runtimeType2 = tb2.m_bakedRuntimeType; } @@ -270,7 +273,7 @@ internal static bool IsTypeEqual(Type? t1, Type? t2) return false; } - internal static unsafe void SetConstantValue(ModuleBuilder module, int tk, Type destType, object? value) + internal static unsafe void SetConstantValue(RuntimeModuleBuilder module, int tk, Type destType, object? value) { // This is a helper function that is used by ParameterBuilder, PropertyBuilder, // and FieldBuilder to validate a default value and save it in the meta-data. @@ -303,7 +306,7 @@ internal static unsafe void SetConstantValue(ModuleBuilder module, int tk, Type // The above behaviors might not be the most consistent but we have to live with them. Type? underlyingType; - if (destType is EnumBuilder enumBldr) + if (destType is RuntimeEnumBuilder enumBldr) { underlyingType = enumBldr.GetEnumUnderlyingType(); @@ -312,7 +315,7 @@ internal static unsafe void SetConstantValue(ModuleBuilder module, int tk, Type if (type != enumBldr.m_typeBuilder.m_bakedRuntimeType && type != underlyingType) throw new ArgumentException(SR.Argument_ConstantDoesntMatch); } - else if (destType is TypeBuilder typeBldr) + else if (destType is RuntimeTypeBuilder typeBldr) { underlyingType = typeBldr.m_enumUnderlyingType; @@ -395,7 +398,7 @@ internal static unsafe void SetConstantValue(ModuleBuilder module, int tk, Type #region Private Data Members private List? m_ca; private int m_tdType; - private readonly ModuleBuilder m_module; + private readonly RuntimeModuleBuilder m_module; private readonly string? m_strName; private readonly string? m_strNameSpace; private string? m_strFullQualName; @@ -406,12 +409,12 @@ internal static unsafe void SetConstantValue(ModuleBuilder module, int tk, Type private List? m_typeInterfaces; private readonly TypeAttributes m_iAttr; private GenericParameterAttributes m_genParamAttributes; - internal List? m_listMethods; + internal List? m_listMethods; internal int m_lastTokenizedMethod; private int m_constructorCount; private readonly int m_iTypeSize; private readonly PackingSize m_iPackingSize; - private readonly TypeBuilder? m_DeclaringType; + private readonly RuntimeTypeBuilder? m_DeclaringType; // We cannot store this on EnumBuilder because users can define enum types manually using TypeBuilder. private Type? m_enumUnderlyingType; @@ -422,27 +425,27 @@ internal static unsafe void SetConstantValue(ModuleBuilder module, int tk, Type private RuntimeType m_bakedRuntimeType = null!; private readonly int m_genParamPos; - private GenericTypeParameterBuilder[]? m_inst; + private RuntimeGenericTypeParameterBuilder[]? m_inst; private readonly bool m_bIsGenParam; - private readonly MethodBuilder? m_declMeth; - private readonly TypeBuilder? m_genTypeDef; + private readonly RuntimeMethodBuilder? m_declMeth; + private readonly RuntimeTypeBuilder? m_genTypeDef; #endregion #region Constructor // ctor for the global (module) type - internal TypeBuilder(ModuleBuilder module) + internal RuntimeTypeBuilder(RuntimeModuleBuilder module) { m_tdType = ((int)MetadataTokenType.TypeDef); m_isHiddenGlobalType = true; m_module = module; - m_listMethods = new List(); + m_listMethods = new List(); // No token has been created so let's initialize it to -1 // The first time we call MethodBuilder.GetToken this will incremented. m_lastTokenizedMethod = -1; } // ctor for generic method parameter - internal TypeBuilder(string szName, int genParamPos, MethodBuilder declMeth) + internal RuntimeTypeBuilder(string szName, int genParamPos, RuntimeMethodBuilder declMeth) { m_strName = szName; m_genParamPos = genParamPos; @@ -456,7 +459,7 @@ internal TypeBuilder(string szName, int genParamPos, MethodBuilder declMeth) } // ctor for generic type parameter - private TypeBuilder(string szName, int genParamPos, TypeBuilder declType) + private RuntimeTypeBuilder(string szName, int genParamPos, RuntimeTypeBuilder declType) { m_strName = szName; m_genParamPos = genParamPos; @@ -468,9 +471,9 @@ private TypeBuilder(string szName, int genParamPos, TypeBuilder declType) m_module = declType.GetModuleBuilder(); } - internal TypeBuilder( - string fullname, TypeAttributes attr, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type? parent, Type[]? interfaces, ModuleBuilder module, - PackingSize iPackingSize, int iTypeSize, TypeBuilder? enclosingType) + internal RuntimeTypeBuilder( + string fullname, TypeAttributes attr, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type? parent, Type[]? interfaces, RuntimeModuleBuilder module, + PackingSize iPackingSize, int iTypeSize, RuntimeTypeBuilder? enclosingType) { ArgumentException.ThrowIfNullOrEmpty(fullname); @@ -483,7 +486,7 @@ private TypeBuilder(string szName, int genParamPos, TypeBuilder declType) int i; m_module = module; m_DeclaringType = enclosingType; - AssemblyBuilder containingAssem = m_module.ContainingAssemblyBuilder; + RuntimeAssemblyBuilder containingAssem = m_module.ContainingAssemblyBuilder; // cannot have two types within the same assembly of the same name containingAssem.CheckTypeNameConflict(fullname, enclosingType); @@ -531,7 +534,7 @@ private TypeBuilder(string szName, int genParamPos, TypeBuilder declType) SetParent(parent); - m_listMethods = new List(); + m_listMethods = new List(); m_lastTokenizedMethod = -1; SetInterfaces(interfaces); @@ -592,7 +595,7 @@ private FieldBuilder DefineDataHelper(string name, byte[]? data, int size, Field fdBuilder = DefineField(name, valueClassType, attributes | FieldAttributes.Static); // now we need to set the RVA - fdBuilder.SetData(data, size); + ((RuntimeFieldBuilder)fdBuilder).SetData(data, size); return fdBuilder; } @@ -629,7 +632,7 @@ private void VerifyTypeAttributes(TypeAttributes attr) } } - public bool IsCreated() + protected override bool IsCreatedCore() { return m_hasBeenCreated; } @@ -657,7 +660,7 @@ internal void ThrowIfCreated() internal object SyncRoot => m_module.SyncRoot; - internal ModuleBuilder GetModuleBuilder() + internal RuntimeModuleBuilder GetModuleBuilder() { return m_module; } @@ -691,7 +694,7 @@ internal void SetGenParamCustomAttribute(CustomAttributeBuilder customBuilder) private void SetGenParamCustomAttributeNoLock(CustAttr ca) { - m_ca ??= new List(); + m_ca ??= new List(); m_ca.Add(ca); } #endregion @@ -940,7 +943,7 @@ public override bool IsAssignableFrom([NotNullWhen(true)] Type? c) return true; Type? fromRuntimeType; - TypeBuilder? fromTypeBuilder = c as TypeBuilder; + RuntimeTypeBuilder? fromTypeBuilder = c as RuntimeTypeBuilder; if (fromTypeBuilder != null) fromRuntimeType = fromTypeBuilder.m_bakedRuntimeType; @@ -1150,21 +1153,17 @@ internal void SetInterfaces(params Type[]? interfaces) } } - public GenericTypeParameterBuilder[] DefineGenericParameters(params string[] names) + protected override GenericTypeParameterBuilder[] DefineGenericParametersCore(params string[] names) { - ArgumentNullException.ThrowIfNull(names); - if (names.Length == 0) - throw new ArgumentException(SR.Arg_EmptyArray, nameof(names)); - for (int i = 0; i < names.Length; i++) ArgumentNullException.ThrowIfNull(names[i], nameof(names)); if (m_inst != null) throw new InvalidOperationException(); - m_inst = new GenericTypeParameterBuilder[names.Length]; + m_inst = new RuntimeGenericTypeParameterBuilder[names.Length]; for (int i = 0; i < names.Length; i++) - m_inst[i] = new GenericTypeParameterBuilder(new TypeBuilder(names[i], i, this)); + m_inst[i] = new RuntimeGenericTypeParameterBuilder(new RuntimeTypeBuilder(names[i], i, this)); return m_inst; } @@ -1191,86 +1190,58 @@ public override Type MakeGenericType(params Type[] typeArguments) #endregion #region Define Method - public void DefineMethodOverride(MethodInfo methodInfoBody, MethodInfo methodInfoDeclaration) + protected override void DefineMethodOverrideCore(MethodInfo methodInfoBody, MethodInfo methodInfoDeclaration) { lock (SyncRoot) { - DefineMethodOverrideNoLock(methodInfoBody, methodInfoDeclaration); - } - } - - private void DefineMethodOverrideNoLock(MethodInfo methodInfoBody, MethodInfo methodInfoDeclaration) - { - ArgumentNullException.ThrowIfNull(methodInfoBody); - ArgumentNullException.ThrowIfNull(methodInfoDeclaration); - - ThrowIfCreated(); - - if (!ReferenceEquals(methodInfoBody.DeclaringType, this)) - // Loader restriction: body method has to be from this class - throw new ArgumentException(SR.ArgumentException_BadMethodImplBody); + ThrowIfCreated(); - int tkBody = m_module.GetMethodToken(methodInfoBody); - int tkDecl = m_module.GetMethodToken(methodInfoDeclaration); + if (!ReferenceEquals(methodInfoBody.DeclaringType, this)) + // Loader restriction: body method has to be from this class + throw new ArgumentException(SR.ArgumentException_BadMethodImplBody); - ModuleBuilder module = m_module; - DefineMethodImpl(new QCallModule(ref module), m_tdType, tkBody, tkDecl); - } + int tkBody = m_module.GetMethodMetadataToken(methodInfoBody); + int tkDecl = m_module.GetMethodMetadataToken(methodInfoDeclaration); - public MethodBuilder DefineMethod(string name, MethodAttributes attributes, CallingConventions callingConvention, - Type? returnType, Type[]? returnTypeRequiredCustomModifiers, Type[]? returnTypeOptionalCustomModifiers, - Type[]? parameterTypes, Type[][]? parameterTypeRequiredCustomModifiers, Type[][]? parameterTypeOptionalCustomModifiers) - { - lock (SyncRoot) - { - return DefineMethodNoLock(name, attributes, callingConvention, returnType, returnTypeRequiredCustomModifiers, - returnTypeOptionalCustomModifiers, parameterTypes, parameterTypeRequiredCustomModifiers, - parameterTypeOptionalCustomModifiers); + RuntimeModuleBuilder module = m_module; + DefineMethodImpl(new QCallModule(ref module), m_tdType, tkBody, tkDecl); } } [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2082:UnrecognizedReflectionPattern", Justification = "Reflection.Emit is not subject to trimming")] - private MethodBuilder DefineMethodNoLock(string name, MethodAttributes attributes, CallingConventions callingConvention, + protected override MethodBuilder DefineMethodCore(string name, MethodAttributes attributes, CallingConventions callingConvention, Type? returnType, Type[]? returnTypeRequiredCustomModifiers, Type[]? returnTypeOptionalCustomModifiers, Type[]? parameterTypes, Type[][]? parameterTypeRequiredCustomModifiers, Type[][]? parameterTypeOptionalCustomModifiers) { - ArgumentException.ThrowIfNullOrEmpty(name); - - if (parameterTypes != null) + lock (SyncRoot) { - if (parameterTypeOptionalCustomModifiers != null && parameterTypeOptionalCustomModifiers.Length != parameterTypes.Length) - throw new ArgumentException(SR.Format(SR.Argument_MismatchedArrays, nameof(parameterTypeOptionalCustomModifiers), nameof(parameterTypes))); - - if (parameterTypeRequiredCustomModifiers != null && parameterTypeRequiredCustomModifiers.Length != parameterTypes.Length) - throw new ArgumentException(SR.Format(SR.Argument_MismatchedArrays, nameof(parameterTypeRequiredCustomModifiers), nameof(parameterTypes))); - } - - ThrowIfCreated(); + ThrowIfCreated(); - // pass in Method attributes - MethodBuilder method = new MethodBuilder( - name, attributes, callingConvention, - returnType, returnTypeRequiredCustomModifiers, returnTypeOptionalCustomModifiers, - parameterTypes, parameterTypeRequiredCustomModifiers, parameterTypeOptionalCustomModifiers, - m_module, this); + // pass in Method attributes + RuntimeMethodBuilder method = new RuntimeMethodBuilder( + name, attributes, callingConvention, + returnType, returnTypeRequiredCustomModifiers, returnTypeOptionalCustomModifiers, + parameterTypes, parameterTypeRequiredCustomModifiers, parameterTypeOptionalCustomModifiers, + m_module, this); - if (!m_isHiddenGlobalType) - { - // If this method is declared to be a constructor, increment our constructor count. - if ((method.Attributes & MethodAttributes.SpecialName) != 0 && method.Name.Equals(ConstructorInfo.ConstructorName)) + if (!m_isHiddenGlobalType) { - m_constructorCount++; + // If this method is declared to be a constructor, increment our constructor count. + if ((method.Attributes & MethodAttributes.SpecialName) != 0 && method.Name.Equals(ConstructorInfo.ConstructorName)) + { + m_constructorCount++; + } } - } - m_listMethods!.Add(method); + m_listMethods!.Add(method); - return method; + return method; + } } [RequiresUnreferencedCode("P/Invoke marshalling may dynamically access members that could be trimmed.")] - public MethodBuilder DefinePInvokeMethod(string name, string dllName, string entryName, MethodAttributes attributes, + protected override MethodBuilder DefinePInvokeMethodCore(string name, string dllName, string entryName, MethodAttributes attributes, CallingConventions callingConvention, Type? returnType, Type[]? returnTypeRequiredCustomModifiers, Type[]? returnTypeOptionalCustomModifiers, Type[]? parameterTypes, Type[][]? parameterTypeRequiredCustomModifiers, Type[][]? parameterTypeOptionalCustomModifiers, @@ -1278,10 +1249,6 @@ private void DefineMethodOverrideNoLock(MethodInfo methodInfoBody, MethodInfo me { lock (SyncRoot) { - ArgumentException.ThrowIfNullOrEmpty(name); - ArgumentException.ThrowIfNullOrEmpty(dllName); - ArgumentException.ThrowIfNullOrEmpty(entryName); - if ((attributes & MethodAttributes.Abstract) != 0) throw new ArgumentException(SR.Argument_BadPInvokeMethod); @@ -1291,7 +1258,7 @@ private void DefineMethodOverrideNoLock(MethodInfo methodInfoBody, MethodInfo me ThrowIfCreated(); attributes |= MethodAttributes.PinvokeImpl; - MethodBuilder method = new MethodBuilder(name, attributes, callingConvention, + RuntimeMethodBuilder method = new RuntimeMethodBuilder(name, attributes, callingConvention, returnType, returnTypeRequiredCustomModifiers, returnTypeOptionalCustomModifiers, parameterTypes, parameterTypeRequiredCustomModifiers, parameterTypeOptionalCustomModifiers, m_module, this); @@ -1343,7 +1310,7 @@ private void DefineMethodOverrideNoLock(MethodInfo methodInfoBody, MethodInfo me break; } - ModuleBuilder module = m_module; + RuntimeModuleBuilder module = m_module; SetPInvokeData(new QCallModule(ref module), dllName, entryName, @@ -1358,30 +1325,25 @@ private void DefineMethodOverrideNoLock(MethodInfo methodInfoBody, MethodInfo me #endregion #region Define Constructor - public ConstructorBuilder DefineTypeInitializer() - { - lock (SyncRoot) - { - return DefineTypeInitializerNoLock(); - } - } - [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2082:UnrecognizedReflectionPattern", Justification = "Reflection.Emit is not subject to trimming")] - private ConstructorBuilder DefineTypeInitializerNoLock() + protected override ConstructorBuilder DefineTypeInitializerCore() { - ThrowIfCreated(); + lock (SyncRoot) + { + ThrowIfCreated(); - // change the attributes and the class constructor's name - const MethodAttributes attr = MethodAttributes.Private | MethodAttributes.Static | MethodAttributes.SpecialName; + // change the attributes and the class constructor's name + const MethodAttributes attr = MethodAttributes.Private | MethodAttributes.Static | MethodAttributes.SpecialName; - ConstructorBuilder constBuilder = new ConstructorBuilder( - ConstructorInfo.TypeConstructorName, attr, CallingConventions.Standard, null, m_module, this); + ConstructorBuilder constBuilder = new RuntimeConstructorBuilder( + ConstructorInfo.TypeConstructorName, attr, CallingConventions.Standard, null, m_module, this); - return constBuilder; + return constBuilder; + } } - public ConstructorBuilder DefineDefaultConstructor(MethodAttributes attributes) + protected override ConstructorBuilder DefineDefaultConstructorCore(MethodAttributes attributes) { if ((m_iAttr & TypeAttributes.Interface) == TypeAttributes.Interface) { @@ -1400,7 +1362,7 @@ public ConstructorBuilder DefineDefaultConstructor(MethodAttributes attributes) Justification = "GetConstructor is only called on a TypeBuilderInstantiation which is not subject to trimming")] private ConstructorBuilder DefineDefaultConstructorNoLock(MethodAttributes attributes) { - ConstructorBuilder constBuilder; + RuntimeConstructorBuilder constBuilder; // get the parent class's default constructor // We really don't want(BindingFlags.Instance|BindingFlags.Public|BindingFlags.NonPublic) here. We really want @@ -1414,8 +1376,8 @@ private ConstructorBuilder DefineDefaultConstructorNoLock(MethodAttributes attri { Type? genericTypeDefinition = m_typeParent.GetGenericTypeDefinition(); - if (genericTypeDefinition is TypeBuilder) - genericTypeDefinition = ((TypeBuilder)genericTypeDefinition).m_bakedRuntimeType; + if (genericTypeDefinition is RuntimeTypeBuilder rtBuilder) + genericTypeDefinition = rtBuilder.m_bakedRuntimeType; if (genericTypeDefinition == null) throw new NotSupportedException(SR.NotSupported_DynamicModule); @@ -1436,7 +1398,7 @@ private ConstructorBuilder DefineDefaultConstructorNoLock(MethodAttributes attri throw new NotSupportedException(SR.NotSupported_NoParentDefaultConstructor); // Define the constructor Builder - constBuilder = DefineConstructor(attributes, CallingConventions.Standard, null); + constBuilder = (RuntimeConstructorBuilder)DefineConstructor(attributes, CallingConventions.Standard, null); m_constructorCount++; // generate the code to call the parent's default constructor @@ -1449,7 +1411,7 @@ private ConstructorBuilder DefineDefaultConstructorNoLock(MethodAttributes attri return constBuilder; } - public ConstructorBuilder DefineConstructor(MethodAttributes attributes, CallingConventions callingConvention, + protected override ConstructorBuilder DefineConstructorCore(MethodAttributes attributes, CallingConventions callingConvention, Type[]? parameterTypes, Type[][]? requiredCustomModifiers, Type[][]? optionalCustomModifiers) { if ((m_iAttr & TypeAttributes.Interface) == TypeAttributes.Interface && (attributes & MethodAttributes.Static) != MethodAttributes.Static) @@ -1484,7 +1446,7 @@ private ConstructorBuilder DefineDefaultConstructorNoLock(MethodAttributes attri attributes |= MethodAttributes.SpecialName; ConstructorBuilder constBuilder = - new ConstructorBuilder(name, attributes, callingConvention, + new RuntimeConstructorBuilder(name, attributes, callingConvention, parameterTypes, requiredCustomModifiers, optionalCustomModifiers, m_module, this); m_constructorCount++; @@ -1495,181 +1457,133 @@ private ConstructorBuilder DefineDefaultConstructorNoLock(MethodAttributes attri #endregion #region Define Nested Type - - public TypeBuilder DefineNestedType(string name, TypeAttributes attr, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type? parent, Type[]? interfaces) + protected override TypeBuilder DefineNestedTypeCore(string name, TypeAttributes attr, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type? parent, Type[]? interfaces, PackingSize packSize, int typeSize) { lock (SyncRoot) { - return new TypeBuilder(name, attr, parent, interfaces, m_module, PackingSize.Unspecified, UnspecifiedTypeSize, this); - } - } - - public TypeBuilder DefineNestedType(string name, TypeAttributes attr, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type? parent, PackingSize packSize, int typeSize) - { - lock (SyncRoot) - { - return new TypeBuilder(name, attr, parent, null, m_module, packSize, typeSize, this); + return new RuntimeTypeBuilder(name, attr, parent, interfaces, m_module, packSize, typeSize, this); } } #endregion #region Define Field - - public FieldBuilder DefineField(string fieldName, Type type, Type[]? requiredCustomModifiers, + protected override FieldBuilder DefineFieldCore(string fieldName, Type type, Type[]? requiredCustomModifiers, Type[]? optionalCustomModifiers, FieldAttributes attributes) { lock (SyncRoot) { - return DefineFieldNoLock(fieldName, type, requiredCustomModifiers, optionalCustomModifiers, attributes); - } - } - - private FieldBuilder DefineFieldNoLock(string fieldName, Type type, Type[]? requiredCustomModifiers, - Type[]? optionalCustomModifiers, FieldAttributes attributes) - { - ThrowIfCreated(); + ThrowIfCreated(); - if (m_enumUnderlyingType == null && IsEnum) - { - if ((attributes & FieldAttributes.Static) == 0) + if (m_enumUnderlyingType == null && IsEnum) { - // remember the underlying type for enum type - m_enumUnderlyingType = type; + if ((attributes & FieldAttributes.Static) == 0) + { + // remember the underlying type for enum type + m_enumUnderlyingType = type; + } } - } - return new FieldBuilder(this, fieldName, type, requiredCustomModifiers, optionalCustomModifiers, attributes); + return new RuntimeFieldBuilder(this, fieldName, type, requiredCustomModifiers, optionalCustomModifiers, attributes); + } } - public FieldBuilder DefineInitializedData(string name, byte[] data, FieldAttributes attributes) + protected override FieldBuilder DefineInitializedDataCore(string name, byte[] data, FieldAttributes attributes) { lock (SyncRoot) { - return DefineInitializedDataNoLock(name, data, attributes); - } - } - - private FieldBuilder DefineInitializedDataNoLock(string name, byte[] data, FieldAttributes attributes) - { - ArgumentNullException.ThrowIfNull(data); - - // This method will define an initialized Data in .sdata. - // We will create a fake TypeDef to represent the data with size. This TypeDef - // will be the signature for the Field. + // This method will define an initialized Data in .sdata. + // We will create a fake TypeDef to represent the data with size. This TypeDef + // will be the signature for the Field. - return DefineDataHelper(name, data, data.Length, attributes); + return DefineDataHelper(name, data, data.Length, attributes); + } } - public FieldBuilder DefineUninitializedData(string name, int size, FieldAttributes attributes) + protected override FieldBuilder DefineUninitializedDataCore(string name, int size, FieldAttributes attributes) { lock (SyncRoot) { - return DefineUninitializedDataNoLock(name, size, attributes); + // This method will define an uninitialized Data in .sdata. + // We will create a fake TypeDef to represent the data with size. This TypeDef + // will be the signature for the Field. + return DefineDataHelper(name, null, size, attributes); } } - private FieldBuilder DefineUninitializedDataNoLock(string name, int size, FieldAttributes attributes) - { - // This method will define an uninitialized Data in .sdata. - // We will create a fake TypeDef to represent the data with size. This TypeDef - // will be the signature for the Field. - return DefineDataHelper(name, null, size, attributes); - } - #endregion #region Define Properties and Events - public PropertyBuilder DefineProperty(string name, PropertyAttributes attributes, CallingConventions callingConvention, + protected override PropertyBuilder DefinePropertyCore(string name, PropertyAttributes attributes, CallingConventions callingConvention, Type returnType, Type[]? returnTypeRequiredCustomModifiers, Type[]? returnTypeOptionalCustomModifiers, Type[]? parameterTypes, Type[][]? parameterTypeRequiredCustomModifiers, Type[][]? parameterTypeOptionalCustomModifiers) { lock (SyncRoot) { - return DefinePropertyNoLock(name, attributes, callingConvention, returnType, returnTypeRequiredCustomModifiers, returnTypeOptionalCustomModifiers, - parameterTypes, parameterTypeRequiredCustomModifiers, parameterTypeOptionalCustomModifiers); - } - } - - private PropertyBuilder DefinePropertyNoLock(string name, PropertyAttributes attributes, CallingConventions callingConvention, - Type returnType, Type[]? returnTypeRequiredCustomModifiers, Type[]? returnTypeOptionalCustomModifiers, - Type[]? parameterTypes, Type[][]? parameterTypeRequiredCustomModifiers, Type[][]? parameterTypeOptionalCustomModifiers) - { - ArgumentException.ThrowIfNullOrEmpty(name); - - SignatureHelper sigHelper; - byte[] sigBytes; + SignatureHelper sigHelper; + byte[] sigBytes; - ThrowIfCreated(); - - // get the signature in SignatureHelper form - sigHelper = SignatureHelper.GetPropertySigHelper( - m_module, callingConvention, - returnType, returnTypeRequiredCustomModifiers, returnTypeOptionalCustomModifiers, - parameterTypes, parameterTypeRequiredCustomModifiers, parameterTypeOptionalCustomModifiers); + ThrowIfCreated(); - // get the signature in byte form - sigBytes = sigHelper.InternalGetSignature(out int sigLength); + // get the signature in SignatureHelper form + sigHelper = SignatureHelper.GetPropertySigHelper( + m_module, callingConvention, + returnType, returnTypeRequiredCustomModifiers, returnTypeOptionalCustomModifiers, + parameterTypes, parameterTypeRequiredCustomModifiers, parameterTypeOptionalCustomModifiers); - ModuleBuilder module = m_module; + // get the signature in byte form + sigBytes = sigHelper.InternalGetSignature(out int sigLength); - int prToken = DefineProperty( - new QCallModule(ref module), - m_tdType, - name, - attributes, - sigBytes, - sigLength); + RuntimeModuleBuilder module = m_module; - // create the property builder now. - return new PropertyBuilder( - m_module, + int prToken = DefineProperty( + new QCallModule(ref module), + m_tdType, name, attributes, - returnType, - prToken, - this); - } + sigBytes, + sigLength); - public EventBuilder DefineEvent(string name, EventAttributes attributes, Type eventtype) - { - lock (SyncRoot) - { - return DefineEventNoLock(name, attributes, eventtype); + // create the property builder now. + return new RuntimePropertyBuilder( + m_module, + name, + attributes, + returnType, + prToken, + this); } } - private EventBuilder DefineEventNoLock(string name, EventAttributes attributes, Type eventtype) + protected override EventBuilder DefineEventCore(string name, EventAttributes attributes, Type eventtype) { - ArgumentException.ThrowIfNullOrEmpty(name); - if (name[0] == '\0') - throw new ArgumentException(SR.Argument_IllegalName, nameof(name)); - - int tkType; - int evToken; - - ThrowIfCreated(); + lock (SyncRoot) + { + int tkType; + int evToken; - tkType = m_module.GetTypeTokenInternal(eventtype); + ThrowIfCreated(); - // Internal helpers to define property records - ModuleBuilder module = m_module; - evToken = DefineEvent( - new QCallModule(ref module), - m_tdType, - name, - attributes, - tkType); + tkType = m_module.GetTypeTokenInternal(eventtype); - // create the property builder now. - return new EventBuilder( - m_module, + // Internal helpers to define property records + RuntimeModuleBuilder module = m_module; + evToken = DefineEvent( + new QCallModule(ref module), + m_tdType, name, attributes, - // tkType, - this, - evToken); + tkType); + + // create the property builder now. + return new RuntimeEventBuilder( + m_module, + name, + attributes, + this, + evToken); + } } #endregion @@ -1677,19 +1591,11 @@ private EventBuilder DefineEventNoLock(string name, EventAttributes attributes, #region Create Type [return: DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] - public TypeInfo CreateTypeInfo() + protected override TypeInfo CreateTypeInfoCore() { TypeInfo? typeInfo = CreateTypeInfoImpl(); - Debug.Assert(typeInfo != null); - return typeInfo; - } - - [return: DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] - public Type CreateType() - { - Type? type = CreateTypeInfoImpl(); - Debug.Assert(type != null); - return type; + Debug.Assert(m_isHiddenGlobalType || typeInfo != null); + return typeInfo!; } [return: DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] @@ -1727,7 +1633,7 @@ public Type CreateType() tkParent = m_module.GetTypeTokenInternal(m_typeParent); } - ModuleBuilder module = m_module; + RuntimeModuleBuilder module = m_module; if (IsGenericParameter) { @@ -1774,7 +1680,7 @@ public Type CreateType() if (m_inst != null) { - foreach (GenericTypeParameterBuilder tb in m_inst) + foreach (RuntimeGenericTypeParameterBuilder tb in m_inst) { tb.m_type.CreateType(); } @@ -1795,7 +1701,7 @@ public Type CreateType() for (int i = 0; i < size; i++) { - MethodBuilder meth = m_listMethods[i]; + RuntimeMethodBuilder meth = m_listMethods[i]; if (meth.IsGenericMethodDefinition) { @@ -1895,11 +1801,11 @@ public Type CreateType() #endregion #region Misc - public int Size => m_iTypeSize; + protected override int SizeCore => m_iTypeSize; - public PackingSize PackingSize => m_iPackingSize; + protected override PackingSize PackingSizeCore => m_iPackingSize; - public void SetParent([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type? parent) + protected override void SetParentCore([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type? parent) { ThrowIfCreated(); @@ -1927,14 +1833,12 @@ public void SetParent([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes } } - public void AddInterfaceImplementation([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type interfaceType) + protected override void AddInterfaceImplementationCore([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type interfaceType) { - ArgumentNullException.ThrowIfNull(interfaceType); - ThrowIfCreated(); int tkInterface = m_module.GetTypeTokenInternal(interfaceType); - ModuleBuilder module = m_module; + RuntimeModuleBuilder module = m_module; AddInterfaceImpl(new QCallModule(ref module), m_tdType, tkInterface); m_typeInterfaces!.Add(interfaceType); @@ -1951,19 +1855,14 @@ internal int TypeToken } } - public void SetCustomAttribute(ConstructorInfo con, byte[] binaryAttribute) + protected override void SetCustomAttributeCore(ConstructorInfo con, byte[] binaryAttribute) { - ArgumentNullException.ThrowIfNull(con); - ArgumentNullException.ThrowIfNull(binaryAttribute); - - DefineCustomAttribute(m_module, m_tdType, ((ModuleBuilder)m_module).GetConstructorToken(con), binaryAttribute); + DefineCustomAttribute(m_module, m_tdType, m_module.GetMethodMetadataToken(con), binaryAttribute); } - public void SetCustomAttribute(CustomAttributeBuilder customBuilder) + protected override void SetCustomAttributeCore(CustomAttributeBuilder customBuilder) { - ArgumentNullException.ThrowIfNull(customBuilder); - - customBuilder.CreateCustomAttribute((ModuleBuilder)m_module, m_tdType); + customBuilder.CreateCustomAttribute(m_module, m_tdType); } #endregion diff --git a/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/SignatureHelper.cs b/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/SignatureHelper.cs index fe5506f7b7653..0e730bf25fbd4 100644 --- a/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/SignatureHelper.cs +++ b/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/SignatureHelper.cs @@ -289,7 +289,7 @@ private void AddOneArgTypeHelper(Type clsArgument, Type[]? requiredCustomModifie AddElementType(CorElementType.ELEMENT_TYPE_CMOD_OPT); - int token = m_module!.GetTypeToken(t); + int token = m_module!.GetTypeMetadataToken(t); Debug.Assert(!MetadataToken.IsNullToken(token)); AddToken(token); } @@ -311,7 +311,7 @@ private void AddOneArgTypeHelper(Type clsArgument, Type[]? requiredCustomModifie AddElementType(CorElementType.ELEMENT_TYPE_CMOD_REQD); - int token = m_module!.GetTypeToken(t); + int token = m_module!.GetTypeMetadataToken(t); Debug.Assert(!MetadataToken.IsNullToken(token)); AddToken(token); } @@ -345,9 +345,8 @@ private void AddOneArgTypeHelperWorker(Type clsArgument, bool lastWasGenericInst foreach (Type t in args) AddOneArgTypeHelper(t); } - else if (clsArgument is TypeBuilder) + else if (clsArgument is RuntimeTypeBuilder clsBuilder) { - TypeBuilder clsBuilder = (TypeBuilder)clsArgument; int tkType; if (clsBuilder.Module.Equals(m_module)) @@ -356,7 +355,7 @@ private void AddOneArgTypeHelperWorker(Type clsArgument, bool lastWasGenericInst } else { - tkType = m_module!.GetTypeToken(clsArgument); + tkType = m_module!.GetTypeMetadataToken(clsArgument); } if (clsArgument.IsValueType) @@ -368,18 +367,18 @@ private void AddOneArgTypeHelperWorker(Type clsArgument, bool lastWasGenericInst InternalAddTypeToken(tkType, CorElementType.ELEMENT_TYPE_CLASS); } } - else if (clsArgument is EnumBuilder) + else if (clsArgument is RuntimeEnumBuilder reBuilder) { - TypeBuilder clsBuilder = ((EnumBuilder)clsArgument).m_typeBuilder; + RuntimeTypeBuilder rtBuilder = reBuilder.m_typeBuilder; int tkType; - if (clsBuilder.Module.Equals(m_module)) + if (rtBuilder.Module.Equals(m_module)) { - tkType = clsBuilder.TypeToken; + tkType = rtBuilder.TypeToken; } else { - tkType = m_module!.GetTypeToken(clsArgument); + tkType = m_module!.GetTypeMetadataToken(clsArgument); } if (clsArgument.IsValueType) @@ -453,11 +452,11 @@ private void AddOneArgTypeHelperWorker(Type clsArgument, bool lastWasGenericInst } else if (clsArgument.IsValueType) { - InternalAddTypeToken(m_module.GetTypeToken(clsArgument), CorElementType.ELEMENT_TYPE_VALUETYPE); + InternalAddTypeToken(m_module.GetTypeMetadataToken(clsArgument), CorElementType.ELEMENT_TYPE_VALUETYPE); } else { - InternalAddTypeToken(m_module.GetTypeToken(clsArgument), CorElementType.ELEMENT_TYPE_CLASS); + InternalAddTypeToken(m_module.GetTypeMetadataToken(clsArgument), CorElementType.ELEMENT_TYPE_CLASS); } } } diff --git a/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/SymbolMethod.cs b/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/SymbolMethod.cs index b0be6da1ff0a4..859b6b6efc5c7 100644 --- a/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/SymbolMethod.cs +++ b/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/SymbolMethod.cs @@ -59,7 +59,7 @@ internal override Type[] GetParameterTypes() return m_parameterTypes; } - internal int GetToken(ModuleBuilder mod) + internal int GetToken(RuntimeModuleBuilder mod) { return mod.GetArrayMethodToken(m_containingType, m_name, m_callingConvention, m_returnType, m_parameterTypes); } diff --git a/src/coreclr/System.Private.CoreLib/src/System/Runtime/Loader/AssemblyLoadContext.CoreCLR.cs b/src/coreclr/System.Private.CoreLib/src/System/Runtime/Loader/AssemblyLoadContext.CoreCLR.cs index 7b55a3f341237..75ce968582821 100644 --- a/src/coreclr/System.Private.CoreLib/src/System/Runtime/Loader/AssemblyLoadContext.CoreCLR.cs +++ b/src/coreclr/System.Private.CoreLib/src/System/Runtime/Loader/AssemblyLoadContext.CoreCLR.cs @@ -184,7 +184,7 @@ public void StartProfileOptimization(string? profile) return asm == null ? null : asm is RuntimeAssembly rtAssembly ? rtAssembly : - asm is System.Reflection.Emit.AssemblyBuilder ab ? ab.InternalAssembly : + asm is System.Reflection.Emit.RuntimeAssemblyBuilder ab ? ab.InternalAssembly : null; } diff --git a/src/coreclr/nativeaot/System.Private.CoreLib/src/System.Private.CoreLib.csproj b/src/coreclr/nativeaot/System.Private.CoreLib/src/System.Private.CoreLib.csproj index 550978d492ae6..4739cacc3ec2a 100644 --- a/src/coreclr/nativeaot/System.Private.CoreLib/src/System.Private.CoreLib.csproj +++ b/src/coreclr/nativeaot/System.Private.CoreLib/src/System.Private.CoreLib.csproj @@ -140,23 +140,14 @@ - - - - - - - - - diff --git a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Emit/AssemblyBuilder.cs b/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Emit/AssemblyBuilder.cs index 7c836b8d55eb2..0325a60f818e2 100644 --- a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Emit/AssemblyBuilder.cs +++ b/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Emit/AssemblyBuilder.cs @@ -7,29 +7,8 @@ namespace System.Reflection.Emit { - public sealed partial class AssemblyBuilder : Assembly + public partial class AssemblyBuilder : Assembly { - internal AssemblyBuilder() - { - // Prevent generating a default constructor - } - - public override string FullName - { - get - { - return default; - } - } - - public override Module ManifestModule - { - get - { - return default; - } - } - [RequiresDynamicCode("Defining a dynamic assembly requires dynamic code.")] public static AssemblyBuilder DefineDynamicAssembly(AssemblyName name, AssemblyBuilderAccess access) { @@ -43,33 +22,5 @@ public static AssemblyBuilder DefineDynamicAssembly(AssemblyName name, AssemblyB ReflectionEmitThrower.ThrowPlatformNotSupportedException(); return default; } - - public ModuleBuilder DefineDynamicModule(string name) - { - return default; - } - - public override bool Equals(object? obj) - { - return default; - } - - public ModuleBuilder GetDynamicModule(string name) - { - return default; - } - - public override int GetHashCode() - { - return default; - } - - public void SetCustomAttribute(ConstructorInfo con, byte[] binaryAttribute) - { - } - - public void SetCustomAttribute(CustomAttributeBuilder customBuilder) - { - } } } diff --git a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Emit/ConstructorBuilder.cs b/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Emit/ConstructorBuilder.cs deleted file mode 100644 index 1c9d3cefedf9e..0000000000000 --- a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Emit/ConstructorBuilder.cs +++ /dev/null @@ -1,149 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -using System.Globalization; - -namespace System.Reflection.Emit -{ - public sealed class ConstructorBuilder : ConstructorInfo - { - internal ConstructorBuilder() - { - // Prevent generating a default constructor - } - - public override MethodAttributes Attributes - { - get - { - return default; - } - } - - public override CallingConventions CallingConvention - { - get - { - return default; - } - } - - public override Type DeclaringType - { - get - { - return default; - } - } - - public bool InitLocals - { - get - { - return default; - } - set - { - } - } - - public override RuntimeMethodHandle MethodHandle - { - get - { - return default; - } - } - - public override Module Module - { - get - { - return default; - } - } - - public override string Name - { - get - { - return default; - } - } - - public override Type ReflectedType - { - get - { - return default; - } - } - - public ParameterBuilder DefineParameter(int iSequence, ParameterAttributes attributes, string strParamName) - { - return default; - } - - public override object[] GetCustomAttributes(bool inherit) - { - return default; - } - - public override object[] GetCustomAttributes(Type attributeType, bool inherit) - { - return default; - } - - public ILGenerator GetILGenerator() - { - return default; - } - - public ILGenerator GetILGenerator(int streamSize) - { - return default; - } - - public override MethodImplAttributes GetMethodImplementationFlags() - { - return default; - } - - public override ParameterInfo[] GetParameters() - { - return default; - } - - public override object? Invoke(object? obj, BindingFlags invokeAttr, Binder? binder, object?[]? parameters, CultureInfo? culture) - { - return default; - } - - public override object Invoke(BindingFlags invokeAttr, Binder? binder, object?[]? parameters, CultureInfo? culture) - { - return default; - } - - public override bool IsDefined(Type attributeType, bool inherit) - { - return default; - } - - public void SetCustomAttribute(ConstructorInfo con, byte[] binaryAttribute) - { - } - - public void SetCustomAttribute(CustomAttributeBuilder customBuilder) - { - } - - public void SetImplementationFlags(MethodImplAttributes attributes) - { - } - - public override string ToString() - { - return default; - } - } -} diff --git a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Emit/EnumBuilder.cs b/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Emit/EnumBuilder.cs deleted file mode 100644 index 006f893e0e9c1..0000000000000 --- a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Emit/EnumBuilder.cs +++ /dev/null @@ -1,390 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -using System.Diagnostics.CodeAnalysis; - -namespace System.Reflection.Emit -{ - public sealed partial class EnumBuilder : TypeInfo - { - internal EnumBuilder() - { - // Prevent generating a default constructor - } - - public override Assembly Assembly - { - get - { - return default; - } - } - - public override string AssemblyQualifiedName - { - get - { - return default; - } - } - - public override Type BaseType - { - get - { - return default; - } - } - - public override Type DeclaringType - { - get - { - return default; - } - } - - public override string FullName - { - get - { - return default; - } - } - - public override Guid GUID - { - get - { - return default; - } - } - - public override bool IsConstructedGenericType - { - get - { - return default; - } - } - - public override Module Module - { - get - { - return default; - } - } - - public override string Name - { - get - { - return default; - } - } - - public override string Namespace - { - get - { - return default; - } - } - - public override Type ReflectedType - { - get - { - return default; - } - } - - public override RuntimeTypeHandle TypeHandle - { - get - { - return default; - } - } - - public FieldBuilder UnderlyingField - { - get - { - return default; - } - } - - public override Type UnderlyingSystemType - { - get - { - return default; - } - } - - [return: DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] - public Type? CreateType() - { - return default; - } - - [return: DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] - public TypeInfo? CreateTypeInfo() - { - return default; - } - - public FieldBuilder DefineLiteral(string literalName, object literalValue) - { - return default; - } - - protected override TypeAttributes GetAttributeFlagsImpl() - { - return default; - } - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)] - protected override ConstructorInfo GetConstructorImpl(BindingFlags bindingAttr, Binder binder, CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers) - { - return default; - } - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)] - public override ConstructorInfo[] GetConstructors(BindingFlags bindingAttr) - { - return default; - } - - public override object[] GetCustomAttributes(bool inherit) - { - return default; - } - - public override object[] GetCustomAttributes(Type attributeType, bool inherit) - { - return default; - } - - public override Type GetElementType() - { - return default; - } - - public override Type GetEnumUnderlyingType() - { - return default; - } - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicEvents | DynamicallyAccessedMemberTypes.NonPublicEvents)] - public override EventInfo GetEvent(string name, BindingFlags bindingAttr) - { - return default; - } - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicEvents)] - public override EventInfo[] GetEvents() - { - return default; - } - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicEvents | DynamicallyAccessedMemberTypes.NonPublicEvents)] - public override EventInfo[] GetEvents(BindingFlags bindingAttr) - { - return default; - } - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)] - public override FieldInfo GetField(string name, BindingFlags bindingAttr) - { - return default; - } - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)] - public override FieldInfo[] GetFields(BindingFlags bindingAttr) - { - return default; - } - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.Interfaces)] - [return: DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.Interfaces)] - public override Type? GetInterface(string name, bool ignoreCase) - { - return default; - } - - public override InterfaceMapping GetInterfaceMap([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.NonPublicMethods)] Type interfaceType) - { - return default; - } - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.Interfaces)] - public override Type[] GetInterfaces() - { - return default; - } - - [DynamicallyAccessedMembers(GetAllMembers)] - public override MemberInfo[] GetMember(string name, MemberTypes type, BindingFlags bindingAttr) - { - return default; - } - - [DynamicallyAccessedMembers(GetAllMembers)] - public override MemberInfo[] GetMembers(BindingFlags bindingAttr) - { - return default; - } - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.NonPublicMethods)] - protected override MethodInfo GetMethodImpl(string name, BindingFlags bindingAttr, Binder binder, CallingConventions callConvention, System.Type[] types, ParameterModifier[] modifiers) - { - return default; - } - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.NonPublicMethods)] - public override MethodInfo[] GetMethods(BindingFlags bindingAttr) - { - return default; - } - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicNestedTypes | DynamicallyAccessedMemberTypes.NonPublicNestedTypes)] - public override Type GetNestedType(string name, BindingFlags bindingAttr) - { - return default; - } - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicNestedTypes | DynamicallyAccessedMemberTypes.NonPublicNestedTypes)] - public override Type[] GetNestedTypes(BindingFlags bindingAttr) - { - return default; - } - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypes.NonPublicProperties)] - public override PropertyInfo[] GetProperties(BindingFlags bindingAttr) - { - return default; - } - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypes.NonPublicProperties)] - protected override PropertyInfo GetPropertyImpl(string name, BindingFlags bindingAttr, Binder binder, Type returnType, Type[] types, ParameterModifier[] modifiers) - { - return default; - } - - protected override bool HasElementTypeImpl() - { - return default; - } - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] - public override object? InvokeMember(string name, BindingFlags invokeAttr, Binder? binder, object? target, object?[]? args, ParameterModifier[]? modifiers, Globalization.CultureInfo? culture, string[]? namedParameters) - { - return default; - } - - protected override bool IsArrayImpl() - { - return default; - } - - public override bool IsAssignableFrom([NotNullWhen(true)] TypeInfo? typeInfo) - { - return default; - } - - protected override bool IsByRefImpl() - { - return default; - } - - public override bool IsByRefLike - { - get - { - return default; - } - } - - protected override bool IsCOMObjectImpl() - { - return default; - } - - public override bool IsDefined(Type attributeType, bool inherit) - { - return default; - } - - protected override bool IsPointerImpl() - { - return default; - } - - protected override bool IsPrimitiveImpl() - { - return default; - } - - public override bool IsTypeDefinition - { - get - { - return default; - } - } - - public override bool IsSZArray - { - get - { - return default; - } - } - - public override bool IsVariableBoundArray - { - get - { - return default; - } - } - - protected override bool IsValueTypeImpl() - { - return default; - } - - [RequiresDynamicCode("The code for an array of the specified type might not be available.")] - public override Type MakeArrayType() - { - return default; - } - - [RequiresDynamicCode("The code for an array of the specified type might not be available.")] - public override Type MakeArrayType(int rank) - { - return default; - } - - public override Type MakeByRefType() - { - return default; - } - - public override Type MakePointerType() - { - return default; - } - - public void SetCustomAttribute(ConstructorInfo con, byte[] binaryAttribute) - { - } - - public void SetCustomAttribute(CustomAttributeBuilder customBuilder) - { - } - } -} diff --git a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Emit/EventBuilder.cs b/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Emit/EventBuilder.cs deleted file mode 100644 index 7abaec6cb2ec7..0000000000000 --- a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Emit/EventBuilder.cs +++ /dev/null @@ -1,37 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -namespace System.Reflection.Emit -{ - public sealed class EventBuilder - { - internal EventBuilder() - { - // Prevent generating a default constructor - } - - public void AddOtherMethod(MethodBuilder mdBuilder) - { - } - - public void SetAddOnMethod(MethodBuilder mdBuilder) - { - } - - public void SetCustomAttribute(ConstructorInfo con, byte[] binaryAttribute) - { - } - - public void SetCustomAttribute(CustomAttributeBuilder customBuilder) - { - } - - public void SetRaiseMethod(MethodBuilder mdBuilder) - { - } - - public void SetRemoveOnMethod(MethodBuilder mdBuilder) - { - } - } -} diff --git a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Emit/FieldBuilder.cs b/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Emit/FieldBuilder.cs deleted file mode 100644 index 6c57ca73eebf8..0000000000000 --- a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Emit/FieldBuilder.cs +++ /dev/null @@ -1,104 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -using System.Globalization; - -namespace System.Reflection.Emit -{ - - public sealed class FieldBuilder : FieldInfo - { - internal FieldBuilder() - { - // Prevent generating a default constructor - } - - public override FieldAttributes Attributes - { - get - { - return default; - } - } - - public override Type DeclaringType - { - get - { - return default; - } - } - - public override RuntimeFieldHandle FieldHandle - { - get - { - return default; - } - } - - public override Type FieldType - { - get - { - return default; - } - } - - public override string Name - { - get - { - return default; - } - } - - public override Type ReflectedType - { - get - { - return default; - } - } - - public override object[] GetCustomAttributes(bool inherit) - { - return default; - } - - public override object[] GetCustomAttributes(Type attributeType, bool inherit) - { - return default; - } - - public override object GetValue(object obj) - { - return default; - } - - public override bool IsDefined(Type attributeType, bool inherit) - { - return default; - } - - public void SetConstant(object defaultValue) - { - } - - public void SetCustomAttribute(ConstructorInfo con, byte[] binaryAttribute) - { - } - - public void SetCustomAttribute(CustomAttributeBuilder customBuilder) - { - } - - public void SetOffset(int iOffset) - { - } - - public override void SetValue(object obj, object val, BindingFlags invokeAttr, Binder binder, CultureInfo culture) - { - } - } -} diff --git a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Emit/GenericTypeParameterBuilder.cs b/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Emit/GenericTypeParameterBuilder.cs deleted file mode 100644 index ea0284c4d0dfb..0000000000000 --- a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Emit/GenericTypeParameterBuilder.cs +++ /dev/null @@ -1,470 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -using System.Diagnostics.CodeAnalysis; - -namespace System.Reflection.Emit -{ - public sealed partial class GenericTypeParameterBuilder : TypeInfo - { - internal GenericTypeParameterBuilder() - { - // Prevent generating a default constructor - } - - public override Assembly Assembly - { - get - { - return default; - } - } - - public override string AssemblyQualifiedName - { - get - { - return default; - } - } - - public override Type BaseType - { - get - { - return default; - } - } - - public override bool ContainsGenericParameters - { - get - { - return default; - } - } - - public override MethodBase DeclaringMethod - { - get - { - return default; - } - } - - public override Type DeclaringType - { - get - { - return default; - } - } - - public override string FullName - { - get - { - return default; - } - } - - public override GenericParameterAttributes GenericParameterAttributes - { - get - { - return default; - } - } - - public override int GenericParameterPosition - { - get - { - return default; - } - } - - public override Guid GUID - { - get - { - return default; - } - } - - public override bool IsConstructedGenericType - { - get - { - return default; - } - } - - public override bool IsGenericParameter - { - get - { - return default; - } - } - - public override bool IsGenericType - { - get - { - return default; - } - } - - public override bool IsGenericTypeDefinition - { - get - { - return default; - } - } - - public override Module Module - { - get - { - return default; - } - } - - public override string Name - { - get - { - return default; - } - } - - public override string Namespace - { - get - { - return default; - } - } - - public override Type ReflectedType - { - get - { - return default; - } - } - - public override RuntimeTypeHandle TypeHandle - { - get - { - return default; - } - } - - public override Type UnderlyingSystemType - { - get - { - return default; - } - } - - public override bool Equals(object? o) - { - return default; - } - - protected override TypeAttributes GetAttributeFlagsImpl() - { - return default; - } - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)] - protected override ConstructorInfo GetConstructorImpl(BindingFlags bindingAttr, Binder binder, CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers) - { - return default; - } - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)] - public override ConstructorInfo[] GetConstructors(BindingFlags bindingAttr) - { - return default; - } - - public override object[] GetCustomAttributes(bool inherit) - { - return default; - } - - public override object[] GetCustomAttributes(Type attributeType, bool inherit) - { - return default; - } - - public override Type GetElementType() - { - return default; - } - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicEvents | DynamicallyAccessedMemberTypes.NonPublicEvents)] - public override EventInfo GetEvent(string name, BindingFlags bindingAttr) - { - return default; - } - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicEvents)] - public override EventInfo[] GetEvents() - { - return default; - } - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicEvents | DynamicallyAccessedMemberTypes.NonPublicEvents)] - public override EventInfo[] GetEvents(BindingFlags bindingAttr) - { - return default; - } - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)] - public override FieldInfo GetField(string name, BindingFlags bindingAttr) - { - return default; - } - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)] - public override FieldInfo[] GetFields(BindingFlags bindingAttr) - { - return default; - } - - public override Type[] GetGenericArguments() - { - return default; - } - - public override Type GetGenericTypeDefinition() - { - return default; - } - - public override int GetHashCode() - { - return default; - } - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.Interfaces)] - [return: DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.Interfaces)] - public override Type GetInterface(string name, bool ignoreCase) - { - return default; - } - - public override InterfaceMapping GetInterfaceMap([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.NonPublicMethods)] Type interfaceType) - { - return default; - } - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.Interfaces)] - public override Type[] GetInterfaces() - { - return default; - } - - [DynamicallyAccessedMembers(GetAllMembers)] - public override MemberInfo[] GetMember(string name, MemberTypes type, BindingFlags bindingAttr) - { - return default; - } - - [DynamicallyAccessedMembers(GetAllMembers)] - public override MemberInfo[] GetMembers(BindingFlags bindingAttr) - { - return default; - } - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.NonPublicMethods)] - protected override MethodInfo GetMethodImpl(string name, BindingFlags bindingAttr, Binder binder, CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers) - { - return default; - } - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.NonPublicMethods)] - public override MethodInfo[] GetMethods(BindingFlags bindingAttr) - { - return default; - } - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicNestedTypes | DynamicallyAccessedMemberTypes.NonPublicNestedTypes)] - public override Type GetNestedType(string name, BindingFlags bindingAttr) - { - return default; - } - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicNestedTypes | DynamicallyAccessedMemberTypes.NonPublicNestedTypes)] - public override Type[] GetNestedTypes(BindingFlags bindingAttr) - { - return default; - } - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypes.NonPublicProperties)] - public override PropertyInfo[] GetProperties(BindingFlags bindingAttr) - { - return default; - } - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypes.NonPublicProperties)] - protected override PropertyInfo GetPropertyImpl(string name, BindingFlags bindingAttr, Binder binder, Type returnType, Type[] types, ParameterModifier[] modifiers) - { - return default; - } - - protected override bool HasElementTypeImpl() - { - return default; - } - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] - public override object? InvokeMember(string name, BindingFlags invokeAttr, Binder? binder, object? target, object?[]? args, ParameterModifier[]? modifiers, Globalization.CultureInfo? culture, string[]? namedParameters) - { - return default; - } - - protected override bool IsArrayImpl() - { - return default; - } - - public override bool IsAssignableFrom([NotNullWhen(true)] TypeInfo? typeInfo) - { - return default; - } - - public override bool IsAssignableFrom([NotNullWhen(true)] Type? c) - { - return default; - } - - protected override bool IsByRefImpl() - { - return default; - } - - public override bool IsByRefLike - { - get - { - return default; - } - } - - protected override bool IsCOMObjectImpl() - { - return default; - } - - public override bool IsDefined(Type attributeType, bool inherit) - { - return default; - } - - protected override bool IsPointerImpl() - { - return default; - } - - protected override bool IsPrimitiveImpl() - { - return default; - } - - public override bool IsSubclassOf(Type c) - { - return default; - } - - public override bool IsTypeDefinition - { - get - { - return default; - } - } - - public override bool IsSZArray - { - get - { - return default; - } - } - - public override bool IsVariableBoundArray - { - get - { - return default; - } - } - - protected override bool IsValueTypeImpl() - { - return default; - } - - [RequiresDynamicCode("The code for an array of the specified type might not be available.")] - public override Type MakeArrayType() - { - return default; - } - - [RequiresDynamicCode("The code for an array of the specified type might not be available.")] - public override Type MakeArrayType(int rank) - { - return default; - } - - public override Type MakeByRefType() - { - return default; - } - - [RequiresDynamicCode("The native code for this instantiation might not be available at runtime.")] - [RequiresUnreferencedCode("If some of the generic arguments are annotated (either with DynamicallyAccessedMembersAttribute, or generic constraints), trimming can't validate that the requirements of those annotations are met.")] - public override Type MakeGenericType(params Type[] typeArguments) - { - return default; - } - - public override Type MakePointerType() - { - return default; - } - - public void SetBaseTypeConstraint([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type baseTypeConstraint) - { - } - - public void SetCustomAttribute(ConstructorInfo con, byte[] binaryAttribute) - { - } - - public void SetCustomAttribute(CustomAttributeBuilder customBuilder) - { - } - - public void SetGenericParameterAttributes(GenericParameterAttributes genericParameterAttributes) - { - } - - public void SetInterfaceConstraints(params Type[] interfaceConstraints) - { - } - - public override string ToString() - { - return default; - } - } -} diff --git a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Emit/MethodBuilder.cs b/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Emit/MethodBuilder.cs deleted file mode 100644 index 4737d419ff150..0000000000000 --- a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Emit/MethodBuilder.cs +++ /dev/null @@ -1,249 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -using System.Diagnostics.CodeAnalysis; - -namespace System.Reflection.Emit -{ - public sealed class MethodBuilder : MethodInfo - { - internal MethodBuilder() - { - // Prevent generating a default constructor - } - - public override MethodAttributes Attributes - { - get - { - return default; - } - } - - public override CallingConventions CallingConvention - { - get - { - return default; - } - } - - public override bool ContainsGenericParameters - { - get - { - return default; - } - } - - public override Type DeclaringType - { - get - { - return default; - } - } - - public bool InitLocals - { - get - { - return default; - } - set - { - } - } - - public override bool IsGenericMethod - { - get - { - return default; - } - } - - public override bool IsGenericMethodDefinition - { - get - { - return default; - } - } - - public override bool IsConstructedGenericMethod - { - get - { - return default; - } - } - - public override RuntimeMethodHandle MethodHandle - { - get - { - return default; - } - } - - public override Module Module - { - get - { - return default; - } - } - - public override string Name - { - get - { - return default; - } - } - - public override Type ReflectedType - { - get - { - return default; - } - } - - public override ParameterInfo ReturnParameter - { - get - { - return default; - } - } - - public override Type ReturnType - { - get - { - return default; - } - } - - public override ICustomAttributeProvider ReturnTypeCustomAttributes - { - get - { - return default; - } - } - - public GenericTypeParameterBuilder[] DefineGenericParameters(params string[] names) - { - return default; - } - - public ParameterBuilder DefineParameter(int position, ParameterAttributes attributes, string strParamName) - { - return default; - } - - public override bool Equals(object? obj) - { - return default; - } - - public override MethodInfo GetBaseDefinition() - { - return default; - } - - public override object[] GetCustomAttributes(bool inherit) - { - return default; - } - - public override object[] GetCustomAttributes(Type attributeType, bool inherit) - { - return default; - } - - public override Type[] GetGenericArguments() - { - return default; - } - - public override MethodInfo GetGenericMethodDefinition() - { - return default; - } - - public override int GetHashCode() - { - return default; - } - - public ILGenerator GetILGenerator() - { - return default; - } - - public ILGenerator GetILGenerator(int size) - { - return default; - } - - public override MethodImplAttributes GetMethodImplementationFlags() - { - return default; - } - - public override ParameterInfo[] GetParameters() - { - return default; - } - - public override object? Invoke(object? obj, BindingFlags invokeAttr, Binder? binder, object?[]? parameters, Globalization.CultureInfo? culture) - { - return default; - } - - public override bool IsDefined(Type attributeType, bool inherit) - { - return default; - } - - [RequiresDynamicCode("The native code for this instantiation might not be available at runtime.")] - [RequiresUnreferencedCode("If some of the generic arguments are annotated (either with DynamicallyAccessedMembersAttribute, or generic constraints), trimming can't validate that the requirements of those annotations are met.")] - public override MethodInfo MakeGenericMethod(params Type[] typeArguments) - { - return default; - } - - public void SetCustomAttribute(ConstructorInfo con, byte[] binaryAttribute) - { - } - - public void SetCustomAttribute(CustomAttributeBuilder customBuilder) - { - } - - public void SetImplementationFlags(MethodImplAttributes attributes) - { - } - - public void SetParameters(params Type[] parameterTypes) - { - } - - public void SetReturnType(Type returnType) - { - } - - public void SetSignature(Type returnType, Type[] returnTypeRequiredCustomModifiers, Type[] returnTypeOptionalCustomModifiers, Type[] parameterTypes, Type[][] parameterTypeRequiredCustomModifiers, Type[][] parameterTypeOptionalCustomModifiers) - { - } - - public override string ToString() - { - return default; - } - } -} diff --git a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Emit/ModuleBuilder.cs b/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Emit/ModuleBuilder.cs deleted file mode 100644 index 41c906da53ba0..0000000000000 --- a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Emit/ModuleBuilder.cs +++ /dev/null @@ -1,105 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -using System.Diagnostics.CodeAnalysis; -using System.Runtime.InteropServices; - -namespace System.Reflection.Emit -{ - public partial class ModuleBuilder : Module - { - internal ModuleBuilder() - { - // Prevent generating a default constructor - } - - public override Assembly Assembly - { - get - { - return default; - } - } - - [RequiresAssemblyFiles(UnknownStringMessageInRAF)] - public override string FullyQualifiedName - { - get - { - return default; - } - } - - [RequiresAssemblyFiles(UnknownStringMessageInRAF)] - public override string Name - { - get - { - return default; - } - } - - public void CreateGlobalFunctions() - { - } - - public EnumBuilder DefineEnum(string name, TypeAttributes visibility, Type underlyingType) - { - return default; - } - - public MethodBuilder DefineGlobalMethod(string name, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] requiredReturnTypeCustomModifiers, Type[] optionalReturnTypeCustomModifiers, Type[] parameterTypes, Type[][] requiredParameterTypeCustomModifiers, Type[][] optionalParameterTypeCustomModifiers) - { - return default; - } - - public FieldBuilder DefineInitializedData(string name, byte[] data, FieldAttributes attributes) - { - return default; - } - - [RequiresUnreferencedCode("P/Invoke marshalling may dynamically access members that could be trimmed.")] - public MethodBuilder DefinePInvokeMethod(string name, string dllName, string entryName, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes, CallingConvention nativeCallConv, CharSet nativeCharSet) - { - return default; - } - - public TypeBuilder DefineType(string name, TypeAttributes attr, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type parent, PackingSize packingSize, int typesize) - { - return default; - } - - public TypeBuilder DefineType(string name, TypeAttributes attr, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type parent, Type[] interfaces) - { - return default; - } - - public FieldBuilder DefineUninitializedData(string name, int size, FieldAttributes attributes) - { - return default; - } - - public override bool Equals(object? obj) - { - return default; - } - - public MethodInfo GetArrayMethod(Type arrayClass, string methodName, CallingConventions callingConvention, Type returnType, Type[] parameterTypes) - { - return default; - } - - public override int GetHashCode() - { - return default; - } - - public void SetCustomAttribute(ConstructorInfo con, byte[] binaryAttribute) - { - } - - public void SetCustomAttribute(CustomAttributeBuilder customBuilder) - { - } - } -} diff --git a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Emit/PropertyBuilder.cs b/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Emit/PropertyBuilder.cs deleted file mode 100644 index 940a21a42b88d..0000000000000 --- a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Emit/PropertyBuilder.cs +++ /dev/null @@ -1,154 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -namespace System.Reflection.Emit -{ - public sealed class PropertyBuilder : PropertyInfo - { - internal PropertyBuilder() - { - // Prevent generating a default constructor - } - - public override PropertyAttributes Attributes - { - get - { - return default; - } - } - - public override bool CanRead - { - get - { - return default; - } - } - - public override bool CanWrite - { - get - { - return default; - } - } - - public override Type DeclaringType - { - get - { - return default; - } - } - - public override Module Module - { - get - { - return default; - } - } - - public override string Name - { - get - { - return default; - } - } - - public override Type PropertyType - { - get - { - return default; - } - } - - public override Type ReflectedType - { - get - { - return default; - } - } - - public void AddOtherMethod(MethodBuilder mdBuilder) - { - } - - public override MethodInfo[] GetAccessors(bool nonPublic) - { - return default; - } - - public override object[] GetCustomAttributes(bool inherit) - { - return default; - } - - public override object[] GetCustomAttributes(Type attributeType, bool inherit) - { - return default; - } - - public override MethodInfo GetGetMethod(bool nonPublic) - { - return default; - } - - public override ParameterInfo[] GetIndexParameters() - { - return default; - } - - public override MethodInfo GetSetMethod(bool nonPublic) - { - return default; - } - - public override object? GetValue(object? obj, object?[]? index) - { - return default; - } - - public override object? GetValue(object? obj, BindingFlags invokeAttr, Binder? binder, object?[]? index, Globalization.CultureInfo? culture) - { - return default; - } - - public override bool IsDefined(Type attributeType, bool inherit) - { - return default; - } - - public void SetConstant(object defaultValue) - { - } - - public void SetCustomAttribute(ConstructorInfo con, byte[] binaryAttribute) - { - } - - public void SetCustomAttribute(CustomAttributeBuilder customBuilder) - { - } - - public void SetGetMethod(MethodBuilder mdBuilder) - { - } - - public void SetSetMethod(MethodBuilder mdBuilder) - { - } - - public override void SetValue(object? obj, object? value, object?[]? index) - { - } - - public override void SetValue(object? obj, object? value, BindingFlags invokeAttr, Binder? binder, object?[]? index, Globalization.CultureInfo? culture) - { - } - } -} diff --git a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilder.cs b/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilder.cs deleted file mode 100644 index 2eb974bce303c..0000000000000 --- a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilder.cs +++ /dev/null @@ -1,587 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -using System.Diagnostics.CodeAnalysis; -using System.Runtime.InteropServices; - -namespace System.Reflection.Emit -{ - public sealed partial class TypeBuilder : TypeInfo - { - internal TypeBuilder() - { - // Prevent generating a default constructor - } - - public override Assembly Assembly - { - get - { - return default; - } - } - - public override string AssemblyQualifiedName - { - get - { - return default; - } - } - - public override Type BaseType - { - get - { - return default; - } - } - - public override MethodBase DeclaringMethod - { - get - { - return default; - } - } - - public override Type DeclaringType - { - get - { - return default; - } - } - - public override string FullName - { - get - { - return default; - } - } - - public override GenericParameterAttributes GenericParameterAttributes - { - get - { - return default; - } - } - - public override int GenericParameterPosition - { - get - { - return default; - } - } - - public override Guid GUID - { - get - { - return default; - } - } - - public override bool IsByRefLike - { - get - { - return default; - } - } - - public override bool IsConstructedGenericType - { - get - { - return default; - } - } - - public override bool IsGenericParameter - { - get - { - return default; - } - } - - public override bool IsGenericType - { - get - { - return default; - } - } - - public override bool IsGenericTypeDefinition - { - get - { - return default; - } - } - - public override bool IsSecurityCritical - { - get - { - return default; - } - } - - public override bool IsSecuritySafeCritical - { - get - { - return default; - } - } - - public override bool IsSecurityTransparent - { - get - { - return default; - } - } - - public override Module Module - { - get - { - return default; - } - } - - public override string Name - { - get - { - return default; - } - } - - public override string Namespace - { - get - { - return default; - } - } - - public PackingSize PackingSize - { - get - { - return default; - } - } - - public override Type ReflectedType - { - get - { - return default; - } - } - - public int Size - { - get - { - return default; - } - } - - public override RuntimeTypeHandle TypeHandle - { - get - { - return default; - } - } - - public override Type UnderlyingSystemType - { - get - { - return default; - } - } - - public void AddInterfaceImplementation([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type interfaceType) { } - - [return: DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] - public Type? CreateType() - { - return default; - } - - [return: DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] - public TypeInfo? CreateTypeInfo() - { - return default; - } - - public ConstructorBuilder DefineConstructor(MethodAttributes attributes, CallingConventions callingConvention, Type[] parameterTypes, Type[][] requiredCustomModifiers, Type[][] optionalCustomModifiers) - { - return default; - } - - public ConstructorBuilder DefineDefaultConstructor(MethodAttributes attributes) - { - return default; - } - - public EventBuilder DefineEvent(string name, EventAttributes attributes, Type eventtype) - { - return default; - } - - public FieldBuilder DefineField(string fieldName, Type type, Type[] requiredCustomModifiers, Type[] optionalCustomModifiers, FieldAttributes attributes) - { - return default; - } - - public GenericTypeParameterBuilder[] DefineGenericParameters(params string[] names) - { - return default; - } - - public FieldBuilder DefineInitializedData(string name, byte[] data, FieldAttributes attributes) - { - return default; - } - - public MethodBuilder DefineMethod(string name, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] returnTypeRequiredCustomModifiers, Type[] returnTypeOptionalCustomModifiers, Type[] parameterTypes, Type[][] parameterTypeRequiredCustomModifiers, Type[][] parameterTypeOptionalCustomModifiers) - { - return default; - } - - public void DefineMethodOverride(MethodInfo methodInfoBody, MethodInfo methodInfoDeclaration) - { - } - - public TypeBuilder DefineNestedType(string name, TypeAttributes attr, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type parent, PackingSize packSize, int typeSize) - { - return default; - } - - public TypeBuilder DefineNestedType(string name, TypeAttributes attr, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type parent, Type[] interfaces) - { - return default; - } - - [RequiresUnreferencedCode("P/Invoke marshalling may dynamically access members that could be trimmed.")] - public MethodBuilder DefinePInvokeMethod(string name, string dllName, string entryName, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] returnTypeRequiredCustomModifiers, Type[] returnTypeOptionalCustomModifiers, Type[] parameterTypes, Type[][] parameterTypeRequiredCustomModifiers, Type[][] parameterTypeOptionalCustomModifiers, CallingConvention nativeCallConv, CharSet nativeCharSet) - { - return default; - } - - public PropertyBuilder DefineProperty(string name, PropertyAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] returnTypeRequiredCustomModifiers, Type[] returnTypeOptionalCustomModifiers, Type[] parameterTypes, Type[][] parameterTypeRequiredCustomModifiers, Type[][] parameterTypeOptionalCustomModifiers) - { - return default; - } - - public ConstructorBuilder DefineTypeInitializer() - { - return default; - } - - public FieldBuilder DefineUninitializedData(string name, int size, FieldAttributes attributes) - { - return default; - } - - protected override TypeAttributes GetAttributeFlagsImpl() - { - return default; - } - - public static ConstructorInfo GetConstructor(Type type, ConstructorInfo constructor) - { - ReflectionEmitThrower.ThrowPlatformNotSupportedException(); - return default; - } - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)] - protected override ConstructorInfo GetConstructorImpl(BindingFlags bindingAttr, Binder binder, CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers) - { - return default; - } - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)] - public override ConstructorInfo[] GetConstructors(BindingFlags bindingAttr) - { - return default; - } - - public override object[] GetCustomAttributes(bool inherit) - { - return default; - } - - public override object[] GetCustomAttributes(Type attributeType, bool inherit) - { - return default; - } - - public override Type GetElementType() - { - return default; - } - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicEvents | DynamicallyAccessedMemberTypes.NonPublicEvents)] - public override EventInfo GetEvent(string name, BindingFlags bindingAttr) - { - return default; - } - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicEvents)] - public override EventInfo[] GetEvents() - { - return default; - } - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicEvents | DynamicallyAccessedMemberTypes.NonPublicEvents)] - public override EventInfo[] GetEvents(BindingFlags bindingAttr) - { - return default; - } - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)] - public override FieldInfo GetField(string name, BindingFlags bindingAttr) - { - return default; - } - - public static FieldInfo GetField(Type type, FieldInfo field) - { - ReflectionEmitThrower.ThrowPlatformNotSupportedException(); - return default; - } - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)] - public override FieldInfo[] GetFields(BindingFlags bindingAttr) - { - return default; - } - - public override Type[] GetGenericArguments() - { - return default; - } - - public override Type GetGenericTypeDefinition() - { - return default; - } - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.Interfaces)] - [return: DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.Interfaces)] - public override Type GetInterface(string name, bool ignoreCase) - { - return default; - } - - public override InterfaceMapping GetInterfaceMap([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.NonPublicMethods)] Type interfaceType) - { - return default; - } - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.Interfaces)] - public override Type[] GetInterfaces() - { - return default; - } - - [DynamicallyAccessedMembers(GetAllMembers)] - public override MemberInfo[] GetMember(string name, MemberTypes type, BindingFlags bindingAttr) - { - return default; - } - - [DynamicallyAccessedMembers(GetAllMembers)] - public override MemberInfo[] GetMembers(BindingFlags bindingAttr) - { - return default; - } - - public static MethodInfo GetMethod(Type type, MethodInfo method) - { - ReflectionEmitThrower.ThrowPlatformNotSupportedException(); - return default; - } - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.NonPublicMethods)] - protected override MethodInfo GetMethodImpl(string name, BindingFlags bindingAttr, Binder binder, CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers) - { - return default; - } - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.NonPublicMethods)] - public override MethodInfo[] GetMethods(BindingFlags bindingAttr) - { - return default; - } - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicNestedTypes | DynamicallyAccessedMemberTypes.NonPublicNestedTypes)] - public override Type GetNestedType(string name, BindingFlags bindingAttr) - { - return default; - } - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicNestedTypes | DynamicallyAccessedMemberTypes.NonPublicNestedTypes)] - public override Type[] GetNestedTypes(BindingFlags bindingAttr) - { - return default; - } - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypes.NonPublicProperties)] - public override PropertyInfo[] GetProperties(BindingFlags bindingAttr) - { - return default; - } - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypes.NonPublicProperties)] - protected override PropertyInfo GetPropertyImpl(string name, BindingFlags bindingAttr, Binder binder, Type returnType, Type[] types, ParameterModifier[] modifiers) - { - return default; - } - - protected override bool HasElementTypeImpl() - { - return default; - } - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] - public override object? InvokeMember(string name, BindingFlags invokeAttr, Binder? binder, object? target, object?[]? args, ParameterModifier[]? modifiers, Globalization.CultureInfo? culture, string[]? namedParameters) - { - return default; - } - - protected override bool IsArrayImpl() - { - return default; - } - - public override bool IsAssignableFrom([NotNullWhen(true)] TypeInfo? typeInfo) - { - return default; - } - - public override bool IsAssignableFrom([NotNullWhen(true)] Type? c) - { - return default; - } - - protected override bool IsByRefImpl() - { - return default; - } - - protected override bool IsCOMObjectImpl() - { - return default; - } - - public bool IsCreated() - { - return default; - } - - public override bool IsDefined(Type attributeType, bool inherit) - { - return default; - } - - protected override bool IsPointerImpl() - { - return default; - } - - protected override bool IsPrimitiveImpl() - { - return default; - } - - public override bool IsSubclassOf(Type c) - { - return default; - } - - public override bool IsTypeDefinition - { - get - { - return default; - } - } - - public override bool IsSZArray - { - get - { - return default; - } - } - - public override bool IsVariableBoundArray - { - get - { - return default; - } - } - - [RequiresDynamicCode("The code for an array of the specified type might not be available.")] - public override Type MakeArrayType() - { - return default; - } - - [RequiresDynamicCode("The code for an array of the specified type might not be available.")] - public override Type MakeArrayType(int rank) - { - return default; - } - - public override Type MakeByRefType() - { - return default; - } - - [RequiresDynamicCode("The native code for this instantiation might not be available at runtime.")] - [RequiresUnreferencedCode("If some of the generic arguments are annotated (either with DynamicallyAccessedMembersAttribute, or generic constraints), trimming can't validate that the requirements of those annotations are met.")] - public override Type MakeGenericType(params Type[] typeArguments) - { - return default; - } - - public override Type MakePointerType() - { - return default; - } - - public void SetCustomAttribute(ConstructorInfo con, byte[] binaryAttribute) - { - } - - public void SetCustomAttribute(CustomAttributeBuilder customBuilder) - { - } - - public void SetParent([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type parent) - { - } - - public override string ToString() - { - return default; - } - } -} diff --git a/src/libraries/System.Private.CoreLib/src/Resources/Strings.resx b/src/libraries/System.Private.CoreLib/src/Resources/Strings.resx index dd206f893724a..7e8bb96f4d014 100644 --- a/src/libraries/System.Private.CoreLib/src/Resources/Strings.resx +++ b/src/libraries/System.Private.CoreLib/src/Resources/Strings.resx @@ -1334,7 +1334,7 @@ Type must be a runtime Type object. - 'type' must contain a TypeBuilder as a generic argument. + 'type' must be or must contain a TypeBuilder as a generic argument. Type passed in must be derived from System.Attribute or System.Attribute itself. @@ -3992,4 +3992,4 @@ The value may not contain directory separator characters. - + \ No newline at end of file diff --git a/src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems b/src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems index 73ff2078c03d4..c842dbbb7ae74 100644 --- a/src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems +++ b/src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems @@ -633,10 +633,16 @@ + + + + + + @@ -644,6 +650,7 @@ + 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 b0345a30cbbf6..6c63291e4d3f2 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 @@ -7,8 +7,54 @@ namespace System.Reflection.Emit { - public sealed partial class AssemblyBuilder : Assembly + public abstract partial class AssemblyBuilder : Assembly { + protected AssemblyBuilder() + { + } + + public ModuleBuilder DefineDynamicModule(string name) + { + ArgumentException.ThrowIfNullOrEmpty(name); + + if (name[0] == '\0') + { + throw new ArgumentException(SR.Argument_InvalidName, nameof(name)); + } + + return DefineDynamicModuleCore(name); + } + + protected abstract ModuleBuilder DefineDynamicModuleCore(string name); + + public ModuleBuilder? GetDynamicModule(string name) + { + ArgumentException.ThrowIfNullOrEmpty(name); + + return GetDynamicModuleCore(name); + } + + protected abstract ModuleBuilder? GetDynamicModuleCore(string name); + + public void SetCustomAttribute(ConstructorInfo con, byte[] binaryAttribute) + { + ArgumentNullException.ThrowIfNull(con); + ArgumentNullException.ThrowIfNull(binaryAttribute); + + SetCustomAttributeCore(con, binaryAttribute); + } + + protected abstract void SetCustomAttributeCore(ConstructorInfo con, byte[] binaryAttribute); + + public void SetCustomAttribute(CustomAttributeBuilder customBuilder) + { + ArgumentNullException.ThrowIfNull(customBuilder); + + SetCustomAttributeCore(customBuilder); + } + + protected abstract void SetCustomAttributeCore(CustomAttributeBuilder customBuilder); + [System.ObsoleteAttribute("Assembly.CodeBase and Assembly.EscapedCodeBase are only included for .NET Framework compatibility. Use Assembly.Location instead.", DiagnosticId = "SYSLIB0012", UrlFormat = "https://aka.ms/dotnet-warnings/{0}")] [RequiresAssemblyFiles(ThrowingMessageInRAF)] public override string? CodeBase => throw new NotSupportedException(SR.NotSupported_DynamicAssembly); diff --git a/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/ConstructorBuilder.cs b/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/ConstructorBuilder.cs new file mode 100644 index 0000000000000..3daac6f42875b --- /dev/null +++ b/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/ConstructorBuilder.cs @@ -0,0 +1,48 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +namespace System.Reflection.Emit +{ + public abstract class ConstructorBuilder : ConstructorInfo + { + protected ConstructorBuilder() + { + } + + public bool InitLocals + { + get => InitLocalsCore; + set { InitLocalsCore = value; } + } + + protected abstract bool InitLocalsCore { get; set; } + + public ParameterBuilder DefineParameter(int iSequence, ParameterAttributes attributes, string strParamName) + => DefineParameterCore(iSequence, attributes, strParamName); + + protected abstract ParameterBuilder DefineParameterCore(int iSequence, ParameterAttributes attributes, string strParamName); + + public ILGenerator GetILGenerator() + => GetILGeneratorCore(64); + + public ILGenerator GetILGenerator(int streamSize) + => GetILGeneratorCore(streamSize); + + protected abstract ILGenerator GetILGeneratorCore(int streamSize); + + public void SetCustomAttribute(ConstructorInfo con, byte[] binaryAttribute) + => SetCustomAttributeCore(con, binaryAttribute); + + protected abstract void SetCustomAttributeCore(ConstructorInfo con, byte[] binaryAttribute); + + public void SetCustomAttribute(CustomAttributeBuilder customBuilder) + => SetCustomAttributeCore(customBuilder); + + protected abstract void SetCustomAttributeCore(CustomAttributeBuilder customBuilder); + + public void SetImplementationFlags(MethodImplAttributes attributes) + => SetImplementationFlagsCore(attributes); + + protected abstract void SetImplementationFlagsCore(MethodImplAttributes attributes); + } +} diff --git a/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/DynamicMethod.cs b/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/DynamicMethod.cs index 970859002073d..1e0622e385931 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/DynamicMethod.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/DynamicMethod.cs @@ -5,6 +5,7 @@ using System.Diagnostics.CodeAnalysis; using System.Globalization; using System.Runtime.CompilerServices; +using System.Runtime.Loader; using System.Text; namespace System.Reflection.Emit @@ -186,6 +187,8 @@ private static Module GetDynamicMethodsModule() if (s_anonymouslyHostedDynamicMethodsModule != null) return s_anonymouslyHostedDynamicMethodsModule; + AssemblyBuilder.EnsureDynamicCodeSupported(); + lock (s_anonymouslyHostedDynamicMethodsModuleLock) { if (s_anonymouslyHostedDynamicMethodsModule != null) @@ -193,12 +196,8 @@ private static Module GetDynamicMethodsModule() AssemblyName assemblyName = new AssemblyName("Anonymously Hosted DynamicMethods Assembly"); - AssemblyBuilder assembly = AssemblyBuilder.InternalDefineDynamicAssembly( - assemblyName, - AssemblyBuilderAccess.Run, - typeof(object).Assembly, - null, - null); + var assembly = RuntimeAssemblyBuilder.InternalDefineDynamicAssembly(assemblyName, + AssemblyBuilderAccess.Run, AssemblyLoadContext.Default, null); // this always gets the internal module. s_anonymouslyHostedDynamicMethodsModule = assembly.ManifestModule!; @@ -264,7 +263,7 @@ private static Module GetDynamicMethodsModule() Debug.Assert(m == null || owner == null, "m and owner cannot both be set"); if (m != null) - _module = ModuleBuilder.GetRuntimeModuleFromModule(m); // this returns the underlying module for all RuntimeModule and ModuleBuilder objects. + _module = RuntimeModuleBuilder.GetRuntimeModuleFromModule(m); // this returns the underlying module for all RuntimeModule and ModuleBuilder objects. else { if (owner?.UnderlyingSystemType is RuntimeType rtOwner) diff --git a/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/EnumBuilder.cs b/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/EnumBuilder.cs new file mode 100644 index 0000000000000..f3cdb7f848fb3 --- /dev/null +++ b/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/EnumBuilder.cs @@ -0,0 +1,45 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System.Diagnostics.CodeAnalysis; + +namespace System.Reflection.Emit +{ + public abstract partial class EnumBuilder : TypeInfo + { + protected EnumBuilder() + { + } + + public FieldBuilder UnderlyingField + => UnderlyingFieldCore; + + protected abstract FieldBuilder UnderlyingFieldCore { get; } + + [return: DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] + public Type CreateType() + => CreateTypeInfoCore(); + + [return: DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] + public TypeInfo CreateTypeInfo() + => CreateTypeInfoCore(); + + [return: DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] + protected abstract TypeInfo CreateTypeInfoCore(); + + public FieldBuilder DefineLiteral(string literalName, object? literalValue) + => DefineLiteralCore(literalName, literalValue); + + protected abstract FieldBuilder DefineLiteralCore(string literalName, object? literalValue); + + public void SetCustomAttribute(ConstructorInfo con, byte[] binaryAttribute) + => SetCustomAttributeCore(con, binaryAttribute); + + protected abstract void SetCustomAttributeCore(ConstructorInfo con, byte[] binaryAttribute); + + public void SetCustomAttribute(CustomAttributeBuilder customBuilder) + => SetCustomAttributeCore(customBuilder); + + protected abstract void SetCustomAttributeCore(CustomAttributeBuilder customBuilder); + } +} diff --git a/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/EventBuilder.cs b/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/EventBuilder.cs new file mode 100644 index 0000000000000..4c4a8f09f6ed4 --- /dev/null +++ b/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/EventBuilder.cs @@ -0,0 +1,51 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +namespace System.Reflection.Emit +{ + public abstract class EventBuilder + { + protected EventBuilder() + { + } + + public void AddOtherMethod(MethodBuilder mdBuilder) + => AddOtherMethodCore(mdBuilder); + + protected abstract void AddOtherMethodCore(MethodBuilder mdBuilder); + + public void SetAddOnMethod(MethodBuilder mdBuilder) + => SetAddOnMethodCore(mdBuilder); + + protected abstract void SetAddOnMethodCore(MethodBuilder mdBuilder); + + public void SetCustomAttribute(ConstructorInfo con, byte[] binaryAttribute) + { + ArgumentNullException.ThrowIfNull(con); + ArgumentNullException.ThrowIfNull(binaryAttribute); + + SetCustomAttributeCore(con, binaryAttribute); + } + + protected abstract void SetCustomAttributeCore(ConstructorInfo con, byte[] binaryAttribute); + + public void SetCustomAttribute(CustomAttributeBuilder customBuilder) + { + ArgumentNullException.ThrowIfNull(customBuilder); + + SetCustomAttributeCore(customBuilder); + } + + protected abstract void SetCustomAttributeCore(CustomAttributeBuilder customBuilder); + + public void SetRaiseMethod(MethodBuilder mdBuilder) + => SetRaiseMethodCore(mdBuilder); + + protected abstract void SetRaiseMethodCore(MethodBuilder mdBuilder); + + public void SetRemoveOnMethod(MethodBuilder mdBuilder) + => SetRemoveOnMethodCore(mdBuilder); + + protected abstract void SetRemoveOnMethodCore(MethodBuilder mdBuilder); + } +} diff --git a/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/FieldBuilder.cs b/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/FieldBuilder.cs new file mode 100644 index 0000000000000..2609261094562 --- /dev/null +++ b/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/FieldBuilder.cs @@ -0,0 +1,41 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +namespace System.Reflection.Emit +{ + public abstract class FieldBuilder : FieldInfo + { + protected FieldBuilder() + { + } + + public void SetConstant(object? defaultValue) + => SetConstantCore(defaultValue); + + protected abstract void SetConstantCore(object? defaultValue); + + public void SetCustomAttribute(ConstructorInfo con, byte[] binaryAttribute) + { + ArgumentNullException.ThrowIfNull(con); + ArgumentNullException.ThrowIfNull(binaryAttribute); + + SetCustomAttributeCore(con, binaryAttribute); + } + + protected abstract void SetCustomAttributeCore(ConstructorInfo con, byte[] binaryAttribute); + + public void SetCustomAttribute(CustomAttributeBuilder customBuilder) + { + ArgumentNullException.ThrowIfNull(customBuilder); + + SetCustomAttributeCore(customBuilder); + } + + protected abstract void SetCustomAttributeCore(CustomAttributeBuilder customBuilder); + + public void SetOffset(int iOffset) + => SetOffsetCore(iOffset); + + protected abstract void SetOffsetCore(int iOffset); + } +} diff --git a/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/GenericTypeParameterBuilder.cs b/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/GenericTypeParameterBuilder.cs new file mode 100644 index 0000000000000..3594991999021 --- /dev/null +++ b/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/GenericTypeParameterBuilder.cs @@ -0,0 +1,39 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System.Diagnostics.CodeAnalysis; + +namespace System.Reflection.Emit +{ + public abstract partial class GenericTypeParameterBuilder : TypeInfo + { + protected GenericTypeParameterBuilder() + { + } + + public void SetCustomAttribute(ConstructorInfo con, byte[] binaryAttribute) => SetCustomAttributeCore(con, binaryAttribute); + + protected abstract void SetCustomAttributeCore(ConstructorInfo con, byte[] binaryAttribute); + + public void SetCustomAttribute(CustomAttributeBuilder customBuilder) + { + ArgumentNullException.ThrowIfNull(customBuilder); + + SetCustomAttributeCore(customBuilder); + } + + protected abstract void SetCustomAttributeCore(CustomAttributeBuilder customBuilder); + + public void SetBaseTypeConstraint([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type? baseTypeConstraint) => SetBaseTypeConstraintCore(baseTypeConstraint); + + protected abstract void SetBaseTypeConstraintCore([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type? baseTypeConstraint); + + public void SetInterfaceConstraints(params Type[]? interfaceConstraints) => SetInterfaceConstraintsCore(interfaceConstraints); + + protected abstract void SetInterfaceConstraintsCore(params Type[]? interfaceConstraints); + + public void SetGenericParameterAttributes(GenericParameterAttributes genericParameterAttributes) => SetGenericParameterAttributesCore(genericParameterAttributes); + + protected abstract void SetGenericParameterAttributesCore(GenericParameterAttributes genericParameterAttributes); + } +} diff --git a/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/MethodBuilder.cs b/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/MethodBuilder.cs new file mode 100644 index 0000000000000..dab5cd525e029 --- /dev/null +++ b/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/MethodBuilder.cs @@ -0,0 +1,88 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System.Diagnostics.CodeAnalysis; + +namespace System.Reflection.Emit +{ + public abstract class MethodBuilder : MethodInfo + { + protected MethodBuilder() + { + } + + public bool InitLocals + { + get => InitLocalsCore; + set { InitLocalsCore = value; } + } + + protected abstract bool InitLocalsCore { get; set; } + + public GenericTypeParameterBuilder[] DefineGenericParameters(params string[] names) + { + ArgumentNullException.ThrowIfNull(names); + + if (names.Length == 0) + throw new ArgumentException(SR.Arg_EmptyArray, nameof(names)); + + return DefineGenericParametersCore(names); + } + + protected abstract GenericTypeParameterBuilder[] DefineGenericParametersCore(params string[] names); + + public ParameterBuilder DefineParameter(int position, ParameterAttributes attributes, string? strParamName) + { + ArgumentOutOfRangeException.ThrowIfNegative(position); + + return DefineParameterCore(position, attributes, strParamName); + } + + protected abstract ParameterBuilder DefineParameterCore(int position, ParameterAttributes attributes, string? strParamName); + + public ILGenerator GetILGenerator() + => GetILGenerator(64); + + public ILGenerator GetILGenerator(int size) + => GetILGeneratorCore(size); + + protected abstract ILGenerator GetILGeneratorCore(int size); + + public void SetCustomAttribute(ConstructorInfo con, byte[] binaryAttribute) + { + ArgumentNullException.ThrowIfNull(con); + ArgumentNullException.ThrowIfNull(binaryAttribute); + + SetCustomAttributeCore(con, binaryAttribute); + } + + protected abstract void SetCustomAttributeCore(ConstructorInfo con, byte[] binaryAttribute); + + public void SetCustomAttribute(CustomAttributeBuilder customBuilder) + { + ArgumentNullException.ThrowIfNull(customBuilder); + + SetCustomAttributeCore(customBuilder); + } + + protected abstract void SetCustomAttributeCore(CustomAttributeBuilder customBuilder); + + public void SetImplementationFlags(MethodImplAttributes attributes) => SetImplementationFlagsCore(attributes); + + protected abstract void SetImplementationFlagsCore(MethodImplAttributes attributes); + + public void SetParameters(params Type[] parameterTypes) + => SetSignature(null, null, null, parameterTypes, null, null); + + public void SetReturnType(Type? returnType) + => SetSignature(returnType, null, null, null, null, null); + + public void SetSignature(Type? returnType, Type[]? returnTypeRequiredCustomModifiers, Type[]? returnTypeOptionalCustomModifiers, + Type[]? parameterTypes, Type[][]? parameterTypeRequiredCustomModifiers, Type[][]? parameterTypeOptionalCustomModifiers) + => SetSignatureCore(returnType, returnTypeRequiredCustomModifiers, returnTypeOptionalCustomModifiers, + parameterTypes, parameterTypeRequiredCustomModifiers, parameterTypeOptionalCustomModifiers); + + protected abstract void SetSignatureCore(Type? returnType, Type[]? returnTypeRequiredCustomModifiers, Type[]? returnTypeOptionalCustomModifiers, + Type[]? parameterTypes, Type[][]? parameterTypeRequiredCustomModifiers, Type[][]? parameterTypeOptionalCustomModifiers); + } +} diff --git a/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/ModuleBuilder.cs b/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/ModuleBuilder.cs index 45f413b369fdf..0e2a07f03ab2d 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/ModuleBuilder.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/ModuleBuilder.cs @@ -6,8 +6,22 @@ namespace System.Reflection.Emit { - public partial class ModuleBuilder : Module + public abstract class ModuleBuilder : Module { + protected ModuleBuilder() + { + } + + public void CreateGlobalFunctions() + => CreateGlobalFunctionsCore(); + + protected abstract void CreateGlobalFunctionsCore(); + + public EnumBuilder DefineEnum(string name, TypeAttributes visibility, Type underlyingType) + => DefineEnumCore(name, visibility, underlyingType); + + protected abstract EnumBuilder DefineEnumCore(string name, TypeAttributes visibility, Type underlyingType); + public MethodBuilder DefineGlobalMethod(string name, MethodAttributes attributes, Type? returnType, Type[]? parameterTypes) => DefineGlobalMethod(name, attributes, CallingConventions.Standard, returnType, null, null, parameterTypes, null, null); @@ -15,6 +29,26 @@ public MethodBuilder DefineGlobalMethod(string name, MethodAttributes attributes Type? returnType, Type[]? parameterTypes) => DefineGlobalMethod(name, attributes, callingConvention, returnType, null, null, parameterTypes, null, null); + public MethodBuilder DefineGlobalMethod(string name, MethodAttributes attributes, CallingConventions callingConvention, + Type? returnType, Type[]? requiredReturnTypeCustomModifiers, Type[]? optionalReturnTypeCustomModifiers, + Type[]? parameterTypes, Type[][]? requiredParameterTypeCustomModifiers, Type[][]? optionalParameterTypeCustomModifiers) + { + ArgumentException.ThrowIfNullOrEmpty(name); + + return DefineGlobalMethodCore(name, attributes, callingConvention, + returnType, requiredReturnTypeCustomModifiers, optionalReturnTypeCustomModifiers, + parameterTypes, requiredParameterTypeCustomModifiers, optionalParameterTypeCustomModifiers); + } + + protected abstract MethodBuilder DefineGlobalMethodCore(string name, MethodAttributes attributes, CallingConventions callingConvention, + Type? returnType, Type[]? requiredReturnTypeCustomModifiers, Type[]? optionalReturnTypeCustomModifiers, + Type[]? parameterTypes, Type[][]? requiredParameterTypeCustomModifiers, Type[][]? optionalParameterTypeCustomModifiers); + + public FieldBuilder DefineInitializedData(string name, byte[] data, FieldAttributes attributes) + => DefineInitializedDataCore(name, data, attributes); + + protected abstract FieldBuilder DefineInitializedDataCore(string name, byte[] data, FieldAttributes attributes); + [RequiresUnreferencedCode("P/Invoke marshalling may dynamically access members that could be trimmed.")] public MethodBuilder DefinePInvokeMethod(string name, string dllName, MethodAttributes attributes, CallingConventions callingConvention, @@ -22,6 +56,18 @@ public MethodBuilder DefineGlobalMethod(string name, MethodAttributes attributes => DefinePInvokeMethod(name, dllName, name, attributes, callingConvention, returnType, parameterTypes, nativeCallConv, nativeCharSet); + [RequiresUnreferencedCode("P/Invoke marshalling may dynamically access members that could be trimmed.")] + public MethodBuilder DefinePInvokeMethod(string name, string dllName, string entryName, + MethodAttributes attributes, CallingConventions callingConvention, + Type? returnType, Type[]? parameterTypes, CallingConvention nativeCallConv, CharSet nativeCharSet) + => DefinePInvokeMethodCore(name, dllName, entryName, attributes, callingConvention, + returnType, parameterTypes, nativeCallConv, nativeCharSet); + + [RequiresUnreferencedCode("P/Invoke marshalling may dynamically access members that could be trimmed.")] + protected abstract MethodBuilder DefinePInvokeMethodCore(string name, string dllName, string entryName, + MethodAttributes attributes, CallingConventions callingConvention, + Type? returnType, Type[]? parameterTypes, CallingConvention nativeCallConv, CharSet nativeCharSet); + public TypeBuilder DefineType(string name) => DefineType(name, TypeAttributes.NotPublic, null, null); @@ -32,6 +78,10 @@ public TypeBuilder DefineType(string name, TypeAttributes attr) [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type? parent) => DefineType(name, attr, parent, null); + public TypeBuilder DefineType(string name, TypeAttributes attr, + [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type? parent, Type[]? interfaces) + => DefineTypeCore(name, attr, parent, interfaces, PackingSize.Unspecified, TypeBuilder.UnspecifiedTypeSize); + public TypeBuilder DefineType(string name, TypeAttributes attr, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type? parent, int typesize) => DefineType(name, attr, parent, PackingSize.Unspecified, typesize); @@ -39,5 +89,55 @@ public TypeBuilder DefineType(string name, TypeAttributes attr) public TypeBuilder DefineType(string name, TypeAttributes attr, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type? parent, PackingSize packsize) => DefineType(name, attr, parent, packsize, TypeBuilder.UnspecifiedTypeSize); + + public TypeBuilder DefineType(string name, TypeAttributes attr, + [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type? parent, PackingSize packingSize, int typesize) + => DefineTypeCore(name, attr, parent, null, packingSize, typesize); + + protected abstract TypeBuilder DefineTypeCore(string name, TypeAttributes attr, + [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type? parent, Type[]? interfaces, PackingSize packingSize, int typesize); + + public FieldBuilder DefineUninitializedData(string name, int size, FieldAttributes attributes) + => DefineUninitializedDataCore(name, size, attributes); + + protected abstract FieldBuilder DefineUninitializedDataCore(string name, int size, FieldAttributes attributes); + + public MethodInfo GetArrayMethod(Type arrayClass, string methodName, CallingConventions callingConvention, + Type? returnType, Type[]? parameterTypes) + { + ArgumentNullException.ThrowIfNull(arrayClass); + ArgumentException.ThrowIfNullOrEmpty(methodName); + + return GetArrayMethodCore(arrayClass, methodName, callingConvention, returnType, parameterTypes); + } + + protected abstract MethodInfo GetArrayMethodCore(Type arrayClass, string methodName, + CallingConventions callingConvention, Type? returnType, Type[]? parameterTypes); + + public void SetCustomAttribute(ConstructorInfo con, byte[] binaryAttribute) + { + ArgumentNullException.ThrowIfNull(con); + ArgumentNullException.ThrowIfNull(binaryAttribute); + + SetCustomAttributeCore(con, binaryAttribute); + } + + protected abstract void SetCustomAttributeCore(ConstructorInfo con, byte[] binaryAttribute); + + public void SetCustomAttribute(CustomAttributeBuilder customBuilder) + { + ArgumentNullException.ThrowIfNull(customBuilder); + + SetCustomAttributeCore(customBuilder); + } + + protected abstract void SetCustomAttributeCore(CustomAttributeBuilder customBuilder); + + public abstract int GetTypeMetadataToken(Type type); + public abstract int GetFieldMetadataToken(FieldInfo field); + public abstract int GetMethodMetadataToken(MethodInfo method); + public abstract int GetMethodMetadataToken(ConstructorInfo constructor); + public abstract int GetSignatureMetadataToken(SignatureHelper signature); + public abstract int GetStringMetadataToken(string stringConstant); } } diff --git a/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/PropertyBuilder.cs b/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/PropertyBuilder.cs new file mode 100644 index 0000000000000..924c40f948a12 --- /dev/null +++ b/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/PropertyBuilder.cs @@ -0,0 +1,50 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +namespace System.Reflection.Emit +{ + public abstract class PropertyBuilder : PropertyInfo + { + protected PropertyBuilder() + { + } + + public void AddOtherMethod(MethodBuilder mdBuilder) + => AddOtherMethodCore(mdBuilder); + + protected abstract void AddOtherMethodCore(MethodBuilder mdBuilder); + + public void SetConstant(object? defaultValue) + => SetConstantCore(defaultValue); + + protected abstract void SetConstantCore(object? defaultValue); + + public void SetCustomAttribute(ConstructorInfo con, byte[] binaryAttribute) + { + ArgumentNullException.ThrowIfNull(con); + ArgumentNullException.ThrowIfNull(binaryAttribute); + + SetCustomAttributeCore(con, binaryAttribute); + } + + protected abstract void SetCustomAttributeCore(ConstructorInfo con, byte[] binaryAttribute); + + public void SetCustomAttribute(CustomAttributeBuilder customBuilder) + { + ArgumentNullException.ThrowIfNull(customBuilder); + SetCustomAttributeCore(customBuilder); + } + + protected abstract void SetCustomAttributeCore(CustomAttributeBuilder customBuilder); + + public void SetGetMethod(MethodBuilder mdBuilder) + => SetGetMethodCore(mdBuilder); + + protected abstract void SetGetMethodCore(MethodBuilder mdBuilder); + + public void SetSetMethod(MethodBuilder mdBuilder) + => SetSetMethodCore(mdBuilder); + + protected abstract void SetSetMethodCore(MethodBuilder mdBuilder); + } +} diff --git a/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilder.cs b/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilder.cs index 47e6cecab3691..57bd198c65792 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilder.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilder.cs @@ -6,16 +6,101 @@ namespace System.Reflection.Emit { - public partial class TypeBuilder : TypeInfo + public abstract partial class TypeBuilder : TypeInfo { + protected TypeBuilder() + { + } + public const int UnspecifiedTypeSize = 0; + public PackingSize PackingSize + => PackingSizeCore; + + protected abstract PackingSize PackingSizeCore { get; } + + public int Size + => SizeCore; + + protected abstract int SizeCore { get; } + + public void AddInterfaceImplementation([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type interfaceType) + { + ArgumentNullException.ThrowIfNull(interfaceType); + + AddInterfaceImplementationCore(interfaceType); + } + + protected abstract void AddInterfaceImplementationCore([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type interfaceType); + + [return: DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] + public Type CreateType() + => CreateTypeInfo(); + + [return: DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] + public TypeInfo CreateTypeInfo() + => CreateTypeInfoCore(); + + [return: DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] + protected abstract TypeInfo CreateTypeInfoCore(); + public ConstructorBuilder DefineConstructor(MethodAttributes attributes, CallingConventions callingConvention, Type[]? parameterTypes) => DefineConstructor(attributes, callingConvention, parameterTypes, null, null); + public ConstructorBuilder DefineConstructor(MethodAttributes attributes, CallingConventions callingConvention, + Type[]? parameterTypes, Type[][]? requiredCustomModifiers, Type[][]? optionalCustomModifiers) + => DefineConstructorCore(attributes, callingConvention, parameterTypes, requiredCustomModifiers, optionalCustomModifiers); + + protected abstract ConstructorBuilder DefineConstructorCore(MethodAttributes attributes, CallingConventions callingConvention, + Type[]? parameterTypes, Type[][]? requiredCustomModifiers, Type[][]? optionalCustomModifiers); + + public ConstructorBuilder DefineDefaultConstructor(MethodAttributes attributes) + => DefineDefaultConstructorCore(attributes); + + protected abstract ConstructorBuilder DefineDefaultConstructorCore(MethodAttributes attributes); + + public EventBuilder DefineEvent(string name, EventAttributes attributes, Type eventtype) + { + ArgumentException.ThrowIfNullOrEmpty(name); + + if (name[0] == '\0') + throw new ArgumentException(SR.Argument_IllegalName, nameof(name)); + + return DefineEventCore(name, attributes, eventtype); + } + + protected abstract EventBuilder DefineEventCore(string name, EventAttributes attributes, Type eventtype); + public FieldBuilder DefineField(string fieldName, Type type, FieldAttributes attributes) => DefineField(fieldName, type, null, null, attributes); + public FieldBuilder DefineField(string fieldName, Type type, Type[]? requiredCustomModifiers, Type[]? optionalCustomModifiers, + FieldAttributes attributes) + => DefineFieldCore(fieldName, type, requiredCustomModifiers, optionalCustomModifiers, attributes); + + protected abstract FieldBuilder DefineFieldCore(string fieldName, Type type, Type[]? requiredCustomModifiers, Type[]? optionalCustomModifiers, + FieldAttributes attributes); + + public GenericTypeParameterBuilder[] DefineGenericParameters(params string[] names) + { + ArgumentNullException.ThrowIfNull(names); + if (names.Length == 0) + throw new ArgumentException(SR.Arg_EmptyArray, nameof(names)); + + return DefineGenericParametersCore(names); + } + + protected abstract GenericTypeParameterBuilder[] DefineGenericParametersCore(params string[] names); + + public FieldBuilder DefineInitializedData(string name, byte[] data, FieldAttributes attributes) + { + ArgumentNullException.ThrowIfNull(data); + + return DefineInitializedDataCore(name, data, attributes); + } + + protected abstract FieldBuilder DefineInitializedDataCore(string name, byte[] data, FieldAttributes attributes); + public MethodBuilder DefineMethod(string name, MethodAttributes attributes) => DefineMethod(name, attributes, CallingConventions.Standard, null, null); @@ -26,9 +111,42 @@ public MethodBuilder DefineMethod(string name, MethodAttributes attributes, Call Type? returnType, Type[]? parameterTypes) => DefineMethod(name, attributes, callingConvention, returnType, null, null, parameterTypes, null, null); - public MethodBuilder DefineMethod(string name, MethodAttributes attributes, Type returnType, Type[] parameterTypes) + public MethodBuilder DefineMethod(string name, MethodAttributes attributes, Type? returnType, Type[]? parameterTypes) => DefineMethod(name, attributes, CallingConventions.Standard, returnType, parameterTypes); + public MethodBuilder DefineMethod(string name, MethodAttributes attributes, CallingConventions callingConvention, + Type? returnType, Type[]? returnTypeRequiredCustomModifiers, Type[]? returnTypeOptionalCustomModifiers, + Type[]? parameterTypes, Type[][]? parameterTypeRequiredCustomModifiers, Type[][]? parameterTypeOptionalCustomModifiers) + { + ArgumentException.ThrowIfNullOrEmpty(name); + + if (parameterTypes != null) + { + if (parameterTypeOptionalCustomModifiers != null && parameterTypeOptionalCustomModifiers.Length != parameterTypes.Length) + throw new ArgumentException(SR.Format(SR.Argument_MismatchedArrays, nameof(parameterTypeOptionalCustomModifiers), nameof(parameterTypes))); + + if (parameterTypeRequiredCustomModifiers != null && parameterTypeRequiredCustomModifiers.Length != parameterTypes.Length) + throw new ArgumentException(SR.Format(SR.Argument_MismatchedArrays, nameof(parameterTypeRequiredCustomModifiers), nameof(parameterTypes))); + } + + return DefineMethodCore(name, attributes, callingConvention, returnType, returnTypeRequiredCustomModifiers, returnTypeOptionalCustomModifiers, + parameterTypes, parameterTypeRequiredCustomModifiers, parameterTypeOptionalCustomModifiers); + } + + protected abstract MethodBuilder DefineMethodCore(string name, MethodAttributes attributes, CallingConventions callingConvention, + Type? returnType, Type[]? returnTypeRequiredCustomModifiers, Type[]? returnTypeOptionalCustomModifiers, + Type[]? parameterTypes, Type[][]? parameterTypeRequiredCustomModifiers, Type[][]? parameterTypeOptionalCustomModifiers); + + public void DefineMethodOverride(MethodInfo methodInfoBody, MethodInfo methodInfoDeclaration) + { + ArgumentNullException.ThrowIfNull(methodInfoBody); + ArgumentNullException.ThrowIfNull(methodInfoDeclaration); + + DefineMethodOverrideCore(methodInfoBody, methodInfoDeclaration); + } + + protected abstract void DefineMethodOverrideCore(MethodInfo methodInfoBody, MethodInfo methodInfoDeclaration); + public TypeBuilder DefineNestedType(string name) => DefineNestedType(name, TypeAttributes.NestedPrivate, null, null); @@ -39,13 +157,24 @@ public TypeBuilder DefineNestedType(string name, TypeAttributes attr) [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type? parent) => DefineNestedType(name, attr, parent, null); + public TypeBuilder DefineNestedType(string name, TypeAttributes attr, + [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type? parent, Type[]? interfaces) + => DefineNestedTypeCore(name, attr, parent, interfaces, PackingSize.Unspecified, UnspecifiedTypeSize); + + protected abstract TypeBuilder DefineNestedTypeCore(string name, TypeAttributes attr, + [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type? parent, Type[]? interfaces, PackingSize packSize, int typeSize); + public TypeBuilder DefineNestedType(string name, TypeAttributes attr, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type? parent, int typeSize) => DefineNestedType(name, attr, parent, PackingSize.Unspecified, typeSize); public TypeBuilder DefineNestedType(string name, TypeAttributes attr, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type? parent, PackingSize packSize) - => DefineNestedType(name, attr, parent, packSize, TypeBuilder.UnspecifiedTypeSize); + => DefineNestedType(name, attr, parent, packSize, UnspecifiedTypeSize); + + public TypeBuilder DefineNestedType(string name, TypeAttributes attr, + [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type? parent, PackingSize packSize, int typeSize) + => DefineNestedTypeCore(name, attr, parent, null, packSize, typeSize); [RequiresUnreferencedCode("P/Invoke marshalling may dynamically access members that could be trimmed.")] public MethodBuilder DefinePInvokeMethod(string name, string dllName, MethodAttributes attributes, @@ -62,6 +191,30 @@ public TypeBuilder DefineNestedType(string name, TypeAttributes attr) name, dllName, entryName, attributes, callingConvention, returnType, null, null, parameterTypes, null, null, nativeCallConv, nativeCharSet); + [RequiresUnreferencedCode("P/Invoke marshalling may dynamically access members that could be trimmed.")] + public MethodBuilder DefinePInvokeMethod(string name, string dllName, string entryName, MethodAttributes attributes, + CallingConventions callingConvention, + Type? returnType, Type[]? returnTypeRequiredCustomModifiers, Type[]? returnTypeOptionalCustomModifiers, + Type[]? parameterTypes, Type[][]? parameterTypeRequiredCustomModifiers, Type[][]? parameterTypeOptionalCustomModifiers, + CallingConvention nativeCallConv, CharSet nativeCharSet) + { + ArgumentException.ThrowIfNullOrEmpty(name); + ArgumentException.ThrowIfNullOrEmpty(dllName); + ArgumentException.ThrowIfNullOrEmpty(entryName); + + return DefinePInvokeMethodCore(name, dllName, entryName, attributes, callingConvention, + returnType, returnTypeRequiredCustomModifiers, returnTypeOptionalCustomModifiers, + parameterTypes, parameterTypeRequiredCustomModifiers, parameterTypeOptionalCustomModifiers, + nativeCallConv, nativeCharSet); + } + + [RequiresUnreferencedCode("P/Invoke marshalling may dynamically access members that could be trimmed.")] + protected abstract MethodBuilder DefinePInvokeMethodCore(string name, string dllName, string entryName, MethodAttributes attributes, + CallingConventions callingConvention, + Type? returnType, Type[]? returnTypeRequiredCustomModifiers, Type[]? returnTypeOptionalCustomModifiers, + Type[]? parameterTypes, Type[][]? parameterTypeRequiredCustomModifiers, Type[][]? parameterTypeOptionalCustomModifiers, + CallingConvention nativeCallConv, CharSet nativeCharSet); + public PropertyBuilder DefineProperty(string name, PropertyAttributes attributes, Type returnType, Type[]? parameterTypes) => DefineProperty(name, attributes, returnType, null, null, parameterTypes, null, null); @@ -75,5 +228,59 @@ public PropertyBuilder DefineProperty(string name, PropertyAttributes attributes => DefineProperty(name, attributes, default, returnType, returnTypeRequiredCustomModifiers, returnTypeOptionalCustomModifiers, parameterTypes, parameterTypeRequiredCustomModifiers, parameterTypeOptionalCustomModifiers); + + public PropertyBuilder DefineProperty(string name, PropertyAttributes attributes, CallingConventions callingConvention, + Type returnType, Type[]? returnTypeRequiredCustomModifiers, Type[]? returnTypeOptionalCustomModifiers, + Type[]? parameterTypes, Type[][]? parameterTypeRequiredCustomModifiers, Type[][]? parameterTypeOptionalCustomModifiers) + { + ArgumentException.ThrowIfNullOrEmpty(name); + + return DefinePropertyCore(name, attributes, callingConvention, + returnType, returnTypeRequiredCustomModifiers, returnTypeOptionalCustomModifiers, + parameterTypes, parameterTypeRequiredCustomModifiers, parameterTypeOptionalCustomModifiers); + } + + protected abstract PropertyBuilder DefinePropertyCore(string name, PropertyAttributes attributes, CallingConventions callingConvention, + Type returnType, Type[]? returnTypeRequiredCustomModifiers, Type[]? returnTypeOptionalCustomModifiers, + Type[]? parameterTypes, Type[][]? parameterTypeRequiredCustomModifiers, Type[][]? parameterTypeOptionalCustomModifiers); + + public ConstructorBuilder DefineTypeInitializer() + => DefineTypeInitializerCore(); + + protected abstract ConstructorBuilder DefineTypeInitializerCore(); + + public FieldBuilder DefineUninitializedData(string name, int size, FieldAttributes attributes) + => DefineUninitializedDataCore(name, size, attributes); + + protected abstract FieldBuilder DefineUninitializedDataCore(string name, int size, FieldAttributes attributes); + + public bool IsCreated() + => IsCreatedCore(); + + protected abstract bool IsCreatedCore(); + + public void SetCustomAttribute(ConstructorInfo con, byte[] binaryAttribute) + { + ArgumentNullException.ThrowIfNull(con); + ArgumentNullException.ThrowIfNull(binaryAttribute); + + SetCustomAttributeCore(con, binaryAttribute); + } + + protected abstract void SetCustomAttributeCore(ConstructorInfo con, byte[] binaryAttribute); + + public void SetCustomAttribute(CustomAttributeBuilder customBuilder) + { + ArgumentNullException.ThrowIfNull(customBuilder); + + SetCustomAttributeCore(customBuilder); + } + + protected abstract void SetCustomAttributeCore(CustomAttributeBuilder customBuilder); + + public void SetParent([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type? parent) + => SetParentCore(parent); + + protected abstract void SetParentCore([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type? parent); } } diff --git a/src/libraries/System.Private.CoreLib/src/System/Runtime/CompilerServices/QCallHandles.cs b/src/libraries/System.Private.CoreLib/src/System/Runtime/CompilerServices/QCallHandles.cs index e3020176fa2be..7bd401ef58f75 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Runtime/CompilerServices/QCallHandles.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Runtime/CompilerServices/QCallHandles.cs @@ -57,7 +57,7 @@ internal QCallModule(ref System.Reflection.RuntimeModule module) _module = module.GetUnderlyingNativeHandle(); } - internal QCallModule(ref System.Reflection.Emit.ModuleBuilder module) + internal QCallModule(ref System.Reflection.Emit.RuntimeModuleBuilder module) { _ptr = Unsafe.AsPointer(ref module); _module = module.InternalModule.GetUnderlyingNativeHandle(); 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 3a5a48295e209..14b8c7b36d9c1 100644 --- a/src/libraries/System.Reflection.Emit/ref/System.Reflection.Emit.cs +++ b/src/libraries/System.Reflection.Emit/ref/System.Reflection.Emit.cs @@ -6,9 +6,9 @@ namespace System.Reflection.Emit { - public sealed partial class AssemblyBuilder : System.Reflection.Assembly + public abstract partial class AssemblyBuilder : System.Reflection.Assembly { - internal AssemblyBuilder() { } + protected AssemblyBuilder() { } [System.ObsoleteAttribute("Assembly.CodeBase and Assembly.EscapedCodeBase are only included for .NET Framework compatibility. Use Assembly.Location instead.", DiagnosticId = "SYSLIB0012", UrlFormat = "https://aka.ms/dotnet-warnings/{0}")] [System.Diagnostics.CodeAnalysis.RequiresAssemblyFilesAttribute("This member throws an exception for assemblies embedded in a single-file app")] public override string? CodeBase { get { throw null; } } @@ -25,11 +25,13 @@ public sealed partial class AssemblyBuilder : System.Reflection.Assembly [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 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; } public override object[] GetCustomAttributes(bool inherit) { throw null; } public override object[] GetCustomAttributes(System.Type attributeType, bool inherit) { throw null; } public override System.Collections.Generic.IList GetCustomAttributesData() { throw null; } public System.Reflection.Emit.ModuleBuilder? GetDynamicModule(string name) { throw null; } + protected abstract System.Reflection.Emit.ModuleBuilder? GetDynamicModuleCore(string name); [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("Types might be removed")] public override System.Type[] GetExportedTypes() { throw null; } [System.Diagnostics.CodeAnalysis.RequiresAssemblyFilesAttribute("This member throws an exception for assemblies embedded in a single-file app")] @@ -53,7 +55,9 @@ public sealed partial class AssemblyBuilder : System.Reflection.Assembly 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 SetCustomAttribute(System.Reflection.ConstructorInfo con, byte[] binaryAttribute) { } + protected abstract void SetCustomAttributeCore(System.Reflection.ConstructorInfo con, byte[] binaryAttribute); public void SetCustomAttribute(System.Reflection.Emit.CustomAttributeBuilder customBuilder) { } + protected abstract void SetCustomAttributeCore(System.Reflection.Emit.CustomAttributeBuilder customBuilder); } [System.FlagsAttribute] public enum AssemblyBuilderAccess @@ -61,36 +65,42 @@ public enum AssemblyBuilderAccess Run = 1, RunAndCollect = 9, } - public sealed partial class ConstructorBuilder : System.Reflection.ConstructorInfo + public abstract partial class ConstructorBuilder : System.Reflection.ConstructorInfo { - internal ConstructorBuilder() { } + protected ConstructorBuilder() { } public override System.Reflection.MethodAttributes Attributes { get { throw null; } } public override System.Reflection.CallingConventions CallingConvention { get { throw null; } } public override System.Type? DeclaringType { get { throw null; } } public bool InitLocals { get { throw null; } set { } } + protected abstract bool InitLocalsCore { get; set; } public override int MetadataToken { get { throw null; } } public override System.RuntimeMethodHandle MethodHandle { get { throw null; } } public override System.Reflection.Module Module { get { throw null; } } public override string Name { get { throw null; } } public override System.Type? ReflectedType { get { throw null; } } public System.Reflection.Emit.ParameterBuilder DefineParameter(int iSequence, System.Reflection.ParameterAttributes attributes, string? strParamName) { throw null; } + protected abstract System.Reflection.Emit.ParameterBuilder DefineParameterCore(int iSequence, System.Reflection.ParameterAttributes attributes, string strParamName); public override object[] GetCustomAttributes(bool inherit) { throw null; } public override object[] GetCustomAttributes(System.Type attributeType, bool inherit) { throw null; } public System.Reflection.Emit.ILGenerator GetILGenerator() { throw null; } public System.Reflection.Emit.ILGenerator GetILGenerator(int streamSize) { throw null; } + protected abstract System.Reflection.Emit.ILGenerator GetILGeneratorCore(int streamSize); public override System.Reflection.MethodImplAttributes GetMethodImplementationFlags() { throw null; } public override System.Reflection.ParameterInfo[] GetParameters() { throw null; } public override object Invoke(object? obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder? binder, object?[]? parameters, System.Globalization.CultureInfo? culture) { throw null; } public override object Invoke(System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder? binder, object?[]? parameters, System.Globalization.CultureInfo? culture) { throw null; } public override bool IsDefined(System.Type attributeType, bool inherit) { throw null; } public void SetCustomAttribute(System.Reflection.ConstructorInfo con, byte[] binaryAttribute) { } + protected abstract void SetCustomAttributeCore(System.Reflection.ConstructorInfo con, byte[] binaryAttribute); public void SetCustomAttribute(System.Reflection.Emit.CustomAttributeBuilder customBuilder) { } + protected abstract void SetCustomAttributeCore(System.Reflection.Emit.CustomAttributeBuilder customBuilder); public void SetImplementationFlags(System.Reflection.MethodImplAttributes attributes) { } + protected abstract void SetImplementationFlagsCore(System.Reflection.MethodImplAttributes attributes); public override string ToString() { throw null; } } - public sealed partial class EnumBuilder : System.Reflection.TypeInfo + public abstract partial class EnumBuilder : System.Reflection.TypeInfo { - internal EnumBuilder() { } + protected EnumBuilder() { } public override System.Reflection.Assembly Assembly { get { throw null; } } public override string? AssemblyQualifiedName { get { throw null; } } public override System.Type? BaseType { get { throw null; } } @@ -107,12 +117,16 @@ public sealed partial class EnumBuilder : System.Reflection.TypeInfo public override System.Type? ReflectedType { get { throw null; } } public override System.RuntimeTypeHandle TypeHandle { get { throw null; } } public System.Reflection.Emit.FieldBuilder UnderlyingField { get { throw null; } } + protected abstract System.Reflection.Emit.FieldBuilder UnderlyingFieldCore { get; } public override System.Type UnderlyingSystemType { get { throw null; } } [return: System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.All)] public System.Type CreateType() { throw null; } [return: System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.All)] public System.Reflection.TypeInfo CreateTypeInfo() { throw null; } + [return: System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.All)] + protected abstract System.Reflection.TypeInfo CreateTypeInfoCore(); public System.Reflection.Emit.FieldBuilder DefineLiteral(string literalName, object? literalValue) { throw null; } + protected abstract System.Reflection.Emit.FieldBuilder DefineLiteralCore(string literalName, object? literalValue); protected override System.Reflection.TypeAttributes GetAttributeFlagsImpl() { throw null; } [System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.NonPublicConstructors | System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicConstructors)] protected override System.Reflection.ConstructorInfo? GetConstructorImpl(System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder? binder, System.Reflection.CallingConventions callConvention, System.Type[] types, System.Reflection.ParameterModifier[]? modifiers) { throw null; } @@ -172,21 +186,29 @@ public sealed partial class EnumBuilder : System.Reflection.TypeInfo public override System.Type MakeByRefType() { throw null; } public override System.Type MakePointerType() { throw null; } public void SetCustomAttribute(System.Reflection.ConstructorInfo con, byte[] binaryAttribute) { } + protected abstract void SetCustomAttributeCore(System.Reflection.ConstructorInfo con, byte[] binaryAttribute); public void SetCustomAttribute(System.Reflection.Emit.CustomAttributeBuilder customBuilder) { } + protected abstract void SetCustomAttributeCore(System.Reflection.Emit.CustomAttributeBuilder customBuilder); } - public sealed partial class EventBuilder + public abstract partial class EventBuilder { - internal EventBuilder() { } + protected EventBuilder() { } public void AddOtherMethod(System.Reflection.Emit.MethodBuilder mdBuilder) { } + protected abstract void AddOtherMethodCore(System.Reflection.Emit.MethodBuilder mdBuilder); public void SetAddOnMethod(System.Reflection.Emit.MethodBuilder mdBuilder) { } + protected abstract void SetAddOnMethodCore(System.Reflection.Emit.MethodBuilder mdBuilder); public void SetCustomAttribute(System.Reflection.ConstructorInfo con, byte[] binaryAttribute) { } + protected abstract void SetCustomAttributeCore(System.Reflection.ConstructorInfo con, byte[] binaryAttribute); public void SetCustomAttribute(System.Reflection.Emit.CustomAttributeBuilder customBuilder) { } + protected abstract void SetCustomAttributeCore(System.Reflection.Emit.CustomAttributeBuilder customBuilder); public void SetRaiseMethod(System.Reflection.Emit.MethodBuilder mdBuilder) { } + protected abstract void SetRaiseMethodCore(System.Reflection.Emit.MethodBuilder mdBuilder); public void SetRemoveOnMethod(System.Reflection.Emit.MethodBuilder mdBuilder) { } + protected abstract void SetRemoveOnMethodCore(System.Reflection.Emit.MethodBuilder mdBuilder); } - public sealed partial class FieldBuilder : System.Reflection.FieldInfo + public abstract partial class FieldBuilder : System.Reflection.FieldInfo { - internal FieldBuilder() { } + protected FieldBuilder() { } public override System.Reflection.FieldAttributes Attributes { get { throw null; } } public override System.Type? DeclaringType { get { throw null; } } public override System.RuntimeFieldHandle FieldHandle { get { throw null; } } @@ -200,14 +222,18 @@ public sealed partial class FieldBuilder : System.Reflection.FieldInfo public override object? GetValue(object? obj) { throw null; } public override bool IsDefined(System.Type attributeType, bool inherit) { throw null; } public void SetConstant(object? defaultValue) { } + protected abstract void SetConstantCore(object? defaultValue); public void SetCustomAttribute(System.Reflection.ConstructorInfo con, byte[] binaryAttribute) { } + protected abstract void SetCustomAttributeCore(System.Reflection.ConstructorInfo con, byte[] binaryAttribute); public void SetCustomAttribute(System.Reflection.Emit.CustomAttributeBuilder customBuilder) { } + protected abstract void SetCustomAttributeCore(System.Reflection.Emit.CustomAttributeBuilder customBuilder); public void SetOffset(int iOffset) { } + protected abstract void SetOffsetCore(int iOffset); public override void SetValue(object? obj, object? val, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder? binder, System.Globalization.CultureInfo? culture) { } } - public sealed partial class GenericTypeParameterBuilder : System.Reflection.TypeInfo + public abstract partial class GenericTypeParameterBuilder : System.Reflection.TypeInfo { - internal GenericTypeParameterBuilder() { } + protected GenericTypeParameterBuilder() { } public override System.Reflection.Assembly Assembly { get { throw null; } } public override string? AssemblyQualifiedName { get { throw null; } } public override System.Type? BaseType { get { throw null; } } @@ -299,20 +325,26 @@ public sealed partial class GenericTypeParameterBuilder : System.Reflection.Type public override System.Type MakeGenericType(params System.Type[] typeArguments) { throw null; } public override System.Type MakePointerType() { throw null; } public void SetBaseTypeConstraint([System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.All)] System.Type? baseTypeConstraint) { } + protected abstract void SetBaseTypeConstraintCore([System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.All)] Type? baseTypeConstraint); public void SetCustomAttribute(System.Reflection.ConstructorInfo con, byte[] binaryAttribute) { } + protected abstract void SetCustomAttributeCore(System.Reflection.ConstructorInfo con, byte[] binaryAttribute); public void SetCustomAttribute(System.Reflection.Emit.CustomAttributeBuilder customBuilder) { } + protected abstract void SetCustomAttributeCore(System.Reflection.Emit.CustomAttributeBuilder customBuilder); public void SetGenericParameterAttributes(System.Reflection.GenericParameterAttributes genericParameterAttributes) { } + protected abstract void SetGenericParameterAttributesCore(System.Reflection.GenericParameterAttributes genericParameterAttributes); public void SetInterfaceConstraints(params System.Type[]? interfaceConstraints) { } + protected abstract void SetInterfaceConstraintsCore(params System.Type[]? interfaceConstraints); public override string ToString() { throw null; } } - public sealed partial class MethodBuilder : System.Reflection.MethodInfo + public abstract partial class MethodBuilder : System.Reflection.MethodInfo { - internal MethodBuilder() { } + protected MethodBuilder() { } public override System.Reflection.MethodAttributes Attributes { get { throw null; } } public override System.Reflection.CallingConventions CallingConvention { get { throw null; } } public override bool ContainsGenericParameters { get { throw null; } } public override System.Type? DeclaringType { get { throw null; } } public bool InitLocals { get { throw null; } set { } } + protected abstract bool InitLocalsCore { get; set; } public override bool IsGenericMethod { get { throw null; } } public override bool IsGenericMethodDefinition { get { throw null; } } public override bool IsSecurityCritical { get { throw null; } } @@ -327,7 +359,9 @@ public sealed partial class MethodBuilder : System.Reflection.MethodInfo public override System.Type ReturnType { get { throw null; } } public override System.Reflection.ICustomAttributeProvider ReturnTypeCustomAttributes { get { throw null; } } public System.Reflection.Emit.GenericTypeParameterBuilder[] DefineGenericParameters(params string[] names) { throw null; } + protected abstract System.Reflection.Emit.GenericTypeParameterBuilder[] DefineGenericParametersCore(params string[] names); public System.Reflection.Emit.ParameterBuilder DefineParameter(int position, System.Reflection.ParameterAttributes attributes, string? strParamName) { throw null; } + protected abstract System.Reflection.Emit.ParameterBuilder DefineParameterCore(int position, System.Reflection.ParameterAttributes attributes, string? strParamName); public override bool Equals(object? obj) { throw null; } public override System.Reflection.MethodInfo GetBaseDefinition() { throw null; } public override object[] GetCustomAttributes(bool inherit) { throw null; } @@ -337,6 +371,7 @@ public sealed partial class MethodBuilder : System.Reflection.MethodInfo public override int GetHashCode() { throw null; } public System.Reflection.Emit.ILGenerator GetILGenerator() { throw null; } public System.Reflection.Emit.ILGenerator GetILGenerator(int size) { throw null; } + protected abstract System.Reflection.Emit.ILGenerator GetILGeneratorCore(int size); public override System.Reflection.MethodImplAttributes GetMethodImplementationFlags() { throw null; } public override System.Reflection.ParameterInfo[] GetParameters() { throw null; } public override object Invoke(object? obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder? binder, object?[]? parameters, System.Globalization.CultureInfo? culture) { throw null; } @@ -345,16 +380,20 @@ public sealed partial class MethodBuilder : System.Reflection.MethodInfo [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("If some of the generic arguments are annotated (either with DynamicallyAccessedMembersAttribute, or generic constraints), trimming can't validate that the requirements of those annotations are met.")] public override System.Reflection.MethodInfo MakeGenericMethod(params System.Type[] typeArguments) { throw null; } public void SetCustomAttribute(System.Reflection.ConstructorInfo con, byte[] binaryAttribute) { } + protected abstract void SetCustomAttributeCore(System.Reflection.ConstructorInfo con, byte[] binaryAttribute); public void SetCustomAttribute(System.Reflection.Emit.CustomAttributeBuilder customBuilder) { } + protected abstract void SetCustomAttributeCore(System.Reflection.Emit.CustomAttributeBuilder customBuilder); public void SetImplementationFlags(System.Reflection.MethodImplAttributes attributes) { } + protected abstract void SetImplementationFlagsCore(System.Reflection.MethodImplAttributes attributes); public void SetParameters(params System.Type[] parameterTypes) { } public void SetReturnType(System.Type? returnType) { } public void SetSignature(System.Type? returnType, System.Type[]? returnTypeRequiredCustomModifiers, System.Type[]? returnTypeOptionalCustomModifiers, System.Type[]? parameterTypes, System.Type[][]? parameterTypeRequiredCustomModifiers, System.Type[][]? parameterTypeOptionalCustomModifiers) { } + protected abstract void SetSignatureCore(System.Type? returnType, System.Type[]? returnTypeRequiredCustomModifiers, System.Type[]? returnTypeOptionalCustomModifiers, System.Type[]? parameterTypes, System.Type[][]? parameterTypeRequiredCustomModifiers, System.Type[][]? parameterTypeOptionalCustomModifiers); public override string ToString() { throw null; } } - public partial class ModuleBuilder : System.Reflection.Module + public abstract partial class ModuleBuilder : System.Reflection.Module { - internal ModuleBuilder() { } + protected ModuleBuilder() { } public override System.Reflection.Assembly Assembly { get { throw null; } } [System.Diagnostics.CodeAnalysis.RequiresAssemblyFilesAttribute("Returns for modules with no file path")] public override string FullyQualifiedName { get { throw null; } } @@ -365,15 +404,21 @@ public partial class ModuleBuilder : System.Reflection.Module public override string Name { get { throw null; } } public override string ScopeName { get { throw null; } } public void CreateGlobalFunctions() { } + protected abstract void CreateGlobalFunctionsCore(); public System.Reflection.Emit.EnumBuilder DefineEnum(string name, System.Reflection.TypeAttributes visibility, System.Type underlyingType) { throw null; } + protected abstract System.Reflection.Emit.EnumBuilder DefineEnumCore(string name, System.Reflection.TypeAttributes visibility, System.Type underlyingType); public System.Reflection.Emit.MethodBuilder DefineGlobalMethod(string name, System.Reflection.MethodAttributes attributes, System.Reflection.CallingConventions callingConvention, System.Type? returnType, System.Type[]? parameterTypes) { throw null; } public System.Reflection.Emit.MethodBuilder DefineGlobalMethod(string name, System.Reflection.MethodAttributes attributes, System.Reflection.CallingConventions callingConvention, System.Type? returnType, System.Type[]? requiredReturnTypeCustomModifiers, System.Type[]? optionalReturnTypeCustomModifiers, System.Type[]? parameterTypes, System.Type[][]? requiredParameterTypeCustomModifiers, System.Type[][]? optionalParameterTypeCustomModifiers) { throw null; } + protected abstract System.Reflection.Emit.MethodBuilder DefineGlobalMethodCore(string name, System.Reflection.MethodAttributes attributes, System.Reflection.CallingConventions callingConvention, System.Type? returnType, System.Type[]? requiredReturnTypeCustomModifiers, System.Type[]? optionalReturnTypeCustomModifiers, System.Type[]? parameterTypes, System.Type[][]? requiredParameterTypeCustomModifiers, System.Type[][]? optionalParameterTypeCustomModifiers); public System.Reflection.Emit.MethodBuilder DefineGlobalMethod(string name, System.Reflection.MethodAttributes attributes, System.Type? returnType, System.Type[]? parameterTypes) { throw null; } public System.Reflection.Emit.FieldBuilder DefineInitializedData(string name, byte[] data, System.Reflection.FieldAttributes attributes) { throw null; } + protected abstract System.Reflection.Emit.FieldBuilder DefineInitializedDataCore(string name, byte[] data, System.Reflection.FieldAttributes attributes); [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("P/Invoke marshalling may dynamically access members that could be trimmed.")] public System.Reflection.Emit.MethodBuilder DefinePInvokeMethod(string name, string dllName, System.Reflection.MethodAttributes attributes, System.Reflection.CallingConventions callingConvention, System.Type? returnType, System.Type[]? parameterTypes, System.Runtime.InteropServices.CallingConvention nativeCallConv, System.Runtime.InteropServices.CharSet nativeCharSet) { throw null; } [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("P/Invoke marshalling may dynamically access members that could be trimmed.")] public System.Reflection.Emit.MethodBuilder DefinePInvokeMethod(string name, string dllName, string entryName, System.Reflection.MethodAttributes attributes, System.Reflection.CallingConventions callingConvention, System.Type? returnType, System.Type[]? parameterTypes, System.Runtime.InteropServices.CallingConvention nativeCallConv, System.Runtime.InteropServices.CharSet nativeCharSet) { throw null; } + [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("P/Invoke marshalling may dynamically access members that could be trimmed.")] + protected abstract System.Reflection.Emit.MethodBuilder DefinePInvokeMethodCore(string name, string dllName, string entryName, System.Reflection.MethodAttributes attributes, System.Reflection.CallingConventions callingConvention, System.Type? returnType, System.Type[]? parameterTypes, System.Runtime.InteropServices.CallingConvention nativeCallConv, System.Runtime.InteropServices.CharSet nativeCharSet); public System.Reflection.Emit.TypeBuilder DefineType(string name) { throw null; } public System.Reflection.Emit.TypeBuilder DefineType(string name, System.Reflection.TypeAttributes attr) { throw null; } public System.Reflection.Emit.TypeBuilder DefineType(string name, System.Reflection.TypeAttributes attr, [System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.All)] System.Type? parent) { throw null; } @@ -381,9 +426,12 @@ public partial class ModuleBuilder : System.Reflection.Module public System.Reflection.Emit.TypeBuilder DefineType(string name, System.Reflection.TypeAttributes attr, [System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.All)] System.Type? parent, System.Reflection.Emit.PackingSize packsize) { throw null; } public System.Reflection.Emit.TypeBuilder DefineType(string name, System.Reflection.TypeAttributes attr, [System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.All)] System.Type? parent, System.Reflection.Emit.PackingSize packingSize, int typesize) { throw null; } public System.Reflection.Emit.TypeBuilder DefineType(string name, System.Reflection.TypeAttributes attr, [System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.All)] System.Type? parent, System.Type[]? interfaces) { throw null; } + protected abstract System.Reflection.Emit.TypeBuilder DefineTypeCore(string name, System.Reflection.TypeAttributes attr, [System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.All)] System.Type? parent, System.Type[]? interfaces, System.Reflection.Emit.PackingSize packingSize, int typesize); public System.Reflection.Emit.FieldBuilder DefineUninitializedData(string name, int size, System.Reflection.FieldAttributes attributes) { throw null; } + protected abstract System.Reflection.Emit.FieldBuilder DefineUninitializedDataCore(string name, int size, System.Reflection.FieldAttributes attributes); public override bool Equals(object? obj) { throw null; } public System.Reflection.MethodInfo GetArrayMethod(System.Type arrayClass, string methodName, System.Reflection.CallingConventions callingConvention, System.Type? returnType, System.Type[]? parameterTypes) { throw null; } + protected abstract System.Reflection.MethodInfo GetArrayMethodCore(System.Type arrayClass, string methodName, System.Reflection.CallingConventions callingConvention, System.Type? returnType, System.Type[]? parameterTypes); public override object[] GetCustomAttributes(bool inherit) { throw null; } public override object[] GetCustomAttributes(System.Type attributeType, bool inherit) { throw null; } public override System.Collections.Generic.IList GetCustomAttributesData() { throw null; } @@ -391,9 +439,12 @@ public partial class ModuleBuilder : System.Reflection.Module public override System.Reflection.FieldInfo? GetField(string name, System.Reflection.BindingFlags bindingAttr) { throw null; } [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("Fields might be removed")] public override System.Reflection.FieldInfo[] GetFields(System.Reflection.BindingFlags bindingFlags) { throw null; } + public abstract int GetFieldMetadataToken(System.Reflection.FieldInfo field); public override int GetHashCode() { throw null; } [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("Methods might be removed")] protected override System.Reflection.MethodInfo? GetMethodImpl(string name, System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder? binder, System.Reflection.CallingConventions callConvention, System.Type[]? types, System.Reflection.ParameterModifier[]? modifiers) { throw null; } + public abstract int GetMethodMetadataToken(System.Reflection.ConstructorInfo constructor); + public abstract int GetMethodMetadataToken(System.Reflection.MethodInfo method); [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("Methods might be removed")] public override System.Reflection.MethodInfo[] GetMethods(System.Reflection.BindingFlags bindingFlags) { throw null; } public override void GetPEKind(out System.Reflection.PortableExecutableKinds peKind, out System.Reflection.ImageFileMachine machine) { throw null; } @@ -405,6 +456,9 @@ public partial class ModuleBuilder : System.Reflection.Module public override System.Type? GetType(string className, bool throwOnError, bool ignoreCase) { throw null; } [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("Types might be removed")] public override System.Type[] GetTypes() { throw null; } + public abstract int GetTypeMetadataToken(System.Type type); + public abstract int GetSignatureMetadataToken(System.Reflection.Emit.SignatureHelper signature); + public abstract int GetStringMetadataToken(string stringConstant); public override bool IsDefined(System.Type attributeType, bool inherit) { throw null; } public override bool IsResource() { throw null; } [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("Trimming changes metadata tokens")] @@ -420,11 +474,13 @@ public partial class ModuleBuilder : System.Reflection.Module [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("Trimming changes metadata tokens")] public override System.Type ResolveType(int metadataToken, System.Type[]? genericTypeArguments, System.Type[]? genericMethodArguments) { throw null; } public void SetCustomAttribute(System.Reflection.ConstructorInfo con, byte[] binaryAttribute) { } + protected abstract void SetCustomAttributeCore(System.Reflection.ConstructorInfo con, byte[] binaryAttribute); public void SetCustomAttribute(System.Reflection.Emit.CustomAttributeBuilder customBuilder) { } + protected abstract void SetCustomAttributeCore(System.Reflection.Emit.CustomAttributeBuilder customBuilder); } - public sealed partial class PropertyBuilder : System.Reflection.PropertyInfo + public abstract partial class PropertyBuilder : System.Reflection.PropertyInfo { - internal PropertyBuilder() { } + protected PropertyBuilder() { } public override System.Reflection.PropertyAttributes Attributes { get { throw null; } } public override bool CanRead { get { throw null; } } public override bool CanWrite { get { throw null; } } @@ -434,6 +490,7 @@ public sealed partial class PropertyBuilder : System.Reflection.PropertyInfo public override System.Type PropertyType { get { throw null; } } public override System.Type? ReflectedType { get { throw null; } } public void AddOtherMethod(System.Reflection.Emit.MethodBuilder mdBuilder) { } + protected abstract void AddOtherMethodCore(System.Reflection.Emit.MethodBuilder mdBuilder); public override System.Reflection.MethodInfo[] GetAccessors(bool nonPublic) { throw null; } public override object[] GetCustomAttributes(bool inherit) { throw null; } public override object[] GetCustomAttributes(System.Type attributeType, bool inherit) { throw null; } @@ -444,16 +501,21 @@ public sealed partial class PropertyBuilder : System.Reflection.PropertyInfo public override object GetValue(object? obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder? binder, object?[]? index, System.Globalization.CultureInfo? culture) { throw null; } public override bool IsDefined(System.Type attributeType, bool inherit) { throw null; } public void SetConstant(object? defaultValue) { } + protected abstract void SetConstantCore(object? defaultValue); public void SetCustomAttribute(System.Reflection.ConstructorInfo con, byte[] binaryAttribute) { } + protected abstract void SetCustomAttributeCore(System.Reflection.ConstructorInfo con, byte[] binaryAttribute); public void SetCustomAttribute(System.Reflection.Emit.CustomAttributeBuilder customBuilder) { } + protected abstract void SetCustomAttributeCore(System.Reflection.Emit.CustomAttributeBuilder customBuilder); public void SetGetMethod(System.Reflection.Emit.MethodBuilder mdBuilder) { } + protected abstract void SetGetMethodCore(System.Reflection.Emit.MethodBuilder mdBuilder); public void SetSetMethod(System.Reflection.Emit.MethodBuilder mdBuilder) { } + protected abstract void SetSetMethodCore(System.Reflection.Emit.MethodBuilder mdBuilder); public override void SetValue(object? obj, object? value, object?[]? index) { } public override void SetValue(object? obj, object? value, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder? binder, object?[]? index, System.Globalization.CultureInfo? culture) { } } - public sealed partial class TypeBuilder : System.Reflection.TypeInfo + public abstract partial class TypeBuilder : System.Reflection.TypeInfo { - internal TypeBuilder() { } + protected TypeBuilder() { } public const int UnspecifiedTypeSize = 0; public override System.Reflection.Assembly Assembly { get { throw null; } } public override string? AssemblyQualifiedName { get { throw null; } } @@ -479,29 +541,42 @@ public sealed partial class TypeBuilder : System.Reflection.TypeInfo public override string Name { get { throw null; } } public override string? Namespace { get { throw null; } } public System.Reflection.Emit.PackingSize PackingSize { get { throw null; } } + protected abstract System.Reflection.Emit.PackingSize PackingSizeCore { get; } public override System.Type? ReflectedType { get { throw null; } } public int Size { get { throw null; } } + protected abstract int SizeCore { get; } public override System.RuntimeTypeHandle TypeHandle { get { throw null; } } public override System.Type UnderlyingSystemType { get { throw null; } } public void AddInterfaceImplementation([System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.All)] System.Type interfaceType) { } + protected abstract void AddInterfaceImplementationCore([System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.All)] System.Type interfaceType); [return: System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.All)] public System.Type CreateType() { throw null; } [return: System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.All)] public System.Reflection.TypeInfo CreateTypeInfo() { throw null; } + [return: System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.All)] + protected abstract System.Reflection.TypeInfo CreateTypeInfoCore(); public System.Reflection.Emit.ConstructorBuilder DefineConstructor(System.Reflection.MethodAttributes attributes, System.Reflection.CallingConventions callingConvention, System.Type[]? parameterTypes) { throw null; } public System.Reflection.Emit.ConstructorBuilder DefineConstructor(System.Reflection.MethodAttributes attributes, System.Reflection.CallingConventions callingConvention, System.Type[]? parameterTypes, System.Type[][]? requiredCustomModifiers, System.Type[][]? optionalCustomModifiers) { throw null; } + protected abstract System.Reflection.Emit.ConstructorBuilder DefineConstructorCore(System.Reflection.MethodAttributes attributes, System.Reflection.CallingConventions callingConvention, System.Type[]? parameterTypes, System.Type[][]? requiredCustomModifiers, System.Type[][]? optionalCustomModifiers); public System.Reflection.Emit.ConstructorBuilder DefineDefaultConstructor(System.Reflection.MethodAttributes attributes) { throw null; } + protected abstract System.Reflection.Emit.ConstructorBuilder DefineDefaultConstructorCore(System.Reflection.MethodAttributes attributes); public System.Reflection.Emit.EventBuilder DefineEvent(string name, System.Reflection.EventAttributes attributes, System.Type eventtype) { throw null; } + protected abstract System.Reflection.Emit.EventBuilder DefineEventCore(string name, System.Reflection.EventAttributes attributes, System.Type eventtype); public System.Reflection.Emit.FieldBuilder DefineField(string fieldName, System.Type type, System.Reflection.FieldAttributes attributes) { throw null; } public System.Reflection.Emit.FieldBuilder DefineField(string fieldName, System.Type type, System.Type[]? requiredCustomModifiers, System.Type[]? optionalCustomModifiers, System.Reflection.FieldAttributes attributes) { throw null; } + protected abstract System.Reflection.Emit.FieldBuilder DefineFieldCore(string fieldName, System.Type type, System.Type[]? requiredCustomModifiers, System.Type[]? optionalCustomModifiers, System.Reflection.FieldAttributes attributes); public System.Reflection.Emit.GenericTypeParameterBuilder[] DefineGenericParameters(params string[] names) { throw null; } + protected abstract System.Reflection.Emit.GenericTypeParameterBuilder[] DefineGenericParametersCore(params string[] names); public System.Reflection.Emit.FieldBuilder DefineInitializedData(string name, byte[] data, System.Reflection.FieldAttributes attributes) { throw null; } + protected abstract System.Reflection.Emit.FieldBuilder DefineInitializedDataCore(string name, byte[] data, System.Reflection.FieldAttributes attributes); public System.Reflection.Emit.MethodBuilder DefineMethod(string name, System.Reflection.MethodAttributes attributes) { throw null; } public System.Reflection.Emit.MethodBuilder DefineMethod(string name, System.Reflection.MethodAttributes attributes, System.Reflection.CallingConventions callingConvention) { throw null; } public System.Reflection.Emit.MethodBuilder DefineMethod(string name, System.Reflection.MethodAttributes attributes, System.Reflection.CallingConventions callingConvention, System.Type? returnType, System.Type[]? parameterTypes) { throw null; } public System.Reflection.Emit.MethodBuilder DefineMethod(string name, System.Reflection.MethodAttributes attributes, System.Reflection.CallingConventions callingConvention, System.Type? returnType, System.Type[]? returnTypeRequiredCustomModifiers, System.Type[]? returnTypeOptionalCustomModifiers, System.Type[]? parameterTypes, System.Type[][]? parameterTypeRequiredCustomModifiers, System.Type[][]? parameterTypeOptionalCustomModifiers) { throw null; } + protected abstract System.Reflection.Emit.MethodBuilder DefineMethodCore(string name, System.Reflection.MethodAttributes attributes, System.Reflection.CallingConventions callingConvention, System.Type? returnType, System.Type[]? returnTypeRequiredCustomModifiers, System.Type[]? returnTypeOptionalCustomModifiers, System.Type[]? parameterTypes, System.Type[][]? parameterTypeRequiredCustomModifiers, System.Type[][]? parameterTypeOptionalCustomModifiers); public System.Reflection.Emit.MethodBuilder DefineMethod(string name, System.Reflection.MethodAttributes attributes, System.Type? returnType, System.Type[]? parameterTypes) { throw null; } public void DefineMethodOverride(System.Reflection.MethodInfo methodInfoBody, System.Reflection.MethodInfo methodInfoDeclaration) { } + protected abstract void DefineMethodOverrideCore(System.Reflection.MethodInfo methodInfoBody, System.Reflection.MethodInfo methodInfoDeclaration); public System.Reflection.Emit.TypeBuilder DefineNestedType(string name) { throw null; } public System.Reflection.Emit.TypeBuilder DefineNestedType(string name, System.Reflection.TypeAttributes attr) { throw null; } public System.Reflection.Emit.TypeBuilder DefineNestedType(string name, System.Reflection.TypeAttributes attr, [System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.All)] System.Type? parent) { throw null; } @@ -509,18 +584,24 @@ public sealed partial class TypeBuilder : System.Reflection.TypeInfo public System.Reflection.Emit.TypeBuilder DefineNestedType(string name, System.Reflection.TypeAttributes attr, [System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.All)] System.Type? parent, System.Reflection.Emit.PackingSize packSize) { throw null; } public System.Reflection.Emit.TypeBuilder DefineNestedType(string name, System.Reflection.TypeAttributes attr, [System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.All)] System.Type? parent, System.Reflection.Emit.PackingSize packSize, int typeSize) { throw null; } public System.Reflection.Emit.TypeBuilder DefineNestedType(string name, System.Reflection.TypeAttributes attr, [System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.All)] System.Type? parent, System.Type[]? interfaces) { throw null; } + protected abstract System.Reflection.Emit.TypeBuilder DefineNestedTypeCore(string name, System.Reflection.TypeAttributes attr, [System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.All)] System.Type? parent, System.Type[]? interfaces, System.Reflection.Emit.PackingSize packSize, int typeSize); [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("P/Invoke marshalling may dynamically access members that could be trimmed.")] public System.Reflection.Emit.MethodBuilder DefinePInvokeMethod(string name, string dllName, System.Reflection.MethodAttributes attributes, System.Reflection.CallingConventions callingConvention, System.Type? returnType, System.Type[]? parameterTypes, System.Runtime.InteropServices.CallingConvention nativeCallConv, System.Runtime.InteropServices.CharSet nativeCharSet) { throw null; } [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("P/Invoke marshalling may dynamically access members that could be trimmed.")] public System.Reflection.Emit.MethodBuilder DefinePInvokeMethod(string name, string dllName, string entryName, System.Reflection.MethodAttributes attributes, System.Reflection.CallingConventions callingConvention, System.Type? returnType, System.Type[]? parameterTypes, System.Runtime.InteropServices.CallingConvention nativeCallConv, System.Runtime.InteropServices.CharSet nativeCharSet) { throw null; } [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("P/Invoke marshalling may dynamically access members that could be trimmed.")] public System.Reflection.Emit.MethodBuilder DefinePInvokeMethod(string name, string dllName, string entryName, System.Reflection.MethodAttributes attributes, System.Reflection.CallingConventions callingConvention, System.Type? returnType, System.Type[]? returnTypeRequiredCustomModifiers, System.Type[]? returnTypeOptionalCustomModifiers, System.Type[]? parameterTypes, System.Type[][]? parameterTypeRequiredCustomModifiers, System.Type[][]? parameterTypeOptionalCustomModifiers, System.Runtime.InteropServices.CallingConvention nativeCallConv, System.Runtime.InteropServices.CharSet nativeCharSet) { throw null; } + [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("P/Invoke marshalling may dynamically access members that could be trimmed.")] + protected abstract System.Reflection.Emit.MethodBuilder DefinePInvokeMethodCore(string name, string dllName, string entryName, System.Reflection.MethodAttributes attributes, System.Reflection.CallingConventions callingConvention, System.Type? returnType, System.Type[]? returnTypeRequiredCustomModifiers, System.Type[]? returnTypeOptionalCustomModifiers, System.Type[]? parameterTypes, System.Type[][]? parameterTypeRequiredCustomModifiers, System.Type[][]? parameterTypeOptionalCustomModifiers, System.Runtime.InteropServices.CallingConvention nativeCallConv, System.Runtime.InteropServices.CharSet nativeCharSet); public System.Reflection.Emit.PropertyBuilder DefineProperty(string name, System.Reflection.PropertyAttributes attributes, System.Reflection.CallingConventions callingConvention, System.Type returnType, System.Type[]? parameterTypes) { throw null; } public System.Reflection.Emit.PropertyBuilder DefineProperty(string name, System.Reflection.PropertyAttributes attributes, System.Reflection.CallingConventions callingConvention, System.Type returnType, System.Type[]? returnTypeRequiredCustomModifiers, System.Type[]? returnTypeOptionalCustomModifiers, System.Type[]? parameterTypes, System.Type[][]? parameterTypeRequiredCustomModifiers, System.Type[][]? parameterTypeOptionalCustomModifiers) { throw null; } public System.Reflection.Emit.PropertyBuilder DefineProperty(string name, System.Reflection.PropertyAttributes attributes, System.Type returnType, System.Type[]? parameterTypes) { throw null; } public System.Reflection.Emit.PropertyBuilder DefineProperty(string name, System.Reflection.PropertyAttributes attributes, System.Type returnType, System.Type[]? returnTypeRequiredCustomModifiers, System.Type[]? returnTypeOptionalCustomModifiers, System.Type[]? parameterTypes, System.Type[][]? parameterTypeRequiredCustomModifiers, System.Type[][]? parameterTypeOptionalCustomModifiers) { throw null; } + protected abstract System.Reflection.Emit.PropertyBuilder DefinePropertyCore(string name, System.Reflection.PropertyAttributes attributes, System.Reflection.CallingConventions callingConvention, System.Type returnType, System.Type[]? returnTypeRequiredCustomModifiers, System.Type[]? returnTypeOptionalCustomModifiers, System.Type[]? parameterTypes, System.Type[][]? parameterTypeRequiredCustomModifiers, System.Type[][]? parameterTypeOptionalCustomModifiers); public System.Reflection.Emit.ConstructorBuilder DefineTypeInitializer() { throw null; } + protected abstract System.Reflection.Emit.ConstructorBuilder DefineTypeInitializerCore(); public System.Reflection.Emit.FieldBuilder DefineUninitializedData(string name, int size, System.Reflection.FieldAttributes attributes) { throw null; } + protected abstract System.Reflection.Emit.FieldBuilder DefineUninitializedDataCore(string name, int size, System.Reflection.FieldAttributes attributes); protected override System.Reflection.TypeAttributes GetAttributeFlagsImpl() { throw null; } public static System.Reflection.ConstructorInfo GetConstructor(System.Type type, System.Reflection.ConstructorInfo constructor) { throw null; } [System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.NonPublicConstructors | System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicConstructors)] @@ -575,6 +656,7 @@ public sealed partial class TypeBuilder : System.Reflection.TypeInfo protected override bool IsByRefImpl() { throw null; } protected override bool IsCOMObjectImpl() { throw null; } public bool IsCreated() { throw null; } + protected abstract bool IsCreatedCore(); public override bool IsDefined(System.Type attributeType, bool inherit) { throw null; } protected override bool IsPointerImpl() { throw null; } protected override bool IsPrimitiveImpl() { throw null; } @@ -589,8 +671,11 @@ public sealed partial class TypeBuilder : System.Reflection.TypeInfo public override System.Type MakeGenericType(params System.Type[] typeArguments) { throw null; } public override System.Type MakePointerType() { throw null; } public void SetCustomAttribute(System.Reflection.ConstructorInfo con, byte[] binaryAttribute) { } + protected abstract void SetCustomAttributeCore(System.Reflection.ConstructorInfo con, byte[] binaryAttribute); public void SetCustomAttribute(System.Reflection.Emit.CustomAttributeBuilder customBuilder) { } + protected abstract void SetCustomAttributeCore(System.Reflection.Emit.CustomAttributeBuilder customBuilder); public void SetParent([System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.All)] System.Type? parent) { } + protected abstract void SetParentCore([System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.All)] System.Type? parent); public override string ToString() { throw null; } } } diff --git a/src/mono/System.Private.CoreLib/System.Private.CoreLib.csproj b/src/mono/System.Private.CoreLib/System.Private.CoreLib.csproj index 50d60902a91bf..a7dd8ba87c9e9 100644 --- a/src/mono/System.Private.CoreLib/System.Private.CoreLib.csproj +++ b/src/mono/System.Private.CoreLib/System.Private.CoreLib.csproj @@ -136,7 +136,7 @@ $(DefineConstants);FEATURE_MANAGED_ETW_CHANNELS $(DefineConstants);FEATURE_PERFTRACING $(DefineConstants);FEATURE_OBJCMARSHAL - $(DefineConstants);FEATURE_WASM_THREADS + $(DefineConstants);FEATURE_WASM_THREADS $(DefineConstants);FEATURE_WASM_PERFTRACING @@ -148,19 +148,15 @@ - + - - + + - + @@ -223,30 +219,30 @@ - - - - - - - - - + + + + + + + + + + - diff --git a/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/ConstructorOnTypeBuilderInst.cs b/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/ConstructorOnTypeBuilderInst.cs index 860a7b1f90686..ba4ffe2403d50 100644 --- a/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/ConstructorOnTypeBuilderInst.cs +++ b/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/ConstructorOnTypeBuilderInst.cs @@ -123,7 +123,7 @@ public override ParameterInfo[] GetParameters() internal override ParameterInfo[] GetParametersInternal() { ParameterInfo[] res; - if (cb is ConstructorBuilder cbuilder) + if (cb is RuntimeConstructorBuilder cbuilder) { res = new ParameterInfo[cbuilder.parameters!.Length]; for (int i = 0; i < cbuilder.parameters.Length; i++) @@ -147,7 +147,7 @@ internal override ParameterInfo[] GetParametersInternal() internal override Type[] GetParameterTypes() { - if (cb is ConstructorBuilder builder) + if (cb is RuntimeConstructorBuilder builder) { return builder.parameters!; } diff --git a/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/EventOnTypeBuilderInst.cs b/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/EventOnTypeBuilderInst.cs index 1db909be6479e..86daf33c63387 100644 --- a/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/EventOnTypeBuilderInst.cs +++ b/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/EventOnTypeBuilderInst.cs @@ -43,10 +43,10 @@ namespace System.Reflection.Emit internal sealed class EventOnTypeBuilderInst : EventInfo { private TypeBuilderInstantiation instantiation; - private EventBuilder? event_builder; + private RuntimeEventBuilder? event_builder; private EventInfo? event_info; - internal EventOnTypeBuilderInst(TypeBuilderInstantiation instantiation, EventBuilder evt) + internal EventOnTypeBuilderInst(TypeBuilderInstantiation instantiation, RuntimeEventBuilder evt) { this.instantiation = instantiation; this.event_builder = evt; diff --git a/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/ParameterBuilder.Mono.cs b/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/ParameterBuilder.Mono.cs index ee427cb98959b..d634f330a21bb 100644 --- a/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/ParameterBuilder.Mono.cs +++ b/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/ParameterBuilder.Mono.cs @@ -96,7 +96,7 @@ public virtual void SetConstant(object? defaultValue) { if (position > 0) { - TypeBuilder.SetConstantValue(methodb.GetParameterType(position - 1), + RuntimeTypeBuilder.SetConstantValue(methodb.GetParameterType(position - 1), defaultValue, ref defaultValue); } diff --git a/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/AssemblyBuilder.Mono.cs b/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeAssemblyBuilder.Mono.cs similarity index 90% rename from src/mono/System.Private.CoreLib/src/System/Reflection/Emit/AssemblyBuilder.Mono.cs rename to src/mono/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeAssemblyBuilder.Mono.cs index 4b21b7c0d17f8..68449d1a75ab8 100644 --- a/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/AssemblyBuilder.Mono.cs +++ b/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeAssemblyBuilder.Mono.cs @@ -171,8 +171,32 @@ public override int GetHashCode() } } + public partial class AssemblyBuilder + { + [RequiresDynamicCode("Defining a dynamic assembly requires dynamic code.")] + public static AssemblyBuilder DefineDynamicAssembly(AssemblyName name, AssemblyBuilderAccess access) + { + ArgumentNullException.ThrowIfNull(name); + + return new RuntimeAssemblyBuilder(name, access); + } + + [RequiresDynamicCode("Defining a dynamic assembly requires dynamic code.")] + public static AssemblyBuilder DefineDynamicAssembly(AssemblyName name, AssemblyBuilderAccess access, IEnumerable? assemblyAttributes) + { + AssemblyBuilder ab = DefineDynamicAssembly(name, access); + if (assemblyAttributes != null) + { + foreach (CustomAttributeBuilder attr in assemblyAttributes) + ab.SetCustomAttribute(attr); + } + + return ab; + } + } + [StructLayout(LayoutKind.Sequential)] - public sealed partial class AssemblyBuilder : Assembly + internal sealed partial class RuntimeAssemblyBuilder : AssemblyBuilder { // // AssemblyBuilder inherits from Assembly, but the runtime thinks its layout inherits from RuntimeAssembly @@ -182,7 +206,7 @@ public sealed partial class AssemblyBuilder : Assembly private LoaderAllocator? m_keepalive; private UIntPtr dynamic_assembly; /* GC-tracked */ - private ModuleBuilder[] modules; + private RuntimeModuleBuilder[] modules; private string? name; private CustomAttributeBuilder[]? cattrs; private string? version; @@ -193,18 +217,18 @@ public sealed partial class AssemblyBuilder : Assembly #endregion private AssemblyName aname; - private ModuleBuilder manifest_module; + private RuntimeModuleBuilder manifest_module; private bool manifest_module_used; [MethodImplAttribute(MethodImplOptions.InternalCall)] - [DynamicDependency("RuntimeResolve", typeof(ModuleBuilder))] - private static extern void basic_init(AssemblyBuilder ab); + [DynamicDependency("RuntimeResolve", typeof(RuntimeModuleBuilder))] + private static extern void basic_init(RuntimeAssemblyBuilder ab); [MethodImplAttribute(MethodImplOptions.InternalCall)] - private static extern void UpdateNativeCustomAttributes(AssemblyBuilder ab); + private static extern void UpdateNativeCustomAttributes(RuntimeAssemblyBuilder ab); [DynamicDependency(nameof(access))] // Automatically keeps all previous fields too due to StructLayout - private AssemblyBuilder(AssemblyName n, AssemblyBuilderAccess access) + internal RuntimeAssemblyBuilder(AssemblyName n, AssemblyBuilderAccess access) { EnsureDynamicCodeSupported(); @@ -231,8 +255,8 @@ private AssemblyBuilder(AssemblyName n, AssemblyBuilderAccess access) basic_init(this); // Netcore only allows one module per assembly - manifest_module = new ModuleBuilder(this, "RefEmit_InMemoryManifestModule"); - modules = new ModuleBuilder[] { manifest_module }; + manifest_module = new RuntimeModuleBuilder(this, "RefEmit_InMemoryManifestModule"); + modules = new RuntimeModuleBuilder[] { manifest_module }; } public override bool ReflectionOnly @@ -240,33 +264,8 @@ public override bool ReflectionOnly get { return base.ReflectionOnly; } } - [RequiresDynamicCode("Defining a dynamic assembly requires dynamic code.")] - public static AssemblyBuilder DefineDynamicAssembly(AssemblyName name, AssemblyBuilderAccess access) - { - ArgumentNullException.ThrowIfNull(name); - - return new AssemblyBuilder(name, access); - } - - [RequiresDynamicCode("Defining a dynamic assembly requires dynamic code.")] - public static AssemblyBuilder DefineDynamicAssembly(AssemblyName name, AssemblyBuilderAccess access, IEnumerable? assemblyAttributes) - { - AssemblyBuilder ab = DefineDynamicAssembly(name, access); - if (assemblyAttributes != null) - { - foreach (CustomAttributeBuilder attr in assemblyAttributes) - ab.SetCustomAttribute(attr); - } - - return ab; - } - - public ModuleBuilder DefineDynamicModule(string name) + protected override ModuleBuilder DefineDynamicModuleCore(string name) { - ArgumentException.ThrowIfNullOrEmpty(name); - if (name[0] == '\0') - throw new ArgumentException(SR.Argument_InvalidName, nameof(name)); - if (manifest_module_used) throw new InvalidOperationException(SR.InvalidOperation_NoMultiModuleAssembly); manifest_module_used = true; @@ -276,18 +275,14 @@ public ModuleBuilder DefineDynamicModule(string name) internal static AssemblyBuilder InternalDefineDynamicAssembly( AssemblyName name, AssemblyBuilderAccess access, - Assembly? _ /*callingAssembly*/, - AssemblyLoadContext? assemblyLoadContext, + AssemblyLoadContext? _, IEnumerable? assemblyAttributes) { - Debug.Assert(assemblyLoadContext is null); return DefineDynamicAssembly(name, access, assemblyAttributes); } - public ModuleBuilder? GetDynamicModule(string name) + protected override ModuleBuilder? GetDynamicModuleCore(string name) { - ArgumentException.ThrowIfNullOrEmpty(name); - if (modules != null) for (int i = 0; i < modules.Length; ++i) if (modules[i].name == name) @@ -298,10 +293,8 @@ public ModuleBuilder DefineDynamicModule(string name) public override bool IsCollectible => access == (uint)AssemblyBuilderAccess.RunAndCollect; - public void SetCustomAttribute(CustomAttributeBuilder customBuilder) + protected override void SetCustomAttributeCore(CustomAttributeBuilder customBuilder) { - ArgumentNullException.ThrowIfNull(customBuilder); - if (cattrs != null) { CustomAttributeBuilder[] new_array = new CustomAttributeBuilder[cattrs.Length + 1]; @@ -318,12 +311,9 @@ public void SetCustomAttribute(CustomAttributeBuilder customBuilder) UpdateNativeCustomAttributes(this); } - public void SetCustomAttribute(ConstructorInfo con, byte[] binaryAttribute) + protected override void SetCustomAttributeCore(ConstructorInfo con, byte[] binaryAttribute) { - ArgumentNullException.ThrowIfNull(con); - ArgumentNullException.ThrowIfNull(binaryAttribute); - - SetCustomAttribute(new CustomAttributeBuilder(con, binaryAttribute)); + SetCustomAttributeCore(new CustomAttributeBuilder(con, binaryAttribute)); } /*Warning, @typeArguments must be a mscorlib internal array. So make a copy before passing it in*/ diff --git a/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/ConstructorBuilder.Mono.cs b/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeConstructorBuilder.Mono.cs similarity index 88% rename from src/mono/System.Private.CoreLib/src/System/Reflection/Emit/ConstructorBuilder.Mono.cs rename to src/mono/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeConstructorBuilder.Mono.cs index 76d0b505143be..97c17e165863f 100644 --- a/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/ConstructorBuilder.Mono.cs +++ b/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeConstructorBuilder.Mono.cs @@ -41,7 +41,7 @@ namespace System.Reflection.Emit { [StructLayout(LayoutKind.Sequential)] - public sealed partial class ConstructorBuilder : ConstructorInfo + internal sealed partial class RuntimeConstructorBuilder : ConstructorBuilder { #region Sync with MonoReflectionCtorBuilder in object-internals.h private RuntimeMethodHandle mhandle; @@ -51,7 +51,7 @@ public sealed partial class ConstructorBuilder : ConstructorInfo private MethodImplAttributes iattrs; private int table_idx; private CallingConventions call_conv; - private TypeBuilder type; + private RuntimeTypeBuilder type; internal ParameterBuilder[]? pinfo; private CustomAttributeBuilder[]? cattrs; private bool init_locals = true; @@ -62,7 +62,7 @@ public sealed partial class ConstructorBuilder : ConstructorInfo internal bool finished; [DynamicDependency(nameof(paramModOpt))] // Automatically keeps all previous fields too due to StructLayout - internal ConstructorBuilder(TypeBuilder tb, MethodAttributes attributes, CallingConventions callingConvention, Type[]? parameterTypes, Type[][]? paramModReq, Type[][]? paramModOpt) + internal RuntimeConstructorBuilder(RuntimeTypeBuilder tb, MethodAttributes attributes, CallingConventions callingConvention, Type[]? parameterTypes, Type[][]? paramModReq, Type[][]? paramModOpt) { attrs = attributes | MethodAttributes.SpecialName | MethodAttributes.RTSpecialName; call_conv = callingConvention; @@ -80,7 +80,7 @@ internal ConstructorBuilder(TypeBuilder tb, MethodAttributes attributes, Calling this.paramModOpt = paramModOpt; table_idx = get_next_table_index(0x06, 1); - ((ModuleBuilder)tb.Module).RegisterToken(this, MetadataToken); + ((RuntimeModuleBuilder)tb.Module).RegisterToken(this, MetadataToken); } // FIXME: @@ -92,7 +92,7 @@ public override CallingConventions CallingConvention } } - public bool InitLocals + protected override bool InitLocalsCore { get { @@ -104,7 +104,7 @@ public bool InitLocals } } - internal TypeBuilder TypeBuilder + internal RuntimeTypeBuilder TypeBuilder { get { @@ -209,7 +209,7 @@ public override string Name } } - public ParameterBuilder DefineParameter(int iSequence, ParameterAttributes attributes, string? strParamName) + protected override ParameterBuilder DefineParameterCore(int iSequence, ParameterAttributes attributes, string? strParamName) { // The 0th ParameterBuilder does not correspond to an // actual parameter, but .NETFramework lets you define @@ -241,12 +241,7 @@ public override object[] GetCustomAttributes(Type attributeType, bool inherit) throw not_supported(); } - public ILGenerator GetILGenerator() - { - return GetILGenerator(64); - } - - public ILGenerator GetILGenerator(int streamSize) + protected override ILGenerator GetILGeneratorCore(int streamSize) { if (finished) throw new InvalidOperationException(); @@ -254,11 +249,11 @@ public ILGenerator GetILGenerator(int streamSize) return ilgen; if (!(((attrs & (MethodAttributes.Abstract | MethodAttributes.PinvokeImpl)) == 0) && ((iattrs & (MethodImplAttributes.Runtime | MethodImplAttributes.InternalCall)) == 0))) throw new InvalidOperationException(); - ilgen = new ILGenerator(type.Module, ((ModuleBuilder)type.Module).GetTokenGenerator(), streamSize); + ilgen = new ILGenerator(type.Module, ((RuntimeModuleBuilder)type.Module).GetTokenGenerator(), streamSize); return ilgen; } - public void SetCustomAttribute(CustomAttributeBuilder customBuilder) + protected override void SetCustomAttributeCore(CustomAttributeBuilder customBuilder) { ArgumentNullException.ThrowIfNull(customBuilder); @@ -286,15 +281,15 @@ public void SetCustomAttribute(CustomAttributeBuilder customBuilder) } } - public void SetCustomAttribute(ConstructorInfo con, byte[] binaryAttribute) + protected override void SetCustomAttributeCore(ConstructorInfo con, byte[] binaryAttribute) { ArgumentNullException.ThrowIfNull(con); ArgumentNullException.ThrowIfNull(binaryAttribute); - SetCustomAttribute(new CustomAttributeBuilder(con, binaryAttribute)); + SetCustomAttributeCore(new CustomAttributeBuilder(con, binaryAttribute)); } - public void SetImplementationFlags(MethodImplAttributes attributes) + protected override void SetImplementationFlagsCore(MethodImplAttributes attributes) { if (type.is_created) throw not_after_created(); @@ -331,16 +326,16 @@ internal void fixup() internal void ResolveUserTypes() { - TypeBuilder.ResolveUserTypes(parameters); + RuntimeTypeBuilder.ResolveUserTypes(parameters); if (paramModReq != null) { foreach (Type[] types in paramModReq) - TypeBuilder.ResolveUserTypes(types); + RuntimeTypeBuilder.ResolveUserTypes(types); } if (paramModOpt != null) { foreach (Type[] types in paramModOpt) - TypeBuilder.ResolveUserTypes(types); + RuntimeTypeBuilder.ResolveUserTypes(types); } } diff --git a/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/EnumBuilder.Mono.cs b/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeEnumBuilder.Mono.cs similarity index 94% rename from src/mono/System.Private.CoreLib/src/System/Reflection/Emit/EnumBuilder.Mono.cs rename to src/mono/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeEnumBuilder.Mono.cs index 5132cb09718b0..e810b3bc56fa2 100644 --- a/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/EnumBuilder.Mono.cs +++ b/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeEnumBuilder.Mono.cs @@ -41,23 +41,23 @@ namespace System.Reflection.Emit { - public sealed partial class EnumBuilder : TypeInfo + internal sealed partial class RuntimeEnumBuilder : EnumBuilder { [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] - private TypeBuilder _tb; + private RuntimeTypeBuilder _tb; private FieldBuilder _underlyingField; private Type _underlyingType; [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2064:UnrecognizedReflectionPattern", Justification = "Reflection.Emit is not subject to trimming")] - internal EnumBuilder(ModuleBuilder mb, string name, TypeAttributes visibility, Type underlyingType) + internal RuntimeEnumBuilder(RuntimeModuleBuilder mb, string name, TypeAttributes visibility, Type underlyingType) { if ((visibility & ~TypeAttributes.VisibilityMask) != 0) throw new ArgumentException(SR.Argument_ShouldOnlySetVisibilityFlags, nameof(name)); if ((visibility & TypeAttributes.VisibilityMask) >= TypeAttributes.NestedPublic && (visibility & TypeAttributes.VisibilityMask) <= TypeAttributes.NestedFamORAssem) throw new ArgumentException(); - _tb = new TypeBuilder(mb, name, (visibility | TypeAttributes.Sealed), + _tb = new RuntimeTypeBuilder(mb, name, (visibility | TypeAttributes.Sealed), typeof(Enum), null, PackingSize.Unspecified, 0, null); _underlyingType = underlyingType; _underlyingField = _tb.DefineField("value__", underlyingType, @@ -65,7 +65,7 @@ internal EnumBuilder(ModuleBuilder mb, string name, TypeAttributes visibility, T setup_enum_type(_tb); } - internal TypeBuilder GetTypeBuilder() + internal RuntimeTypeBuilder GetTypeBuilder() { return _tb; } @@ -176,7 +176,7 @@ internal int TypeToken } } - public FieldBuilder UnderlyingField + protected override FieldBuilder UnderlyingFieldCore { get { @@ -193,13 +193,7 @@ public override Type UnderlyingSystemType } [return: DynamicallyAccessedMembersAttribute(DynamicallyAccessedMemberTypes.All)] - public Type? CreateType() - { - return _tb.CreateType(); - } - - [return: DynamicallyAccessedMembersAttribute(DynamicallyAccessedMemberTypes.All)] - public TypeInfo? CreateTypeInfo() + protected override TypeInfo CreateTypeInfoCore() { return _tb.CreateTypeInfo(); } @@ -216,7 +210,7 @@ public override Type GetEnumUnderlyingType() [MethodImplAttribute(MethodImplOptions.InternalCall)] private extern void setup_enum_type(Type t); - public FieldBuilder DefineLiteral(string literalName, object? literalValue) + protected override FieldBuilder DefineLiteralCore(string literalName, object? literalValue) { Type fieldType = this; FieldBuilder fieldBuilder = _tb.DefineField(literalName, @@ -454,14 +448,14 @@ public override Type MakePointerType() return new PointerType(this); } - public void SetCustomAttribute(CustomAttributeBuilder customBuilder) + protected override void SetCustomAttributeCore(CustomAttributeBuilder customBuilder) { _tb.SetCustomAttribute(customBuilder); } - public void SetCustomAttribute(ConstructorInfo con, byte[] binaryAttribute) + protected override void SetCustomAttributeCore(ConstructorInfo con, byte[] binaryAttribute) { - SetCustomAttribute(new CustomAttributeBuilder(con, binaryAttribute)); + SetCustomAttributeCore(new CustomAttributeBuilder(con, binaryAttribute)); } internal override bool IsUserType diff --git a/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/EventBuilder.Mono.cs b/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeEventBuilder.Mono.cs similarity index 83% rename from src/mono/System.Private.CoreLib/src/System/Reflection/Emit/EventBuilder.Mono.cs rename to src/mono/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeEventBuilder.Mono.cs index ef7aec4fcd37d..8ff4a3df89eac 100644 --- a/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/EventBuilder.Mono.cs +++ b/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeEventBuilder.Mono.cs @@ -40,12 +40,12 @@ namespace System.Reflection.Emit { [StructLayout(LayoutKind.Sequential)] - public sealed partial class EventBuilder + internal sealed partial class RuntimeEventBuilder : EventBuilder { #region Sync with MonoReflectionEventBuilder in object-internals.h internal string name; private Type type; - private TypeBuilder typeb; + private RuntimeTypeBuilder typeb; private CustomAttributeBuilder[]? cattrs; internal MethodBuilder? add_method; internal MethodBuilder? remove_method; @@ -56,7 +56,7 @@ public sealed partial class EventBuilder #endregion [DynamicDependency(nameof(table_idx))] // Automatically keeps all previous fields too due to StructLayout - internal EventBuilder(TypeBuilder tb, string eventName, EventAttributes eventAttrs, Type eventType) + internal RuntimeEventBuilder(RuntimeTypeBuilder tb, string eventName, EventAttributes eventAttrs, Type eventType) { name = eventName; attrs = eventAttrs; @@ -70,7 +70,7 @@ internal int get_next_table_index(int table, int count) return typeb.get_next_table_index(table, count); } - public void AddOtherMethod(MethodBuilder mdBuilder) + protected override void AddOtherMethodCore(MethodBuilder mdBuilder) { ArgumentNullException.ThrowIfNull(mdBuilder); RejectIfCreated(); @@ -87,28 +87,27 @@ public void AddOtherMethod(MethodBuilder mdBuilder) other_methods[other_methods.Length - 1] = mdBuilder; } - public void SetAddOnMethod(MethodBuilder mdBuilder) + protected override void SetAddOnMethodCore(MethodBuilder mdBuilder) { ArgumentNullException.ThrowIfNull(mdBuilder); RejectIfCreated(); add_method = mdBuilder; } - public void SetRaiseMethod(MethodBuilder mdBuilder) + protected override void SetRaiseMethodCore(MethodBuilder mdBuilder) { ArgumentNullException.ThrowIfNull(mdBuilder); RejectIfCreated(); raise_method = mdBuilder; } - public void SetRemoveOnMethod(MethodBuilder mdBuilder) + protected override void SetRemoveOnMethodCore(MethodBuilder mdBuilder) { ArgumentNullException.ThrowIfNull(mdBuilder); RejectIfCreated(); remove_method = mdBuilder; } - public void SetCustomAttribute(CustomAttributeBuilder customBuilder) + protected override void SetCustomAttributeCore(CustomAttributeBuilder customBuilder) { - ArgumentNullException.ThrowIfNull(customBuilder); RejectIfCreated(); string? attrname = customBuilder.Ctor.ReflectedType!.FullName; if (attrname == "System.Runtime.CompilerServices.SpecialNameAttribute") @@ -130,11 +129,9 @@ public void SetCustomAttribute(CustomAttributeBuilder customBuilder) } } - public void SetCustomAttribute(ConstructorInfo con, byte[] binaryAttribute) + protected override void SetCustomAttributeCore(ConstructorInfo con, byte[] binaryAttribute) { - ArgumentNullException.ThrowIfNull(con); - ArgumentNullException.ThrowIfNull(binaryAttribute); - SetCustomAttribute(new CustomAttributeBuilder(con, binaryAttribute)); + SetCustomAttributeCore(new CustomAttributeBuilder(con, binaryAttribute)); } private void RejectIfCreated() diff --git a/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/FieldBuilder.Mono.cs b/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeFieldBuilder.Mono.cs similarity index 87% rename from src/mono/System.Private.CoreLib/src/System/Reflection/Emit/FieldBuilder.Mono.cs rename to src/mono/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeFieldBuilder.Mono.cs index 09c6414c44616..5e33bb26fcf59 100644 --- a/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/FieldBuilder.Mono.cs +++ b/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeFieldBuilder.Mono.cs @@ -41,7 +41,7 @@ namespace System.Reflection.Emit { [StructLayout(LayoutKind.Sequential)] - public sealed partial class FieldBuilder : FieldInfo + internal sealed partial class RuntimeFieldBuilder : FieldBuilder { #region Sync with MonoReflectionFieldBuilder in object-internals.h private FieldAttributes attrs; @@ -49,7 +49,7 @@ public sealed partial class FieldBuilder : FieldInfo private string name; private object? def_value; private int offset; - internal TypeBuilder typeb; + internal RuntimeTypeBuilder typeb; private byte[]? rva_data; private CustomAttributeBuilder[]? cattrs; private UnmanagedMarshal? marshal_info; @@ -59,7 +59,7 @@ public sealed partial class FieldBuilder : FieldInfo #endregion [DynamicDependency(nameof(modOpt))] // Automatically keeps all previous fields too due to StructLayout - internal FieldBuilder(TypeBuilder tb, string fieldName, Type type, FieldAttributes attributes, Type[]? modReq, Type[]? modOpt) + internal RuntimeFieldBuilder(RuntimeTypeBuilder tb, string fieldName, Type type, FieldAttributes attributes, Type[]? modReq, Type[]? modOpt) { ArgumentNullException.ThrowIfNull(type); @@ -71,7 +71,7 @@ internal FieldBuilder(TypeBuilder tb, string fieldName, Type type, FieldAttribut offset = -1; typeb = tb; - ((ModuleBuilder)tb.Module).RegisterToken(this, MetadataToken); + ((RuntimeModuleBuilder)tb.Module).RegisterToken(this, MetadataToken); } public override FieldAttributes Attributes @@ -133,7 +133,7 @@ public override object[] GetCustomAttributes(Type attributeType, bool inherit) throw CreateNotSupportedException(); } - public override int MetadataToken { get { return ((ModuleBuilder)typeb.Module).GetToken(this); } } + public override int MetadataToken { get { return ((RuntimeModuleBuilder)typeb.Module).GetToken(this); } } public override object? GetValue(object? obj) { @@ -165,7 +165,7 @@ internal static PackingSize RVADataPackingSize(int size) return PackingSize.Size1; } - public void SetConstant(object? defaultValue) + protected override void SetConstantCore(object? defaultValue) { RejectIfCreated(); @@ -174,12 +174,10 @@ public void SetConstant(object? defaultValue) def_value = defaultValue; } - public void SetCustomAttribute(CustomAttributeBuilder customBuilder) + protected override void SetCustomAttributeCore(CustomAttributeBuilder customBuilder) { RejectIfCreated(); - ArgumentNullException.ThrowIfNull(customBuilder); - string? attrname = customBuilder.Ctor.ReflectedType!.FullName; if (attrname == "System.Runtime.InteropServices.FieldOffsetAttribute") { @@ -221,13 +219,13 @@ public void SetCustomAttribute(CustomAttributeBuilder customBuilder) } } - public void SetCustomAttribute(ConstructorInfo con, byte[] binaryAttribute) + protected override void SetCustomAttributeCore(ConstructorInfo con, byte[] binaryAttribute) { RejectIfCreated(); - SetCustomAttribute(new CustomAttributeBuilder(con, binaryAttribute)); + SetCustomAttributeCore(new CustomAttributeBuilder(con, binaryAttribute)); } - public void SetOffset(int iOffset) + protected override void SetOffsetCore(int iOffset) { RejectIfCreated(); if (iOffset < 0) @@ -253,11 +251,11 @@ private void RejectIfCreated() internal void ResolveUserTypes() { - type = TypeBuilder.ResolveUserType(type); - TypeBuilder.ResolveUserTypes(modReq); - TypeBuilder.ResolveUserTypes(modOpt); + type = RuntimeTypeBuilder.ResolveUserType(type); + RuntimeTypeBuilder.ResolveUserTypes(modReq); + RuntimeTypeBuilder.ResolveUserTypes(modOpt); if (marshal_info != null) - marshal_info.marshaltyperef = TypeBuilder.ResolveUserType(marshal_info.marshaltyperef); + marshal_info.marshaltyperef = RuntimeTypeBuilder.ResolveUserType(marshal_info.marshaltyperef); } internal FieldInfo RuntimeResolve() diff --git a/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/GenericTypeParameterBuilder.cs b/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeGenericTypeParameterBuilder.cs similarity index 94% rename from src/mono/System.Private.CoreLib/src/System/Reflection/Emit/GenericTypeParameterBuilder.cs rename to src/mono/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeGenericTypeParameterBuilder.cs index 694a49755b3f6..705662d037649 100644 --- a/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/GenericTypeParameterBuilder.cs +++ b/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeGenericTypeParameterBuilder.cs @@ -39,11 +39,11 @@ namespace System.Reflection.Emit { [ComVisible(true)] [StructLayout(LayoutKind.Sequential)] - public sealed class GenericTypeParameterBuilder : TypeInfo + internal sealed class RuntimeGenericTypeParameterBuilder : GenericTypeParameterBuilder { #region Sync with MonoReflectionGenericParam in object-internals.h - private TypeBuilder tbuilder; - private MethodBuilder? mbuilder; + private RuntimeTypeBuilder tbuilder; + private RuntimeMethodBuilder? mbuilder; private string name; private int index; private Type? base_type; @@ -53,7 +53,7 @@ public sealed class GenericTypeParameterBuilder : TypeInfo #endregion [DynamicDependency(nameof(attrs))] // Automatically keeps all previous fields too due to StructLayout - internal GenericTypeParameterBuilder(TypeBuilder tbuilder, MethodBuilder? mbuilder, string name, int index) + internal RuntimeGenericTypeParameterBuilder(RuntimeTypeBuilder tbuilder, RuntimeMethodBuilder? mbuilder, string name, int index) { this.tbuilder = tbuilder; this.mbuilder = mbuilder; @@ -61,17 +61,17 @@ internal GenericTypeParameterBuilder(TypeBuilder tbuilder, MethodBuilder? mbuild this.index = index; } - public void SetBaseTypeConstraint([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type? baseTypeConstraint) + protected override void SetBaseTypeConstraintCore([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type? baseTypeConstraint) { this.base_type = baseTypeConstraint ?? typeof(object); } - public void SetInterfaceConstraints(params Type[]? interfaceConstraints) + protected override void SetInterfaceConstraintsCore(params Type[]? interfaceConstraints) { this.iface_constraints = interfaceConstraints; } - public void SetGenericParameterAttributes(GenericParameterAttributes genericParameterAttributes) + protected override void SetGenericParameterAttributesCore(GenericParameterAttributes genericParameterAttributes) { this.attrs = genericParameterAttributes; } @@ -424,10 +424,8 @@ public override Type[] GetGenericParameterConstraints() get { return mbuilder; } } - public void SetCustomAttribute(CustomAttributeBuilder customBuilder) + protected override void SetCustomAttributeCore(CustomAttributeBuilder customBuilder) { - ArgumentNullException.ThrowIfNull(customBuilder); - if (cattrs != null) { CustomAttributeBuilder[] new_array = new CustomAttributeBuilder[cattrs.Length + 1]; @@ -443,9 +441,9 @@ public void SetCustomAttribute(CustomAttributeBuilder customBuilder) } // FIXME: "unverified implementation" - public void SetCustomAttribute(ConstructorInfo con, byte[] binaryAttribute) + protected override void SetCustomAttributeCore(ConstructorInfo con, byte[] binaryAttribute) { - SetCustomAttribute(new CustomAttributeBuilder(con, binaryAttribute)); + SetCustomAttributeCore(new CustomAttributeBuilder(con, binaryAttribute)); } private static Exception not_supported() diff --git a/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/MethodBuilder.Mono.cs b/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeMethodBuilder.Mono.cs similarity index 88% rename from src/mono/System.Private.CoreLib/src/System/Reflection/Emit/MethodBuilder.Mono.cs rename to src/mono/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeMethodBuilder.Mono.cs index 3e53858042005..b39b43dc0c190 100644 --- a/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/MethodBuilder.Mono.cs +++ b/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeMethodBuilder.Mono.cs @@ -41,7 +41,7 @@ namespace System.Reflection.Emit { [StructLayout(LayoutKind.Sequential)] - public sealed partial class MethodBuilder : MethodInfo + internal sealed partial class RuntimeMethodBuilder : MethodBuilder { #region Sync with MonoReflectionMethodBuilder in object-internals.h private RuntimeMethodHandle mhandle; @@ -53,7 +53,7 @@ public sealed partial class MethodBuilder : MethodInfo private int table_idx; private byte[]? code; private ILGenerator? ilgen; - private TypeBuilder type; + private RuntimeTypeBuilder type; internal ParameterBuilder[]? pinfo; private CustomAttributeBuilder[]? cattrs; private MethodInfo[]? override_methods; @@ -65,7 +65,7 @@ public sealed partial class MethodBuilder : MethodInfo private CallingConventions call_conv; private bool init_locals = true; private IntPtr generic_container; - internal GenericTypeParameterBuilder[]? generic_params; + internal RuntimeGenericTypeParameterBuilder[]? generic_params; private Type[]? returnModReq; private Type[]? returnModOpt; private Type[][]? paramModReq; @@ -75,7 +75,7 @@ public sealed partial class MethodBuilder : MethodInfo private RuntimeMethodInfo? created; [DynamicDependency(nameof(paramModOpt))] // Automatically keeps all previous fields too due to StructLayout - internal MethodBuilder(TypeBuilder tb, string name, MethodAttributes attributes, CallingConventions callingConvention, Type? returnType, Type[]? returnModReq, Type[]? returnModOpt, Type[]? parameterTypes, Type[][]? paramModReq, Type[][]? paramModOpt) + internal RuntimeMethodBuilder(RuntimeTypeBuilder tb, string name, MethodAttributes attributes, CallingConventions callingConvention, Type? returnType, Type[]? returnModReq, Type[]? returnModOpt, Type[]? parameterTypes, Type[][]? paramModReq, Type[][]? paramModOpt) { this.name = name; this.attrs = attributes; @@ -101,10 +101,10 @@ internal MethodBuilder(TypeBuilder tb, string name, MethodAttributes attributes, type = tb; table_idx = get_next_table_index(0x06, 1); - ((ModuleBuilder)tb.Module).RegisterToken(this, MetadataToken); + ((RuntimeModuleBuilder)tb.Module).RegisterToken(this, MetadataToken); } - internal MethodBuilder(TypeBuilder tb, string name, MethodAttributes attributes, + internal RuntimeMethodBuilder(RuntimeTypeBuilder tb, string name, MethodAttributes attributes, CallingConventions callingConvention, Type? returnType, Type[]? returnModReq, Type[]? returnModOpt, Type[]? parameterTypes, Type[][]? paramModReq, Type[][]? paramModOpt, string dllName, string entryName, CallingConvention nativeCConv, CharSet nativeCharset) : this(tb, name, attributes, callingConvention, returnType, returnModReq, returnModOpt, parameterTypes, paramModReq, paramModOpt) @@ -120,13 +120,13 @@ public override bool ContainsGenericParameters get { throw new NotSupportedException(); } } - public bool InitLocals + protected override bool InitLocalsCore { get { return init_locals; } set { init_locals = value; } } - internal TypeBuilder TypeBuilder + internal RuntimeTypeBuilder TypeBuilder { get { return type; } } @@ -310,12 +310,7 @@ public override object[] GetCustomAttributes(Type attributeType, bool inherit) throw NotSupported(); } - public ILGenerator GetILGenerator() - { - return GetILGenerator(64); - } - - public ILGenerator GetILGenerator(int size) + protected override ILGenerator GetILGeneratorCore(int size) { if (((iattrs & MethodImplAttributes.CodeTypeMask) != MethodImplAttributes.IL) || @@ -324,11 +319,11 @@ public ILGenerator GetILGenerator(int size) throw new InvalidOperationException("Method body should not exist."); if (ilgen != null) return ilgen; - ilgen = new ILGenerator(type.Module, ((ModuleBuilder)type.Module).GetTokenGenerator(), size); + ilgen = new ILGenerator(type.Module, ((RuntimeModuleBuilder)type.Module).GetTokenGenerator(), size); return ilgen; } - public ParameterBuilder DefineParameter(int position, ParameterAttributes attributes, string strParamName) + protected override ParameterBuilder DefineParameterCore(int position, ParameterAttributes attributes, string? strParamName) { RejectIfCreated(); @@ -371,26 +366,24 @@ internal void fixup() internal void ResolveUserTypes() { - rtype = TypeBuilder.ResolveUserType(rtype); - TypeBuilder.ResolveUserTypes(parameters); - TypeBuilder.ResolveUserTypes(returnModReq); - TypeBuilder.ResolveUserTypes(returnModOpt); + rtype = RuntimeTypeBuilder.ResolveUserType(rtype); + RuntimeTypeBuilder.ResolveUserTypes(parameters); + RuntimeTypeBuilder.ResolveUserTypes(returnModReq); + RuntimeTypeBuilder.ResolveUserTypes(returnModOpt); if (paramModReq != null) { foreach (Type[] types in paramModReq) - TypeBuilder.ResolveUserTypes(types); + RuntimeTypeBuilder.ResolveUserTypes(types); } if (paramModOpt != null) { foreach (Type[] types in paramModOpt) - TypeBuilder.ResolveUserTypes(types); + RuntimeTypeBuilder.ResolveUserTypes(types); } } - public void SetCustomAttribute(CustomAttributeBuilder customBuilder) + protected override void SetCustomAttributeCore(CustomAttributeBuilder customBuilder) { - ArgumentNullException.ThrowIfNull(customBuilder); - switch (customBuilder.Ctor.ReflectedType!.FullName) { case "System.Runtime.CompilerServices.MethodImplAttribute": @@ -475,14 +468,12 @@ public void SetCustomAttribute(CustomAttributeBuilder customBuilder) } } - public void SetCustomAttribute(ConstructorInfo con, byte[] binaryAttribute) + protected override void SetCustomAttributeCore(ConstructorInfo con, byte[] binaryAttribute) { - ArgumentNullException.ThrowIfNull(con); - ArgumentNullException.ThrowIfNull(binaryAttribute); - SetCustomAttribute(new CustomAttributeBuilder(con, binaryAttribute)); + SetCustomAttributeCore(new CustomAttributeBuilder(con, binaryAttribute)); } - public void SetImplementationFlags(MethodImplAttributes attributes) + protected override void SetImplementationFlagsCore(MethodImplAttributes attributes) { RejectIfCreated(); iattrs = attributes; @@ -591,30 +582,22 @@ public override Type[] GetGenericArguments() return result; } - public GenericTypeParameterBuilder[] DefineGenericParameters(params string[] names) + protected override GenericTypeParameterBuilder[] DefineGenericParametersCore(params string[] names) { - ArgumentNullException.ThrowIfNull(names); - if (names.Length == 0) - throw new ArgumentException(SR.Arg_EmptyArray, nameof(names)); type.check_not_created(); - generic_params = new GenericTypeParameterBuilder[names.Length]; + generic_params = new RuntimeGenericTypeParameterBuilder[names.Length]; for (int i = 0; i < names.Length; i++) { string item = names[i]; if (item == null) throw new ArgumentNullException(nameof(names)); - generic_params[i] = new GenericTypeParameterBuilder(type, this, item, i); + generic_params[i] = new RuntimeGenericTypeParameterBuilder(type, this, item, i); } return generic_params; } - public void SetReturnType(Type? returnType) - { - rtype = returnType; - } - - public void SetParameters(params Type[]? parameterTypes) + protected override void SetSignatureCore(Type? returnType, Type[]? returnTypeRequiredCustomModifiers, Type[]? returnTypeOptionalCustomModifiers, Type[]? parameterTypes, Type[][]? parameterTypeRequiredCustomModifiers, Type[][]? parameterTypeOptionalCustomModifiers) { if (parameterTypes != null) { @@ -625,16 +608,12 @@ public void SetParameters(params Type[]? parameterTypes) this.parameters = new Type[parameterTypes.Length]; Array.Copy(parameterTypes, this.parameters, parameterTypes.Length); } - } - public void SetSignature(Type? returnType, Type[]? returnTypeRequiredCustomModifiers, Type[]? returnTypeOptionalCustomModifiers, Type[]? parameterTypes, Type[][]? parameterTypeRequiredCustomModifiers, Type[][]? parameterTypeOptionalCustomModifiers) - { - SetReturnType(returnType); - SetParameters(parameterTypes); - this.returnModReq = returnTypeRequiredCustomModifiers; - this.returnModOpt = returnTypeOptionalCustomModifiers; - this.paramModReq = parameterTypeRequiredCustomModifiers; - this.paramModOpt = parameterTypeOptionalCustomModifiers; + rtype = returnType; + returnModReq = returnTypeRequiredCustomModifiers; + returnModOpt = returnTypeOptionalCustomModifiers; + paramModReq = parameterTypeRequiredCustomModifiers; + paramModOpt = parameterTypeOptionalCustomModifiers; } public override Module Module diff --git a/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/ModuleBuilder.Mono.cs b/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeModuleBuilder.Mono.cs similarity index 84% rename from src/mono/System.Private.CoreLib/src/System/Reflection/Emit/ModuleBuilder.Mono.cs rename to src/mono/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeModuleBuilder.Mono.cs index a1cf9e4b74236..cac8aeb0bcfc0 100644 --- a/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/ModuleBuilder.Mono.cs +++ b/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeModuleBuilder.Mono.cs @@ -44,7 +44,7 @@ namespace System.Reflection.Emit { [StructLayout(LayoutKind.Sequential)] - public partial class ModuleBuilder : Module + internal sealed partial class RuntimeModuleBuilder : ModuleBuilder { #region Sync with MonoReflectionModuleBuilder in object-internals.h @@ -60,10 +60,10 @@ public partial class ModuleBuilder : Module private UIntPtr dynamic_image; /* GC-tracked */ private int num_types; - private TypeBuilder[]? types; + private RuntimeTypeBuilder[]? types; private CustomAttributeBuilder[]? cattrs; private int table_idx; - internal AssemblyBuilder assemblyb; + internal RuntimeAssemblyBuilder assemblyb; private object[]? global_methods; private object[]? global_fields; private bool is_main; @@ -73,28 +73,28 @@ public partial class ModuleBuilder : Module #endregion private byte[] guid; - private TypeBuilder? global_type; + private RuntimeTypeBuilder? global_type; private bool global_type_created; // name_cache keys are display names - private Dictionary name_cache; + private Dictionary name_cache; private Dictionary us_string_cache; private ModuleBuilderTokenGenerator? token_gen; [MethodImplAttribute(MethodImplOptions.InternalCall)] - private static extern void basic_init(ModuleBuilder ab); + private static extern void basic_init(RuntimeModuleBuilder ab); [MethodImplAttribute(MethodImplOptions.InternalCall)] - private static extern void set_wrappers_type(ModuleBuilder mb, Type? ab); + private static extern void set_wrappers_type(RuntimeModuleBuilder mb, Type? ab); [DynamicDependency(nameof(table_indexes))] // Automatically keeps all previous fields too due to StructLayout - internal ModuleBuilder(AssemblyBuilder assb, string name) + internal RuntimeModuleBuilder(RuntimeAssemblyBuilder assb, string name) { this.name = this.scopename = name; this.fqname = name; this.assembly = this.assemblyb = assb; guid = Guid.NewGuid().ToByteArray(); table_idx = get_next_table_index(0x00, 1); - name_cache = new Dictionary(); + name_cache = new Dictionary(); us_string_cache = new Dictionary(512); this.global_type_created = false; @@ -102,7 +102,7 @@ internal ModuleBuilder(AssemblyBuilder assb, string name) CreateGlobalType(); - TypeBuilder tb = new TypeBuilder(this, TypeAttributes.Abstract, 0xFFFFFF); /*last valid token*/ + RuntimeTypeBuilder tb = new RuntimeTypeBuilder(this, TypeAttributes.Abstract, 0xFFFFFF); /*last valid token*/ Type? type = tb.CreateTypeInfo(); set_wrappers_type(this, type); } @@ -120,7 +120,7 @@ public override string FullyQualifiedName } } - public void CreateGlobalFunctions() + protected override void CreateGlobalFunctionsCore() { if (global_type_created) throw new InvalidOperationException(SR.InvalidOperation_GlobalsHaveBeenCreated); @@ -131,18 +131,18 @@ public void CreateGlobalFunctions() } } - public FieldBuilder DefineInitializedData(string name, byte[] data, FieldAttributes attributes) + protected override FieldBuilder DefineInitializedDataCore(string name, byte[] data, FieldAttributes attributes) { ArgumentNullException.ThrowIfNull(data); FieldAttributes maskedAttributes = attributes & ~FieldAttributes.ReservedMask; - FieldBuilder fb = DefineDataImpl(name, data.Length, maskedAttributes | FieldAttributes.HasFieldRVA); + RuntimeFieldBuilder fb = (RuntimeFieldBuilder)DefineDataImpl(name, data.Length, maskedAttributes | FieldAttributes.HasFieldRVA); fb.SetRVAData(data); return fb; } - public FieldBuilder DefineUninitializedData(string name, int size, FieldAttributes attributes) + protected override FieldBuilder DefineUninitializedDataCore(string name, int size, FieldAttributes attributes) { return DefineDataImpl(name, size, attributes & ~FieldAttributes.ReservedMask); } @@ -165,7 +165,7 @@ private FieldBuilder DefineDataImpl(string name, int size, FieldAttributes attri { TypeBuilder tb = DefineType(typeName, TypeAttributes.Public | TypeAttributes.ExplicitLayout | TypeAttributes.Sealed, - typeof(ValueType), null, FieldBuilder.RVADataPackingSize(size), size); + typeof(ValueType), null, RuntimeFieldBuilder.RVADataPackingSize(size), size); tb.CreateType(); datablobtype = tb; } @@ -200,9 +200,8 @@ private void addGlobalMethod(MethodBuilder mb) } } - public MethodBuilder DefineGlobalMethod(string name, MethodAttributes attributes, CallingConventions callingConvention, Type? returnType, Type[]? requiredReturnTypeCustomModifiers, Type[]? optionalReturnTypeCustomModifiers, Type[]? parameterTypes, Type[][]? requiredParameterTypeCustomModifiers, Type[][]? optionalParameterTypeCustomModifiers) + protected override MethodBuilder DefineGlobalMethodCore(string name, MethodAttributes attributes, CallingConventions callingConvention, Type? returnType, Type[]? requiredReturnTypeCustomModifiers, Type[]? optionalReturnTypeCustomModifiers, Type[]? parameterTypes, Type[][]? requiredParameterTypeCustomModifiers, Type[][]? optionalParameterTypeCustomModifiers) { - ArgumentNullException.ThrowIfNull(name); if ((attributes & MethodAttributes.Static) == 0) throw new ArgumentException(SR.Argument_GlobalMembersMustBeStatic); if (global_type_created) @@ -215,7 +214,7 @@ public MethodBuilder DefineGlobalMethod(string name, MethodAttributes attributes } [RequiresUnreferencedCode("P/Invoke marshalling may dynamically access members that could be trimmed.")] - public MethodBuilder DefinePInvokeMethod(string name, string dllName, string entryName, MethodAttributes attributes, CallingConventions callingConvention, Type? returnType, Type[]? parameterTypes, CallingConvention nativeCallConv, CharSet nativeCharSet) + protected override MethodBuilder DefinePInvokeMethodCore(string name, string dllName, string entryName, MethodAttributes attributes, CallingConventions callingConvention, Type? returnType, Type[]? parameterTypes, CallingConvention nativeCallConv, CharSet nativeCharSet) { ArgumentNullException.ThrowIfNull(name); if ((attributes & MethodAttributes.Static) == 0) @@ -229,20 +228,20 @@ public MethodBuilder DefinePInvokeMethod(string name, string dllName, string ent return mb; } - private void AddType(TypeBuilder tb) + private void AddType(RuntimeTypeBuilder tb) { if (types != null) { if (types.Length == num_types) { - TypeBuilder[] new_types = new TypeBuilder[types.Length * 2]; + RuntimeTypeBuilder[] new_types = new RuntimeTypeBuilder[types.Length * 2]; Array.Copy(types, new_types, num_types); types = new_types; } } else { - types = new TypeBuilder[1]; + types = new RuntimeTypeBuilder[1]; } types[num_types] = tb; num_types++; @@ -254,7 +253,7 @@ private TypeBuilder DefineType(string name, TypeAttributes attr, [DynamicallyAcc ITypeIdentifier ident = TypeIdentifiers.FromInternal(name); if (name_cache.ContainsKey(ident)) throw new ArgumentException(SR.Argument_DuplicateTypeName); - TypeBuilder res = new TypeBuilder(this, name, attr, parent, interfaces, packingSize, typesize, null); + RuntimeTypeBuilder res = new RuntimeTypeBuilder(this, name, attr, parent, interfaces, packingSize, typesize, null); AddType(res); name_cache.Add(ident, res); @@ -262,41 +261,36 @@ private TypeBuilder DefineType(string name, TypeAttributes attr, [DynamicallyAcc return res; } - internal void RegisterTypeName(TypeBuilder tb, ITypeName name) + internal void RegisterTypeName(RuntimeTypeBuilder tb, ITypeName name) { name_cache.Add(name, tb); } - internal TypeBuilder? GetRegisteredType(ITypeName name) + internal RuntimeTypeBuilder? GetRegisteredType(ITypeName name) { - TypeBuilder? result; + RuntimeTypeBuilder? result; name_cache.TryGetValue(name, out result); return result; } - public TypeBuilder DefineType(string name, TypeAttributes attr, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type? parent, Type[]? interfaces) - { - return DefineType(name, attr, parent, interfaces, PackingSize.Unspecified, TypeBuilder.UnspecifiedTypeSize); - } - - public TypeBuilder DefineType(string name, TypeAttributes attr, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type? parent, PackingSize packingSize, int typesize) + protected override TypeBuilder DefineTypeCore(string name, TypeAttributes attr, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type? parent, Type[]? interfaces, PackingSize packingSize, int typesize) { - return DefineType(name, attr, parent, null, packingSize, typesize); + return DefineType(name, attr, parent, interfaces, packingSize, typesize); } - public MethodInfo GetArrayMethod(Type arrayClass, string methodName, CallingConventions callingConvention, Type? returnType, Type[]? parameterTypes) + protected override MethodInfo GetArrayMethodCore(Type arrayClass, string methodName, CallingConventions callingConvention, Type? returnType, Type[]? parameterTypes) { return new MonoArrayMethod(arrayClass, methodName, callingConvention, returnType!, parameterTypes!); // FIXME: nulls should be allowed } - public EnumBuilder DefineEnum(string name, TypeAttributes visibility, Type underlyingType) + protected override EnumBuilder DefineEnumCore(string name, TypeAttributes visibility, Type underlyingType) { ITypeIdentifier ident = TypeIdentifiers.FromInternal(name); if (name_cache.ContainsKey(ident)) throw new ArgumentException(SR.Argument_DuplicateTypeName); - EnumBuilder eb = new EnumBuilder(this, name, visibility, underlyingType); - TypeBuilder res = eb.GetTypeBuilder(); + RuntimeEnumBuilder eb = new RuntimeEnumBuilder(this, name, visibility, underlyingType); + RuntimeTypeBuilder res = eb.GetTypeBuilder(); AddType(res); name_cache.Add(ident, res); return eb; @@ -314,7 +308,7 @@ public EnumBuilder DefineEnum(string name, TypeAttributes visibility, Type under return GetType(className, false, ignoreCase); } - private static TypeBuilder? search_in_array(TypeBuilder[] arr, int validElementsInArray, ITypeName className) + private static RuntimeTypeBuilder? search_in_array(RuntimeTypeBuilder[] arr, int validElementsInArray, ITypeName className) { int i; for (i = 0; i < validElementsInArray; ++i) @@ -327,7 +321,7 @@ public EnumBuilder DefineEnum(string name, TypeAttributes visibility, Type under return null; } - private static TypeBuilder? search_nested_in_array(TypeBuilder[] arr, int validElementsInArray, ITypeName className) + private static RuntimeTypeBuilder? search_nested_in_array(RuntimeTypeBuilder[] arr, int validElementsInArray, ITypeName className) { int i; for (i = 0; i < validElementsInArray; ++i) @@ -338,9 +332,9 @@ public EnumBuilder DefineEnum(string name, TypeAttributes visibility, Type under return null; } - private static TypeBuilder? GetMaybeNested(TypeBuilder t, IEnumerable nested) + private static RuntimeTypeBuilder? GetMaybeNested(RuntimeTypeBuilder t, IEnumerable nested) { - TypeBuilder? result = t; + RuntimeTypeBuilder? result = t; foreach (ITypeName pname in nested) { @@ -358,7 +352,7 @@ public EnumBuilder DefineEnum(string name, TypeAttributes visibility, Type under { ArgumentException.ThrowIfNullOrEmpty(className); - TypeBuilder? result = null; + RuntimeTypeBuilder? result = null; if (types == null && throwOnError) throw new TypeLoadException(className); @@ -384,9 +378,8 @@ public EnumBuilder DefineEnum(string name, TypeAttributes visibility, Type under if (result != null && (ts.HasModifiers || ts.IsByRef)) { Type mt = result; - if (result is TypeBuilder) + if (result is RuntimeTypeBuilder tb) { - var tb = result as TypeBuilder; if (tb.is_created) mt = tb.CreateType()!; } @@ -407,7 +400,7 @@ public EnumBuilder DefineEnum(string name, TypeAttributes visibility, Type under } if (ts.IsByRef) mt = mt.MakeByRefType(); - result = mt as TypeBuilder; + result = mt as RuntimeTypeBuilder; if (result == null) return mt; } @@ -433,9 +426,8 @@ internal int get_next_table_index(int table, int count) return index; } - public void SetCustomAttribute(CustomAttributeBuilder customBuilder) + protected override void SetCustomAttributeCore(CustomAttributeBuilder customBuilder) { - ArgumentNullException.ThrowIfNull(customBuilder); if (cattrs != null) { CustomAttributeBuilder[] new_array = new CustomAttributeBuilder[cattrs.Length + 1]; @@ -450,9 +442,9 @@ public void SetCustomAttribute(CustomAttributeBuilder customBuilder) } } - public void SetCustomAttribute(ConstructorInfo con, byte[] binaryAttribute) + protected override void SetCustomAttributeCore(ConstructorInfo con, byte[] binaryAttribute) { - SetCustomAttribute(new CustomAttributeBuilder(con, binaryAttribute)); + SetCustomAttributeCore(new CustomAttributeBuilder(con, binaryAttribute)); } /* internal ISymbolDocumentWriter? DefineDocument (string url, Guid language, Guid languageVendor, Guid documentType) @@ -481,7 +473,7 @@ public override Type[] GetTypes() return copy; } - internal static int GetMethodToken(MethodInfo method) + public override int GetMethodMetadataToken(MethodInfo method) { ArgumentNullException.ThrowIfNull(method); @@ -490,17 +482,17 @@ internal static int GetMethodToken(MethodInfo method) internal int GetArrayMethodToken(Type arrayClass, string methodName, CallingConventions callingConvention, Type? returnType, Type[]? parameterTypes) { - return GetMethodToken(GetArrayMethod(arrayClass, methodName, callingConvention, returnType, parameterTypes)); + return GetMethodMetadataToken(GetArrayMethod(arrayClass, methodName, callingConvention, returnType, parameterTypes)); } - internal static int GetConstructorToken(ConstructorInfo con) + public override int GetMethodMetadataToken(ConstructorInfo constructor) { - ArgumentNullException.ThrowIfNull(con); + ArgumentNullException.ThrowIfNull(constructor); - return con.MetadataToken; + return constructor.MetadataToken; } - internal static int GetFieldToken(FieldInfo field) + public override int GetFieldMetadataToken(FieldInfo field) { ArgumentNullException.ThrowIfNull(field); @@ -524,19 +516,19 @@ internal int GetSignatureToken(byte[] sigBytes, int sigLength) throw new NotImplementedException(); } - internal int GetSignatureToken(SignatureHelper sigHelper) + public override int GetSignatureMetadataToken(SignatureHelper signature) { - ArgumentNullException.ThrowIfNull(sigHelper); - return GetToken(sigHelper); + ArgumentNullException.ThrowIfNull(signature); + return GetToken(signature); } - internal int GetStringConstant(string str) + public override int GetStringMetadataToken(string stringConstant) { - ArgumentNullException.ThrowIfNull(str); - return GetToken(str); + ArgumentNullException.ThrowIfNull(stringConstant); + return GetToken(stringConstant); } - internal static int GetTypeToken(Type type) + public override int GetTypeMetadataToken(Type type) { ArgumentNullException.ThrowIfNull(type); if (type.IsByRef) @@ -548,17 +540,17 @@ internal static int GetTypeToken(Type type) Justification = "Reflection.Emit is not subject to trimming")] internal int GetTypeToken(string name) { - return GetTypeToken(GetType(name)!); + return GetTypeMetadataToken(GetType(name)!); } [MethodImplAttribute(MethodImplOptions.InternalCall)] - private static extern int getUSIndex(ModuleBuilder mb, string str); + private static extern int getUSIndex(RuntimeModuleBuilder mb, string str); [MethodImplAttribute(MethodImplOptions.InternalCall)] - private static extern int getToken(ModuleBuilder mb, object obj, bool create_open_instance); + private static extern int getToken(RuntimeModuleBuilder mb, object obj, bool create_open_instance); [MethodImplAttribute(MethodImplOptions.InternalCall)] - private static extern int getMethodToken(ModuleBuilder mb, MethodBase method, + private static extern int getMethodToken(RuntimeModuleBuilder mb, MethodBase method, Type[] opt_param_types); internal int GetToken(string str) @@ -625,21 +617,21 @@ private int GetPseudoToken(MemberInfo member, bool create_open_instance) else token = typeref_tokengen--; } - else if (member is EnumBuilder eb) + else if (member is RuntimeEnumBuilder eb) { token = GetPseudoToken(eb.GetTypeBuilder(), create_open_instance); dict[member] = token; // n.b. don't register with the runtime, the TypeBuilder already did it. return token; } - else if (member is ConstructorBuilder cb) + else if (member is RuntimeConstructorBuilder cb) { if (member.Module == this && !cb.TypeBuilder.ContainsGenericParameters) token = methoddef_tokengen--; else token = memberref_tokengen--; } - else if (member is MethodBuilder mb) + else if (member is RuntimeMethodBuilder mb) { if (member.Module == this && !mb.TypeBuilder.ContainsGenericParameters && !mb.IsGenericMethodDefinition) token = methoddef_tokengen--; @@ -715,13 +707,13 @@ internal int GetToken(SignatureHelper helper) // Called from the runtime to return the corresponding finished reflection object internal static object RuntimeResolve(object obj) { - if (obj is MethodBuilder mb) + if (obj is RuntimeMethodBuilder mb) return mb.RuntimeResolve(); - if (obj is ConstructorBuilder cb) + if (obj is RuntimeConstructorBuilder cb) return cb.RuntimeResolve(); - if (obj is FieldBuilder fb) + if (obj is RuntimeFieldBuilder fb) return fb.RuntimeResolve(); - if (obj is GenericTypeParameterBuilder gtpb) + if (obj is RuntimeGenericTypeParameterBuilder gtpb) return gtpb.RuntimeResolve(); if (obj is FieldOnTypeBuilderInst fotbi) return fotbi.RuntimeResolve(); @@ -752,7 +744,7 @@ internal bool IsMain internal void CreateGlobalType() { - global_type ??= new TypeBuilder(this, 0, 1, true); + global_type ??= new RuntimeTypeBuilder(this, 0, 1, true); } public override Assembly Assembly @@ -784,7 +776,7 @@ public override bool IsResource() return false; } - internal ModuleBuilder InternalModule => this; + internal RuntimeModuleBuilder InternalModule => this; internal IntPtr GetUnderlyingNativeHandle() { return _impl; } @@ -934,9 +926,9 @@ public override int MetadataToken internal sealed class ModuleBuilderTokenGenerator : ITokenGenerator { - private ModuleBuilder mb; + private RuntimeModuleBuilder mb; - public ModuleBuilderTokenGenerator(ModuleBuilder mb) + public ModuleBuilderTokenGenerator(RuntimeModuleBuilder mb) { this.mb = mb; } diff --git a/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/PropertyBuilder.Mono.cs b/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/RuntimePropertyBuilder.Mono.cs similarity index 87% rename from src/mono/System.Private.CoreLib/src/System/Reflection/Emit/PropertyBuilder.Mono.cs rename to src/mono/System.Private.CoreLib/src/System/Reflection/Emit/RuntimePropertyBuilder.Mono.cs index cb6b4f4faac9c..aced71428884c 100644 --- a/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/PropertyBuilder.Mono.cs +++ b/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/RuntimePropertyBuilder.Mono.cs @@ -41,7 +41,7 @@ namespace System.Reflection.Emit { [StructLayout(LayoutKind.Sequential)] - public sealed partial class PropertyBuilder : PropertyInfo + internal sealed partial class RuntimePropertyBuilder : PropertyBuilder { #region Sync with MonoReflectionPropertyBuilder in object-internals.h private PropertyAttributes attrs; @@ -53,7 +53,7 @@ public sealed partial class PropertyBuilder : PropertyInfo private MethodBuilder? set_method; private MethodBuilder? get_method; private int table_idx; - internal TypeBuilder typeb; + internal RuntimeTypeBuilder typeb; private Type[]? returnModReq; private Type[]? returnModOpt; private Type[][]? paramModReq; @@ -61,7 +61,7 @@ public sealed partial class PropertyBuilder : PropertyInfo private CallingConventions callingConvention; #endregion - internal PropertyBuilder(TypeBuilder tb, string name, PropertyAttributes attributes, CallingConventions callingConvention, Type returnType, Type[]? returnModReq, Type[]? returnModOpt, Type[]? parameterTypes, Type[][]? paramModReq, Type[][]? paramModOpt) + internal RuntimePropertyBuilder(RuntimeTypeBuilder tb, string name, PropertyAttributes attributes, CallingConventions callingConvention, Type returnType, Type[]? returnModReq, Type[]? returnModOpt, Type[]? parameterTypes, Type[][]? paramModReq, Type[][]? paramModOpt) { this.name = name; this.attrs = attributes; @@ -109,7 +109,7 @@ public override Type ReflectedType get { return typeb; } } - public void AddOtherMethod(MethodBuilder mdBuilder) + protected override void AddOtherMethodCore(MethodBuilder mdBuilder) { ArgumentNullException.ThrowIfNull(mdBuilder); typeb.check_not_created(); @@ -153,15 +153,14 @@ public override bool IsDefined(Type attributeType, bool inherit) { throw not_supported(); } - public void SetConstant(object? defaultValue) + protected override void SetConstantCore(object? defaultValue) { typeb.check_not_created(); def_value = defaultValue; } - public void SetCustomAttribute(CustomAttributeBuilder customBuilder) + protected override void SetCustomAttributeCore(CustomAttributeBuilder customBuilder) { - ArgumentNullException.ThrowIfNull(customBuilder); typeb.check_not_created(); string? attrname = customBuilder.Ctor.ReflectedType!.FullName; if (attrname == "System.Runtime.CompilerServices.SpecialNameAttribute") @@ -184,19 +183,19 @@ public void SetCustomAttribute(CustomAttributeBuilder customBuilder) } } - public void SetCustomAttribute(ConstructorInfo con, byte[] binaryAttribute) + protected override void SetCustomAttributeCore(ConstructorInfo con, byte[] binaryAttribute) { - SetCustomAttribute(new CustomAttributeBuilder(con, binaryAttribute)); + SetCustomAttributeCore(new CustomAttributeBuilder(con, binaryAttribute)); } - public void SetGetMethod(MethodBuilder mdBuilder) + protected override void SetGetMethodCore(MethodBuilder mdBuilder) { typeb.check_not_created(); ArgumentNullException.ThrowIfNull(mdBuilder); get_method = mdBuilder; } - public void SetSetMethod(MethodBuilder mdBuilder) + protected override void SetSetMethodCore(MethodBuilder mdBuilder) { ArgumentNullException.ThrowIfNull(mdBuilder); set_method = mdBuilder; diff --git a/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilder.Mono.cs b/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeTypeBuilder.Mono.cs similarity index 89% rename from src/mono/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilder.Mono.cs rename to src/mono/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeTypeBuilder.Mono.cs index 44e40df96a2e5..67d46428068c9 100644 --- a/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilder.Mono.cs +++ b/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeTypeBuilder.Mono.cs @@ -44,8 +44,112 @@ namespace System.Reflection.Emit { + public abstract partial class TypeBuilder + { + [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2070:UnrecognizedReflectionPattern", + Justification = "Linker thinks Type.GetConstructor(ConstructorInfo) is one of the public APIs because it doesn't analyze method signatures. We already have ConstructorInfo.")] + [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2055:UnrecognizedReflectionPattern", + Justification = "Type.MakeGenericType is used to create a typical instantiation")] + public static ConstructorInfo GetConstructor(Type type, ConstructorInfo constructor) + { + if (!IsValidGetMethodType(type)) + throw new ArgumentException(SR.Argument_MustBeTypeBuilder, nameof(type)); + + if (type is TypeBuilder && type.ContainsGenericParameters) + type = type.MakeGenericType(type.GetGenericArguments()); + + if (!constructor.DeclaringType!.IsGenericTypeDefinition) + throw new ArgumentException(SR.Argument_ConstructorNeedGenericDeclaringType, nameof(constructor)); + + if (constructor.DeclaringType != type.GetGenericTypeDefinition()) + throw new ArgumentException(SR.Argument_InvalidConstructorDeclaringType, nameof(type)); + + ConstructorInfo res = type.GetConstructor(constructor); + if (res == null) + throw new ArgumentException("constructor not found"); + + return res; + } + + private static bool IsValidGetMethodType(Type type) + { + if (type == null) + return false; + + if (type is TypeBuilder || type is TypeBuilderInstantiation) + return true; + /*GetMethod() must work with TypeBuilders after CreateType() was called.*/ + if (type.Module is ModuleBuilder) + return true; + if (type.IsGenericParameter) + return false; + + Type[] inst = type.GetGenericArguments(); + if (inst == null) + return false; + for (int i = 0; i < inst.Length; ++i) + { + if (IsValidGetMethodType(inst[i])) + return true; + } + return false; + } + + [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2055:UnrecognizedReflectionPattern", + Justification = "Type.MakeGenericType is used to create a typical instantiation")] + public static MethodInfo GetMethod(Type type, MethodInfo method) + { + if (!IsValidGetMethodType(type)) + throw new ArgumentException(SR.Argument_MustBeTypeBuilder, nameof(type)); + + if (type is TypeBuilder && type.ContainsGenericParameters) + type = type.MakeGenericType(type.GetGenericArguments()); + + if (method.IsGenericMethod && !method.IsGenericMethodDefinition) + throw new ArgumentException(SR.Argument_NeedGenericMethodDefinition, nameof(method)); + + if (!method.DeclaringType!.IsGenericTypeDefinition) + throw new ArgumentException(SR.Argument_MethodNeedGenericDeclaringType, nameof(method)); + + if (method.DeclaringType != type.GetGenericTypeDefinition()) + throw new ArgumentException(SR.Argument_InvalidMethodDeclaringType, nameof(type)); + + MethodInfo res = type.GetMethod(method); + if (res == null) + throw new ArgumentException(string.Format("method {0} not found in type {1}", method.Name, type)); + + return res; + } + + [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2055:UnrecognizedReflectionPattern", + Justification = "Type.MakeGenericType is used to create a typical instantiation")] + public static FieldInfo GetField(Type type, FieldInfo field) + { + if (!IsValidGetMethodType(type)) + throw new ArgumentException(SR.Argument_MustBeTypeBuilder, nameof(type)); + + if (type is TypeBuilder && type.ContainsGenericParameters) + type = type.MakeGenericType(type.GetGenericArguments()); + + if (!field.DeclaringType!.IsGenericTypeDefinition) + throw new ArgumentException(SR.Argument_FieldNeedGenericDeclaringType, nameof(field)); + + if (field.DeclaringType != type.GetGenericTypeDefinition()) + throw new ArgumentException(SR.Argument_InvalidFieldDeclaringType, nameof(type)); + + if (field is FieldOnTypeBuilderInst) + throw new ArgumentException("The specified field must be declared on a generic type definition.", nameof(field)); + + FieldInfo res = type.GetField(field); + if (res == null) + throw new System.Exception("field not found"); + else + return res; + } + } + [StructLayout(LayoutKind.Sequential)] - public sealed partial class TypeBuilder : TypeInfo + internal sealed partial class RuntimeTypeBuilder : TypeBuilder { #region Sync with MonoReflectionTypeBuilder in object-internals.h private string tname; // name in internal form @@ -57,21 +161,21 @@ public sealed partial class TypeBuilder : TypeInfo private Type? nesting_type; internal Type[]? interfaces; internal int num_methods; - internal MethodBuilder[]? methods; - internal ConstructorBuilder[]? ctors; - internal PropertyBuilder[]? properties; + internal RuntimeMethodBuilder[]? methods; + internal RuntimeConstructorBuilder[]? ctors; + internal RuntimePropertyBuilder[]? properties; internal int num_fields; - internal FieldBuilder[]? fields; - internal EventBuilder[]? events; + internal RuntimeFieldBuilder[]? fields; + internal RuntimeEventBuilder[]? events; private CustomAttributeBuilder[]? cattrs; - internal TypeBuilder[]? subtypes; + internal RuntimeTypeBuilder[]? subtypes; internal TypeAttributes attrs; private int table_idx; - private ModuleBuilder pmodule; + private RuntimeModuleBuilder pmodule; private int class_size; private PackingSize packing_size; private IntPtr generic_container; - private GenericTypeParameterBuilder[]? generic_params; + private RuntimeGenericTypeParameterBuilder[]? generic_params; [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] private TypeInfo? created; @@ -92,7 +196,7 @@ protected override TypeAttributes GetAttributeFlagsImpl() [DynamicDependency(nameof(state))] // Automatically keeps all previous fields too due to StructLayout [DynamicDependency(nameof(IsAssignableToInternal))] // Used from reflection.c: mono_reflection_call_is_assignable_to - internal TypeBuilder(ModuleBuilder mb, TypeAttributes attr, int table_idx, bool is_hidden_global_type = false) + internal RuntimeTypeBuilder(RuntimeModuleBuilder mb, TypeAttributes attr, int table_idx, bool is_hidden_global_type = false) { this.is_hidden_global_type = is_hidden_global_type; this.parent = null; @@ -110,7 +214,7 @@ internal TypeBuilder(ModuleBuilder mb, TypeAttributes attr, int table_idx, bool [DynamicDependency(nameof(state))] // Automatically keeps all previous fields too due to StructLayout [DynamicDependency(nameof(IsAssignableToInternal))] // Used from reflection.c: mono_reflection_call_is_assignable_to - internal TypeBuilder(ModuleBuilder mb, string fullname, TypeAttributes attr, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)]Type? parent, Type[]? interfaces, PackingSize packing_size, int type_size, Type? nesting_type) + internal RuntimeTypeBuilder(RuntimeModuleBuilder mb, string fullname, TypeAttributes attr, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)]Type? parent, Type[]? interfaces, PackingSize packing_size, int type_size, Type? nesting_type) { this.is_hidden_global_type = false; int sep_index; @@ -255,12 +359,12 @@ public override string Name get { return nspace; } } - public PackingSize PackingSize + protected override PackingSize PackingSizeCore { get { return packing_size; } } - public int Size + protected override int SizeCore { get { return class_size; } } @@ -270,10 +374,8 @@ public int Size get { return nesting_type; } } - public void AddInterfaceImplementation([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type interfaceType) + protected override void AddInterfaceImplementationCore([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type interfaceType) { - ArgumentNullException.ThrowIfNull(interfaceType); - check_not_created(); if (interfaces != null) @@ -316,7 +418,7 @@ public void AddInterfaceImplementation([DynamicallyAccessedMembers(DynamicallyAc ConstructorBuilder? found = null; int count = 0; - foreach (ConstructorBuilder cb in ctors) + foreach (RuntimeConstructorBuilder cb in ctors) { if (callConvention != CallingConventions.Any && cb.CallingConvention != callConvention) continue; @@ -378,7 +480,7 @@ public override object[] GetCustomAttributes(Type attributeType, bool inherit) return created!.GetCustomAttributes(attributeType, inherit); } - private TypeBuilder DefineNestedType(string name, TypeAttributes attr, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type? parent, Type[]? interfaces, + protected override TypeBuilder DefineNestedTypeCore(string name, TypeAttributes attr, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type? parent, Type[]? interfaces, PackingSize packSize, int typeSize) { // Visibility must be NestedXXX @@ -397,53 +499,42 @@ public override object[] GetCustomAttributes(Type attributeType, bool inherit) } } - TypeBuilder res = new TypeBuilder(pmodule, name, attr, parent, interfaces, packSize, typeSize, this); + RuntimeTypeBuilder res = new RuntimeTypeBuilder(pmodule, name, attr, parent, interfaces, packSize, typeSize, this); res.fullname = res.GetFullName(); pmodule.RegisterTypeName(res, res.fullname); if (subtypes != null) { - TypeBuilder[] new_types = new TypeBuilder[subtypes.Length + 1]; + RuntimeTypeBuilder[] new_types = new RuntimeTypeBuilder[subtypes.Length + 1]; Array.Copy(subtypes, new_types, subtypes.Length); new_types[subtypes.Length] = res; subtypes = new_types; } else { - subtypes = new TypeBuilder[1]; + subtypes = new RuntimeTypeBuilder[1]; subtypes[0] = res; } return res; } - public TypeBuilder DefineNestedType(string name, TypeAttributes attr, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type? parent, Type[]? interfaces) - { - return DefineNestedType(name, attr, parent, interfaces, PackingSize.Unspecified, UnspecifiedTypeSize); - } - - public TypeBuilder DefineNestedType(string name, TypeAttributes attr, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type? parent, PackingSize packSize, - int typeSize) - { - return DefineNestedType(name, attr, parent, null, packSize, typeSize); - } - - public ConstructorBuilder DefineConstructor(MethodAttributes attributes, CallingConventions callingConvention, Type[]? parameterTypes, Type[][]? requiredCustomModifiers, Type[][]? optionalCustomModifiers) + protected override ConstructorBuilder DefineConstructorCore(MethodAttributes attributes, CallingConventions callingConvention, Type[]? parameterTypes, Type[][]? requiredCustomModifiers, Type[][]? optionalCustomModifiers) { check_not_created(); if (IsInterface && (attributes & MethodAttributes.Static) == 0) throw new InvalidOperationException(); - ConstructorBuilder cb = new ConstructorBuilder(this, attributes, + RuntimeConstructorBuilder cb = new RuntimeConstructorBuilder(this, attributes, callingConvention, parameterTypes, requiredCustomModifiers, optionalCustomModifiers); if (ctors != null) { - ConstructorBuilder[] new_ctors = new ConstructorBuilder[ctors.Length + 1]; + RuntimeConstructorBuilder[] new_ctors = new RuntimeConstructorBuilder[ctors.Length + 1]; Array.Copy(ctors, new_ctors, ctors.Length); new_ctors[ctors.Length] = cb; ctors = new_ctors; } else { - ctors = new ConstructorBuilder[1]; + ctors = new RuntimeConstructorBuilder[1]; ctors[0] = cb; } return cb; @@ -451,7 +542,7 @@ public ConstructorBuilder DefineConstructor(MethodAttributes attributes, Calling [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2075:UnrecognizedReflectionPattern", Justification = "Reflection.Emit is not subject to trimming")] - public ConstructorBuilder DefineDefaultConstructor(MethodAttributes attributes) + protected override ConstructorBuilder DefineDefaultConstructorCore(MethodAttributes attributes) { Type parent_type, old_parent_type; @@ -480,7 +571,7 @@ public ConstructorBuilder DefineDefaultConstructor(MethodAttributes attributes) throw new NotSupportedException(SR.NotSupported_NoParentDefaultConstructor); } - ConstructorBuilder cb = DefineConstructor(attributes, + RuntimeConstructorBuilder cb = (RuntimeConstructorBuilder)DefineConstructor(attributes, CallingConventions.Standard, EmptyTypes); ILGenerator ig = cb.GetILGenerator(); ig.Emit(OpCodes.Ldarg_0); @@ -490,26 +581,26 @@ public ConstructorBuilder DefineDefaultConstructor(MethodAttributes attributes) return cb; } - private void append_method(MethodBuilder mb) + private void append_method(RuntimeMethodBuilder mb) { if (methods != null) { if (methods.Length == num_methods) { - MethodBuilder[] new_methods = new MethodBuilder[methods.Length * 2]; + RuntimeMethodBuilder[] new_methods = new RuntimeMethodBuilder[methods.Length * 2]; Array.Copy(methods, new_methods, num_methods); methods = new_methods; } } else { - methods = new MethodBuilder[1]; + methods = new RuntimeMethodBuilder[1]; } methods[num_methods] = mb; num_methods++; } - public MethodBuilder DefineMethod(string name, MethodAttributes attributes, CallingConventions callingConvention, Type? returnType, Type[]? returnTypeRequiredCustomModifiers, Type[]? returnTypeOptionalCustomModifiers, Type[]? parameterTypes, Type[][]? parameterTypeRequiredCustomModifiers, Type[][]? parameterTypeOptionalCustomModifiers) + protected override MethodBuilder DefineMethodCore(string name, MethodAttributes attributes, CallingConventions callingConvention, Type? returnType, Type[]? returnTypeRequiredCustomModifiers, Type[]? returnTypeOptionalCustomModifiers, Type[]? parameterTypes, Type[][]? parameterTypeRequiredCustomModifiers, Type[][]? parameterTypeOptionalCustomModifiers) { check_name(nameof(name), name); check_not_created(); @@ -520,7 +611,7 @@ public MethodBuilder DefineMethod(string name, MethodAttributes attributes, Call throw new ArgumentException("Interface method must be abstract and virtual."); returnType ??= typeof(void); - MethodBuilder res = new MethodBuilder(this, name, attributes, + RuntimeMethodBuilder res = new RuntimeMethodBuilder(this, name, attributes, callingConvention, returnType, returnTypeRequiredCustomModifiers, returnTypeOptionalCustomModifiers, parameterTypes, @@ -531,7 +622,7 @@ public MethodBuilder DefineMethod(string name, MethodAttributes attributes, Call } [RequiresUnreferencedCode("P/Invoke marshalling may dynamically access members that could be trimmed.")] - public MethodBuilder DefinePInvokeMethod( + protected override MethodBuilder DefinePInvokeMethodCore( string name, string dllName, string entryName, MethodAttributes attributes, @@ -554,8 +645,8 @@ public MethodBuilder DefineMethod(string name, MethodAttributes attributes, Call throw new ArgumentException(SR.Argument_BadPInvokeOnInterface); check_not_created(); - MethodBuilder res - = new MethodBuilder( + RuntimeMethodBuilder res + = new RuntimeMethodBuilder( this, name, attributes, @@ -574,33 +665,31 @@ MethodBuilder res return res; } - public void DefineMethodOverride(MethodInfo methodInfoBody, MethodInfo methodInfoDeclaration) + protected override void DefineMethodOverrideCore(MethodInfo methodInfoBody, MethodInfo methodInfoDeclaration) { - ArgumentNullException.ThrowIfNull(methodInfoBody); - ArgumentNullException.ThrowIfNull(methodInfoDeclaration); check_not_created(); if (methodInfoBody.DeclaringType != this) throw new ArgumentException("method body must belong to this type"); - if (methodInfoBody is MethodBuilder mb) + if (methodInfoBody is RuntimeMethodBuilder mb) { mb.set_override(methodInfoDeclaration); } } - public FieldBuilder DefineField(string fieldName, Type type, Type[]? requiredCustomModifiers, Type[]? optionalCustomModifiers, FieldAttributes attributes) + protected override FieldBuilder DefineFieldCore(string fieldName, Type type, Type[]? requiredCustomModifiers, Type[]? optionalCustomModifiers, FieldAttributes attributes) { check_name(nameof(fieldName), fieldName); if (type == typeof(void)) throw new ArgumentException(SR.Argument_BadFieldType); check_not_created(); - FieldBuilder res = new FieldBuilder(this, fieldName, type, attributes, requiredCustomModifiers, optionalCustomModifiers); + RuntimeFieldBuilder res = new RuntimeFieldBuilder(this, fieldName, type, attributes, requiredCustomModifiers, optionalCustomModifiers); if (fields != null) { if (fields.Length == num_fields) { - FieldBuilder[] new_fields = new FieldBuilder[fields.Length * 2]; + RuntimeFieldBuilder[] new_fields = new RuntimeFieldBuilder[fields.Length * 2]; Array.Copy(fields, new_fields, num_fields); fields = new_fields; } @@ -609,7 +698,7 @@ public FieldBuilder DefineField(string fieldName, Type type, Type[]? requiredCus } else { - fields = new FieldBuilder[1]; + fields = new RuntimeFieldBuilder[1]; fields[0] = res; num_fields++; } @@ -623,7 +712,7 @@ public FieldBuilder DefineField(string fieldName, Type type, Type[]? requiredCus return res; } - public PropertyBuilder DefineProperty(string name, PropertyAttributes attributes, CallingConventions callingConvention, Type returnType, Type[]? returnTypeRequiredCustomModifiers, Type[]? returnTypeOptionalCustomModifiers, Type[]? parameterTypes, Type[][]? parameterTypeRequiredCustomModifiers, Type[][]? parameterTypeOptionalCustomModifiers) + protected override PropertyBuilder DefinePropertyCore(string name, PropertyAttributes attributes, CallingConventions callingConvention, Type returnType, Type[]? returnTypeRequiredCustomModifiers, Type[]? returnTypeOptionalCustomModifiers, Type[]? parameterTypes, Type[][]? parameterTypeRequiredCustomModifiers, Type[][]? parameterTypeOptionalCustomModifiers) { check_name(nameof(name), name); if (parameterTypes != null) @@ -632,7 +721,7 @@ public PropertyBuilder DefineProperty(string name, PropertyAttributes attributes throw new ArgumentNullException(nameof(parameterTypes)); check_not_created(); - PropertyBuilder res = new PropertyBuilder(this, name, attributes, callingConvention, returnType, returnTypeRequiredCustomModifiers, returnTypeOptionalCustomModifiers, parameterTypes, parameterTypeRequiredCustomModifiers, parameterTypeOptionalCustomModifiers); + RuntimePropertyBuilder res = new RuntimePropertyBuilder(this, name, attributes, callingConvention, returnType, returnTypeRequiredCustomModifiers, returnTypeOptionalCustomModifiers, parameterTypes, parameterTypeRequiredCustomModifiers, parameterTypeOptionalCustomModifiers); if (properties != null) { @@ -641,12 +730,12 @@ public PropertyBuilder DefineProperty(string name, PropertyAttributes attributes } else { - properties = new PropertyBuilder[1] { res }; + properties = new RuntimePropertyBuilder[1] { res }; } return res; } - public ConstructorBuilder DefineTypeInitializer() + protected override ConstructorBuilder DefineTypeInitializerCore() { return DefineConstructor(MethodAttributes.Public | MethodAttributes.Static | MethodAttributes.SpecialName | @@ -690,26 +779,16 @@ private bool has_ctor_method() return false; } - // We require emitted types to have all members on their bases to be accessible. - // This is basically an identity function for `this`. - [return: DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] - public Type CreateType() - { - Type? type = CreateTypeInfo(); - Debug.Assert(type != null); - return type; - } - // We require emitted types to have all members on their bases to be accessible. // This is basically an identity function for `this`. [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2083:UnrecognizedReflectionPattern", Justification = "Reflection.Emit is not subject to trimming")] [return: DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] - public TypeInfo? CreateTypeInfo() + protected override TypeInfo CreateTypeInfoCore() { /* handle nesting_type */ if (createTypeCalled) - return created; + return created!; if (!IsInterface && (parent == null) && (this != typeof(object)) && (FullName != "")) { @@ -720,14 +799,14 @@ public Type CreateType() // value type. if (fields != null) { - foreach (FieldBuilder fb in fields) + foreach (RuntimeFieldBuilder fb in fields) { if (fb == null) continue; Type ft = fb.FieldType; - if (!fb.IsStatic && (ft is TypeBuilder builder) && ft.IsValueType && (ft != this) && is_nested_in(ft)) + if (!fb.IsStatic && (ft is RuntimeTypeBuilder builder) && ft.IsValueType && (ft != this) && is_nested_in(ft)) { - TypeBuilder tb = builder; + RuntimeTypeBuilder tb = builder; if (!tb.is_created) { throw new NotImplementedException(); @@ -773,7 +852,7 @@ public Type CreateType() throw new BadImageFormatException(); if (!iface.IsInterface) throw new TypeLoadException(); - if (iface is TypeBuilder builder && !builder.is_created) + if (iface is RuntimeTypeBuilder builder && !builder.is_created) throw new TypeLoadException(); } } @@ -783,7 +862,7 @@ public Type CreateType() bool is_concrete = !IsAbstract; for (int i = 0; i < num_methods; ++i) { - MethodBuilder mb = (MethodBuilder)(methods[i]); + RuntimeMethodBuilder mb = methods[i]; if (is_concrete && mb.IsAbstract) throw new InvalidOperationException("Type is concrete but has abstract method " + mb); mb.check_override(); @@ -793,7 +872,7 @@ public Type CreateType() if (ctors != null) { - foreach (ConstructorBuilder ctor in ctors) + foreach (RuntimeConstructorBuilder ctor in ctors) ctor.fixup(); } @@ -803,7 +882,7 @@ public Type CreateType() if (is_hidden_global_type) { - return null; + return null!; } if (created != null) @@ -819,21 +898,21 @@ private void ResolveUserTypes() ResolveUserTypes(interfaces); if (fields != null) { - foreach (FieldBuilder fb in fields) + foreach (RuntimeFieldBuilder fb in fields) { fb?.ResolveUserTypes(); } } if (methods != null) { - foreach (MethodBuilder mb in methods) + foreach (RuntimeMethodBuilder mb in methods) { mb?.ResolveUserTypes(); } } if (ctors != null) { - foreach (ConstructorBuilder cb in ctors) + foreach (RuntimeConstructorBuilder cb in ctors) { cb?.ResolveUserTypes(); } @@ -875,7 +954,7 @@ internal void GenerateDebugInfo (ISymbolWriter symbolWriter) } if (ctors != null) { - foreach (ConstructorBuilder ctor in ctors) + foreach (RuntimeConstructorBuilder ctor in ctors) ctor.GenerateDebugInfo (symbolWriter); } @@ -905,7 +984,7 @@ internal ConstructorInfo[] GetConstructorsInternal(BindingFlags bindingAttr) bool match; MethodAttributes mattrs; - foreach (ConstructorBuilder c in ctors) + foreach (RuntimeConstructorBuilder c in ctors) { match = false; mattrs = c.Attributes; @@ -1150,7 +1229,7 @@ public override MethodInfo[] GetMethods(BindingFlags bindingAttr) if (subtypes == null) return null; - foreach (TypeBuilder t in subtypes) + foreach (RuntimeTypeBuilder t in subtypes) { if (!t.is_created) continue; @@ -1182,7 +1261,7 @@ public override Type[] GetNestedTypes(BindingFlags bindingAttr) if (subtypes == null) return EmptyTypes; - foreach (TypeBuilder t in subtypes) + foreach (RuntimeTypeBuilder t in subtypes) { match = false; if ((t.attrs & TypeAttributes.VisibilityMask) == TypeAttributes.NestedPublic) @@ -1317,7 +1396,7 @@ public override Type MakeGenericType(params Type[] typeArguments) Type[] copy = new Type[typeArguments.Length]; typeArguments.CopyTo(copy, 0); - return AssemblyBuilder.MakeGenericType(this, copy); + return RuntimeAssemblyBuilder.MakeGenericType(this, copy); } public override Type MakePointerType() @@ -1334,10 +1413,8 @@ public override RuntimeTypeHandle TypeHandle } } - public void SetCustomAttribute(CustomAttributeBuilder customBuilder) + protected override void SetCustomAttributeCore(CustomAttributeBuilder customBuilder) { - ArgumentNullException.ThrowIfNull(customBuilder); - string? attrname = customBuilder.Ctor.ReflectedType!.FullName; if (attrname == "System.Runtime.InteropServices.StructLayoutAttribute") { @@ -1354,7 +1431,7 @@ public void SetCustomAttribute(CustomAttributeBuilder customBuilder) _ => throw new Exception("Error in customattr"), // we should ignore it since it can be any value anyway... }; - Type ctor_type = customBuilder.Ctor is ConstructorBuilder builder ? builder.parameters![0] : customBuilder.Ctor.GetParametersInternal()[0].ParameterType; + Type ctor_type = customBuilder.Ctor is RuntimeConstructorBuilder builder ? builder.parameters![0] : customBuilder.Ctor.GetParametersInternal()[0].ParameterType; int pos = 6; if (ctor_type.FullName == "System.Int16") pos = 4; @@ -1457,43 +1534,41 @@ public void SetCustomAttribute(CustomAttributeBuilder customBuilder) } } - public void SetCustomAttribute(ConstructorInfo con, byte[] binaryAttribute) + protected override void SetCustomAttributeCore(ConstructorInfo con, byte[] binaryAttribute) { - SetCustomAttribute(new CustomAttributeBuilder(con, binaryAttribute)); + SetCustomAttributeCore(new CustomAttributeBuilder(con, binaryAttribute)); } - public EventBuilder DefineEvent(string name, EventAttributes attributes, Type eventtype) + protected override EventBuilder DefineEventCore(string name, EventAttributes attributes, Type eventtype) { check_name(nameof(name), name); ArgumentNullException.ThrowIfNull(eventtype); check_not_created(); - EventBuilder res = new EventBuilder(this, name, attributes, eventtype); + RuntimeEventBuilder res = new RuntimeEventBuilder(this, name, attributes, eventtype); if (events != null) { - EventBuilder[] new_events = new EventBuilder[events.Length + 1]; + RuntimeEventBuilder[] new_events = new RuntimeEventBuilder[events.Length + 1]; Array.Copy(events, new_events, events.Length); new_events[events.Length] = res; events = new_events; } else { - events = new EventBuilder[1]; + events = new RuntimeEventBuilder[1]; events[0] = res; } return res; } - public FieldBuilder DefineInitializedData(string name, byte[] data, FieldAttributes attributes) + protected override FieldBuilder DefineInitializedDataCore(string name, byte[] data, FieldAttributes attributes) { - ArgumentNullException.ThrowIfNull(data); - - FieldBuilder res = DefineUninitializedData(name, data.Length, attributes); + RuntimeFieldBuilder res = (RuntimeFieldBuilder)DefineUninitializedData(name, data.Length, attributes); res.SetRVAData(data); return res; } - public FieldBuilder DefineUninitializedData(string name, int size, FieldAttributes attributes) + protected override FieldBuilder DefineUninitializedDataCore(string name, int size, FieldAttributes attributes) { ArgumentException.ThrowIfNullOrEmpty(name); if ((size <= 0) || (size > 0x3f0000)) @@ -1505,9 +1580,9 @@ public FieldBuilder DefineUninitializedData(string name, int size, FieldAttribut Type? datablobtype = pmodule.GetRegisteredType(fullname.NestedName(ident)); if (datablobtype == null) { - TypeBuilder tb = DefineNestedType(typeName, + TypeBuilder tb = DefineNestedTypeCore(typeName, TypeAttributes.NestedPrivate | TypeAttributes.ExplicitLayout | TypeAttributes.Sealed, - typeof(ValueType), null, FieldBuilder.RVADataPackingSize(size), size); + typeof(ValueType), null, RuntimeFieldBuilder.RVADataPackingSize(size), size); tb.CreateType(); datablobtype = tb; } @@ -1518,7 +1593,7 @@ public FieldBuilder DefineUninitializedData(string name, int size, FieldAttribut [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2074:UnrecognizedReflectionPattern", Justification = "Linker doesn't analyze ResolveUserType but it's an identity function")] - public void SetParent([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type? parent) + protected override void SetParentCore([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type? parent) { check_not_created(); @@ -1641,7 +1716,7 @@ internal bool IsAssignableToInternal([NotNullWhen(true)] Type? c) return c.IsAssignableFrom(parent); } - public bool IsCreated() + protected override bool IsCreatedCore() { return is_created; } @@ -1713,125 +1788,20 @@ public override int GenericParameterPosition } } - public GenericTypeParameterBuilder[] DefineGenericParameters(params string[] names) + protected override GenericTypeParameterBuilder[] DefineGenericParametersCore(params string[] names) { - ArgumentNullException.ThrowIfNull(names); - if (names.Length == 0) - throw new ArgumentException(SR.Arg_EmptyArray, nameof(names)); - - generic_params = new GenericTypeParameterBuilder[names.Length]; + generic_params = new RuntimeGenericTypeParameterBuilder[names.Length]; for (int i = 0; i < names.Length; i++) { string item = names[i]; ArgumentNullException.ThrowIfNull(item, nameof(names)); - generic_params[i] = new GenericTypeParameterBuilder(this, null, item, i); + generic_params[i] = new RuntimeGenericTypeParameterBuilder(this, null, item, i); } return generic_params; } - [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2070:UnrecognizedReflectionPattern", - Justification = "Linker thinks Type.GetConstructor(ConstructorInfo) is one of the public APIs because it doesn't analyze method signatures. We already have ConstructorInfo.")] - [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2055:UnrecognizedReflectionPattern", - Justification = "Type.MakeGenericType is used to create a typical instantiation")] - public static ConstructorInfo GetConstructor(Type type, ConstructorInfo constructor) - { - if (!IsValidGetMethodType(type)) - throw new ArgumentException(SR.Argument_MustBeTypeBuilder, nameof(type)); - - if (type is TypeBuilder && type.ContainsGenericParameters) - type = type.MakeGenericType(type.GetGenericArguments()); - - if (!constructor.DeclaringType!.IsGenericTypeDefinition) - throw new ArgumentException(SR.Argument_ConstructorNeedGenericDeclaringType, nameof(constructor)); - - if (constructor.DeclaringType != type.GetGenericTypeDefinition()) - throw new ArgumentException(SR.Argument_InvalidConstructorDeclaringType, nameof(type)); - - ConstructorInfo res = type.GetConstructor(constructor); - if (res == null) - throw new ArgumentException("constructor not found"); - - return res; - } - - private static bool IsValidGetMethodType(Type type) - { - if (type == null) - return false; - - if (type is TypeBuilder || type is TypeBuilderInstantiation) - return true; - /*GetMethod() must work with TypeBuilders after CreateType() was called.*/ - if (type.Module is ModuleBuilder) - return true; - if (type.IsGenericParameter) - return false; - - Type[] inst = type.GetGenericArguments(); - if (inst == null) - return false; - for (int i = 0; i < inst.Length; ++i) - { - if (IsValidGetMethodType(inst[i])) - return true; - } - return false; - } - - [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2055:UnrecognizedReflectionPattern", - Justification = "Type.MakeGenericType is used to create a typical instantiation")] - public static MethodInfo GetMethod(Type type, MethodInfo method) - { - if (!IsValidGetMethodType(type)) - throw new ArgumentException(SR.Argument_MustBeTypeBuilder, nameof(type)); - - if (type is TypeBuilder && type.ContainsGenericParameters) - type = type.MakeGenericType(type.GetGenericArguments()); - - if (method.IsGenericMethod && !method.IsGenericMethodDefinition) - throw new ArgumentException(SR.Argument_NeedGenericMethodDefinition, nameof(method)); - - if (!method.DeclaringType!.IsGenericTypeDefinition) - throw new ArgumentException(SR.Argument_MethodNeedGenericDeclaringType, nameof(method)); - - if (method.DeclaringType != type.GetGenericTypeDefinition()) - throw new ArgumentException(SR.Argument_InvalidMethodDeclaringType, nameof(type)); - - MethodInfo res = type.GetMethod(method); - if (res == null) - throw new ArgumentException(string.Format("method {0} not found in type {1}", method.Name, type)); - - return res; - } - - [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2055:UnrecognizedReflectionPattern", - Justification = "Type.MakeGenericType is used to create a typical instantiation")] - public static FieldInfo GetField(Type type, FieldInfo field) - { - if (!IsValidGetMethodType(type)) - throw new ArgumentException(SR.Argument_MustBeTypeBuilder, nameof(type)); - - if (type is TypeBuilder && type.ContainsGenericParameters) - type = type.MakeGenericType(type.GetGenericArguments()); - - if (!field.DeclaringType!.IsGenericTypeDefinition) - throw new ArgumentException(SR.Argument_FieldNeedGenericDeclaringType, nameof(field)); - - if (field.DeclaringType != type.GetGenericTypeDefinition()) - throw new ArgumentException(SR.Argument_InvalidFieldDeclaringType, nameof(type)); - - if (field is FieldOnTypeBuilderInst) - throw new ArgumentException("The specified field must be declared on a generic type definition.", nameof(field)); - - FieldInfo res = type.GetField(field); - if (res == null) - throw new System.Exception("field not found"); - else - return res; - } - internal override bool IsUserType { get @@ -1892,7 +1862,7 @@ internal static bool SetConstantValue(Type destType, object? value, ref object? // The above behaviors might not be the most consistent but we have to live with them. Type? underlyingType; - if (destType is EnumBuilder enumBldr) + if (destType is RuntimeEnumBuilder enumBldr) { underlyingType = enumBldr.GetEnumUnderlyingType(); @@ -1902,7 +1872,7 @@ internal static bool SetConstantValue(Type destType, object? value, ref object? type == underlyingType)) throw_argument_ConstantDoesntMatch(); } - else if (destType is TypeBuilder typeBldr) + else if (destType is RuntimeTypeBuilder typeBldr) { underlyingType = typeBldr.underlying_type; diff --git a/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/SignatureHelper.cs b/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/SignatureHelper.cs index 742f7c821421b..48b7c88eb3eb0 100644 --- a/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/SignatureHelper.cs +++ b/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/SignatureHelper.cs @@ -372,7 +372,7 @@ public override int GetHashCode() public byte[] GetSignature() { - TypeBuilder.ResolveUserTypes(arguments); + RuntimeTypeBuilder.ResolveUserTypes(arguments); return type switch { diff --git a/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilderInstantiation.cs b/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilderInstantiation.cs index 3cc5d84c03005..abf9999b10fae 100644 --- a/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilderInstantiation.cs +++ b/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilderInstantiation.cs @@ -102,7 +102,7 @@ internal bool IsCreated { get { - return generic_type is TypeBuilder tb ? tb.is_created : true; + return generic_type is RuntimeTypeBuilder tb ? tb.is_created : true; } } diff --git a/src/mono/mono/metadata/custom-attrs.c b/src/mono/mono/metadata/custom-attrs.c index 46d8f54b94dec..77b0458dcdfba 100644 --- a/src/mono/mono/metadata/custom-attrs.c +++ b/src/mono/mono/metadata/custom-attrs.c @@ -2453,33 +2453,33 @@ mono_reflection_get_custom_attrs_info_checked (MonoObjectHandle obj, MonoError * g_free (type_name); goto leave; } - } else if (strcmp ("AssemblyBuilder", klass_name) == 0) { + } else if (strcmp ("RuntimeAssemblyBuilder", klass_name) == 0) { MonoReflectionAssemblyBuilderHandle assemblyb = MONO_HANDLE_CAST (MonoReflectionAssemblyBuilder, obj); MonoReflectionAssemblyHandle assembly = MONO_HANDLE_CAST (MonoReflectionAssembly, assemblyb); MonoArrayHandle cattrs = MONO_HANDLE_NEW_GET (MonoArray, assemblyb, cattrs); MonoImage * image = MONO_HANDLE_GETVAL (assembly, assembly)->image; g_assert (image); cinfo = mono_custom_attrs_from_builders_handle (NULL, image, cattrs); - } else if (strcmp ("TypeBuilder", klass_name) == 0) { + } else if (strcmp ("RuntimeTypeBuilder", klass_name) == 0) { MonoReflectionTypeBuilderHandle tb = MONO_HANDLE_CAST (MonoReflectionTypeBuilder, obj); MonoReflectionModuleBuilderHandle module = MONO_HANDLE_NEW_GET (MonoReflectionModuleBuilder, tb, module); MonoDynamicImage *dynamic_image = MONO_HANDLE_GETVAL (module, dynamic_image); MonoArrayHandle cattrs = MONO_HANDLE_NEW_GET (MonoArray, tb, cattrs); cinfo = mono_custom_attrs_from_builders_handle (NULL, &dynamic_image->image, cattrs); - } else if (strcmp ("ModuleBuilder", klass_name) == 0) { + } else if (strcmp ("RuntimeModuleBuilder", klass_name) == 0) { MonoReflectionModuleBuilderHandle mb = MONO_HANDLE_CAST (MonoReflectionModuleBuilder, obj); MonoDynamicImage *dynamic_image = MONO_HANDLE_GETVAL (mb, dynamic_image); MonoArrayHandle cattrs = MONO_HANDLE_NEW_GET (MonoArray, mb, cattrs); cinfo = mono_custom_attrs_from_builders_handle (NULL, &dynamic_image->image, cattrs); - } else if (strcmp ("ConstructorBuilder", klass_name) == 0) { + } else if (strcmp ("RuntimeConstructorBuilder", klass_name) == 0) { mono_error_set_not_supported (error, ""); goto leave; - } else if (strcmp ("MethodBuilder", klass_name) == 0) { + } else if (strcmp ("RuntimeMethodBuilder", klass_name) == 0) { MonoReflectionMethodBuilderHandle mb = MONO_HANDLE_CAST (MonoReflectionMethodBuilder, obj); MonoMethod *mhandle = MONO_HANDLE_GETVAL (mb, mhandle); MonoArrayHandle cattrs = MONO_HANDLE_NEW_GET (MonoArray, mb, cattrs); cinfo = mono_custom_attrs_from_builders_handle (NULL, m_class_get_image (mhandle->klass), cattrs); - } else if (strcmp ("FieldBuilder", klass_name) == 0) { + } else if (strcmp ("RuntimeFieldBuilder", klass_name) == 0) { MonoReflectionFieldBuilderHandle fb = MONO_HANDLE_CAST (MonoReflectionFieldBuilder, obj); MonoReflectionTypeBuilderHandle tb = MONO_HANDLE_CAST (MonoReflectionTypeBuilder, MONO_HANDLE_NEW_GET (MonoReflectionType, fb, typeb)); MonoReflectionModuleBuilderHandle mb = MONO_HANDLE_NEW_GET (MonoReflectionModuleBuilder, tb, module); diff --git a/src/mono/mono/metadata/dynamic-image.c b/src/mono/mono/metadata/dynamic-image.c index 8ada324045597..16b82be23ffe3 100644 --- a/src/mono/mono/metadata/dynamic-image.c +++ b/src/mono/mono/metadata/dynamic-image.c @@ -148,7 +148,7 @@ mono_dynamic_image_register_token (MonoDynamicImage *assembly, guint32 token, Mo MONO_REQ_GC_UNSAFE_MODE; g_assert (!MONO_HANDLE_IS_NULL (obj)); - g_assert (strcmp (m_class_get_name (mono_handle_class (obj)), "EnumBuilder")); + g_assert (strcmp (m_class_get_name (mono_handle_class (obj)), "RuntimeEnumBuilder")); dynamic_image_lock (assembly); MonoObject *prev = (MonoObject *)mono_g_hash_table_lookup (assembly->tokens, GUINT_TO_POINTER (token)); if (prev) { diff --git a/src/mono/mono/metadata/icall-def.h b/src/mono/mono/metadata/icall-def.h index 84b050e5ba989..34b0c5deaf418 100644 --- a/src/mono/mono/metadata/icall-def.h +++ b/src/mono/mono/metadata/icall-def.h @@ -294,10 +294,6 @@ HANDLES(MCATTR_1, "GetCustomAttributesDataInternal", ves_icall_MonoCustomAttrs_G HANDLES(MCATTR_2, "GetCustomAttributesInternal", ves_icall_MonoCustomAttrs_GetCustomAttributesInternal, MonoArray, 3, (MonoObject, MonoReflectionType, MonoBoolean)) HANDLES(MCATTR_3, "IsDefinedInternal", ves_icall_MonoCustomAttrs_IsDefinedInternal, MonoBoolean, 2, (MonoObject, MonoReflectionType)) -ICALL_TYPE(ASSEMB, "System.Reflection.Emit.AssemblyBuilder", ASSEMB_1) -HANDLES(ASSEMB_1, "UpdateNativeCustomAttributes", ves_icall_AssemblyBuilder_UpdateNativeCustomAttributes, void, 1, (MonoReflectionAssemblyBuilder)) -HANDLES(ASSEMB_2, "basic_init", ves_icall_AssemblyBuilder_basic_init, void, 1, (MonoReflectionAssemblyBuilder)) - #ifndef DISABLE_REFLECTION_EMIT ICALL_TYPE(CATTRB, "System.Reflection.Emit.CustomAttributeBuilder", CATTRB_1) HANDLES(CATTRB_1, "GetBlob", ves_icall_CustomAttributeBuilder_GetBlob, MonoArray, 7, (MonoReflectionAssembly, MonoObject, MonoArray, MonoArray, MonoArray, MonoArray, MonoArray)) @@ -306,10 +302,14 @@ HANDLES(CATTRB_1, "GetBlob", ves_icall_CustomAttributeBuilder_GetBlob, MonoArray ICALL_TYPE(DYNM, "System.Reflection.Emit.DynamicMethod", DYNM_1) HANDLES(DYNM_1, "create_dynamic_method", ves_icall_DynamicMethod_create_dynamic_method, void, 4, (MonoReflectionDynamicMethod, MonoString, guint32, guint32)) -ICALL_TYPE(ENUMB, "System.Reflection.Emit.EnumBuilder", ENUMB_1) +ICALL_TYPE(ASSEMB, "System.Reflection.Emit.RuntimeAssemblyBuilder", ASSEMB_1) +HANDLES(ASSEMB_1, "UpdateNativeCustomAttributes", ves_icall_AssemblyBuilder_UpdateNativeCustomAttributes, void, 1, (MonoReflectionAssemblyBuilder)) +HANDLES(ASSEMB_2, "basic_init", ves_icall_AssemblyBuilder_basic_init, void, 1, (MonoReflectionAssemblyBuilder)) + +ICALL_TYPE(ENUMB, "System.Reflection.Emit.RuntimeEnumBuilder", ENUMB_1) HANDLES(ENUMB_1, "setup_enum_type", ves_icall_EnumBuilder_setup_enum_type, void, 2, (MonoReflectionType, MonoReflectionType)) -ICALL_TYPE(MODULEB, "System.Reflection.Emit.ModuleBuilder", MODULEB_10) +ICALL_TYPE(MODULEB, "System.Reflection.Emit.RuntimeModuleBuilder", MODULEB_10) HANDLES(MODULEB_10, "GetRegisteredToken", ves_icall_ModuleBuilder_GetRegisteredToken, MonoObject, 2, (MonoReflectionModuleBuilder, guint32)) HANDLES(MODULEB_8, "RegisterToken", ves_icall_ModuleBuilder_RegisterToken, void, 3, (MonoReflectionModuleBuilder, MonoObject, guint32)) HANDLES(MODULEB_2, "basic_init", ves_icall_ModuleBuilder_basic_init, void, 1, (MonoReflectionModuleBuilder)) @@ -318,13 +318,13 @@ HANDLES(MODULEB_6, "getToken", ves_icall_ModuleBuilder_getToken, gint32, 3, (Mon HANDLES(MODULEB_7, "getUSIndex", ves_icall_ModuleBuilder_getUSIndex, guint32, 2, (MonoReflectionModuleBuilder, MonoString)) HANDLES(MODULEB_9, "set_wrappers_type", ves_icall_ModuleBuilder_set_wrappers_type, void, 2, (MonoReflectionModuleBuilder, MonoReflectionType)) +ICALL_TYPE(TYPEB, "System.Reflection.Emit.RuntimeTypeBuilder", TYPEB_1) +HANDLES(TYPEB_1, "create_runtime_class", ves_icall_TypeBuilder_create_runtime_class, MonoReflectionType, 1, (MonoReflectionTypeBuilder)) + ICALL_TYPE(SIGH, "System.Reflection.Emit.SignatureHelper", SIGH_1) HANDLES(SIGH_1, "get_signature_field", ves_icall_SignatureHelper_get_signature_field, MonoArray, 1, (MonoReflectionSigHelper)) HANDLES(SIGH_2, "get_signature_local", ves_icall_SignatureHelper_get_signature_local, MonoArray, 1, (MonoReflectionSigHelper)) -ICALL_TYPE(TYPEB, "System.Reflection.Emit.TypeBuilder", TYPEB_1) -HANDLES(TYPEB_1, "create_runtime_class", ves_icall_TypeBuilder_create_runtime_class, MonoReflectionType, 1, (MonoReflectionTypeBuilder)) - ICALL_TYPE(FIELDI, "System.Reflection.FieldInfo", FILEDI_1) HANDLES(FILEDI_1, "get_marshal_info", ves_icall_System_Reflection_FieldInfo_get_marshal_info, MonoReflectionMarshalAsAttribute, 1, (MonoReflectionField)) HANDLES(FILEDI_2, "internal_from_handle_type", ves_icall_System_Reflection_FieldInfo_internal_from_handle_type, MonoReflectionField, 2, (MonoClassField_ref, MonoType_ref)) diff --git a/src/mono/mono/metadata/object-internals.h b/src/mono/mono/metadata/object-internals.h index d5049ab1eb647..1a6d61e3114de 100644 --- a/src/mono/mono/metadata/object-internals.h +++ b/src/mono/mono/metadata/object-internals.h @@ -1059,7 +1059,7 @@ typedef struct { MonoArray *param_modopt; } MonoReflectionCtorBuilder; -/* Safely access System.Reflection.Emit.ConstructorBuilder from native code */ +/* Safely access System.Reflection.Emit.RuntimeConstructorBuilder from native code */ TYPED_HANDLE_DECL (MonoReflectionCtorBuilder); typedef struct { @@ -1092,7 +1092,7 @@ typedef struct { MonoArray *param_modopt; } MonoReflectionMethodBuilder; -/* Safely access System.Reflection.Emit.MethodBuilder from native code */ +/* Safely access System.Reflection.Emit.RuntimeMethodBuilder from native code */ TYPED_HANDLE_DECL (MonoReflectionMethodBuilder); typedef struct { @@ -1122,7 +1122,7 @@ typedef struct { guint32 access; } MonoReflectionAssemblyBuilder; -/* Safely access System.Reflection.Emit.AssemblyBuilder from native code */ +/* Safely access System.Reflection.Emit.RuntimeAssemblyBuilder from native code */ TYPED_HANDLE_DECL (MonoReflectionAssemblyBuilder); typedef struct { @@ -1141,7 +1141,7 @@ typedef struct { MonoArray *modopt; } MonoReflectionFieldBuilder; -/* Safely access System.Reflection.Emit.FieldBuilder from native code */ +/* Safely access System.Reflection.Emit.RuntimeFieldBuilder from native code */ TYPED_HANDLE_DECL (MonoReflectionFieldBuilder); typedef struct { @@ -1192,7 +1192,7 @@ typedef struct { MonoArray *table_indexes; } MonoReflectionModuleBuilder; -/* Safely acess System.Reflection.Emit.ModuleBuilder from native code */ +/* Safely acess System.Reflection.Emit.RuntimeModuleBuilder from native code */ TYPED_HANDLE_DECL (MonoReflectionModuleBuilder); typedef enum { @@ -1258,7 +1258,7 @@ typedef struct { guint32 attrs; } MonoReflectionGenericParam; -/* Safely access System.Reflection.Emit.GenericTypeParameterBuilder from native code */ +/* Safely access System.Reflection.Emit.RuntimeGenericTypeParameterBuilder from native code */ TYPED_HANDLE_DECL (MonoReflectionGenericParam); typedef struct { @@ -1266,7 +1266,7 @@ typedef struct { MonoReflectionTypeBuilder *tb; } MonoReflectionEnumBuilder; -/* Safely access System.Reflection.Emit.EnumBuilder from native code */ +/* Safely access System.Reflection.Emit.RuntimeEnumBuilder from native code */ TYPED_HANDLE_DECL (MonoReflectionEnumBuilder); typedef struct _MonoReflectionGenericClass MonoReflectionGenericClass; diff --git a/src/mono/mono/metadata/reflection.c b/src/mono/mono/metadata/reflection.c index 487a31f4ec317..c56703d0e9aee 100644 --- a/src/mono/mono/metadata/reflection.c +++ b/src/mono/mono/metadata/reflection.c @@ -69,7 +69,7 @@ static GENERATE_GET_CLASS_WITH_CACHE (missing, "System.Reflection", "Missing"); static GENERATE_GET_CLASS_WITH_CACHE (method_body, "System.Reflection", "RuntimeMethodBody"); static GENERATE_GET_CLASS_WITH_CACHE (local_variable_info, "System.Reflection", "RuntimeLocalVariableInfo"); static GENERATE_GET_CLASS_WITH_CACHE (exception_handling_clause, "System.Reflection", "RuntimeExceptionHandlingClause"); -static GENERATE_GET_CLASS_WITH_CACHE (type_builder, "System.Reflection.Emit", "TypeBuilder"); +static GENERATE_GET_CLASS_WITH_CACHE (type_builder, "System.Reflection.Emit", "RuntimeTypeBuilder"); static GENERATE_GET_CLASS_WITH_CACHE (dbnull, "System", "DBNull"); @@ -2493,17 +2493,17 @@ mono_reflection_get_token_checked (MonoObjectHandle obj, MonoError *error) MonoClass *klass = mono_handle_class (obj); const char *klass_name = m_class_get_name (klass); - if (strcmp (klass_name, "MethodBuilder") == 0) { + if (strcmp (klass_name, "RuntimeMethodBuilder") == 0) { MonoReflectionMethodBuilderHandle mb = MONO_HANDLE_CAST (MonoReflectionMethodBuilder, obj); token = MONO_HANDLE_GETVAL (mb, table_idx) | MONO_TOKEN_METHOD_DEF; - } else if (strcmp (klass_name, "ConstructorBuilder") == 0) { + } else if (strcmp (klass_name, "RuntimeConstructorBuilder") == 0) { MonoReflectionCtorBuilderHandle mb = MONO_HANDLE_CAST (MonoReflectionCtorBuilder, obj); token = MONO_HANDLE_GETVAL (mb, table_idx) | MONO_TOKEN_METHOD_DEF; - } else if (strcmp (klass_name, "FieldBuilder") == 0) { + } else if (strcmp (klass_name, "RuntimeFieldBuilder") == 0) { g_assert_not_reached (); - } else if (strcmp (klass_name, "TypeBuilder") == 0) { + } else if (strcmp (klass_name, "RuntimeTypeBuilder") == 0) { MonoReflectionTypeBuilderHandle tb = MONO_HANDLE_CAST (MonoReflectionTypeBuilder, obj); token = MONO_HANDLE_GETVAL (tb, table_idx) | MONO_TOKEN_TYPE_DEF; } else if (strcmp (klass_name, "RuntimeType") == 0) { @@ -2551,7 +2551,7 @@ mono_reflection_get_token_checked (MonoObjectHandle obj, MonoError *error) MonoMethod *method = MONO_HANDLE_GETVAL (MONO_HANDLE_CAST (MonoReflectionMethod, member_impl), method); token = mono_method_get_param_token (method, position); - } else if (strcmp (klass_name, "RuntimeModule") == 0 || strcmp (klass_name, "ModuleBuilder") == 0) { + } else if (strcmp (klass_name, "RuntimeModule") == 0 || strcmp (klass_name, "RuntimeModuleBuilder") == 0) { MonoReflectionModuleHandle m = MONO_HANDLE_CAST (MonoReflectionModule, obj); token = MONO_HANDLE_GETVAL (m, token); @@ -2713,7 +2713,7 @@ MonoReflectionMethodHandle ves_icall_RuntimeMethodInfo_MakeGenericMethod_impl (MonoReflectionMethodHandle rmethod, MonoArrayHandle types, MonoError *error) { error_init (error); - g_assert (0 != strcmp (m_class_get_name (mono_handle_class (rmethod)), "MethodBuilder")); + g_assert (0 != strcmp (m_class_get_name (mono_handle_class (rmethod)), "RuntimeMethodBuilder")); MonoMethod *method = MONO_HANDLE_GETVAL (rmethod, method); MonoMethod *imethod = reflection_bind_generic_method_parameters (method, types, error); @@ -3185,7 +3185,7 @@ mono_reflection_call_is_assignable_to (MonoClass *klass, MonoClass *oklass, Mono * need a TypeBuilder so use mono_class_get_ref_info (klass). */ g_assert (mono_class_has_ref_info (klass)); - g_assert (!strcmp (m_class_get_name (mono_object_class (&mono_class_get_ref_info_raw (klass)->type.object)), "TypeBuilder")); /* FIXME use handles */ + g_assert (!strcmp (m_class_get_name (mono_object_class (&mono_class_get_ref_info_raw (klass)->type.object)), "RuntimeTypeBuilder")); /* FIXME use handles */ params [0] = mono_type_get_object_checked (m_class_get_byval_arg (oklass), error); return_val_if_nok (error, FALSE); diff --git a/src/mono/mono/metadata/sre.c b/src/mono/mono/metadata/sre.c index b8f2e6a7a88cf..e8e360ae52fac 100644 --- a/src/mono/mono/metadata/sre.c +++ b/src/mono/mono/metadata/sre.c @@ -66,7 +66,7 @@ dyn_methods_unlock (void) static GENERATE_GET_CLASS_WITH_CACHE (marshal_as_attribute, "System.Runtime.InteropServices", "MarshalAsAttribute"); #ifndef DISABLE_REFLECTION_EMIT -static GENERATE_GET_CLASS_WITH_CACHE (module_builder, "System.Reflection.Emit", "ModuleBuilder"); +static GENERATE_GET_CLASS_WITH_CACHE (module_builder, "System.Reflection.Emit", "RuntimeModuleBuilder"); #endif static char* string_to_utf8_image_raw (MonoImage *image, MonoString *s, MonoError *error); @@ -1022,7 +1022,7 @@ mono_image_create_method_token (MonoDynamicImage *assembly, MonoObjectHandle obj g_assert (!MONO_HANDLE_IS_NULL (opt_param_types) && (mono_method_signature_internal (method)->sentinelpos >= 0)); token = create_method_token (assembly, method, opt_param_types, error); goto_if_nok (error, fail); - } else if (strcmp (klass->name, "MethodBuilder") == 0) { + } else if (strcmp (klass->name, "RuntimeMethodBuilder") == 0) { g_assert_not_reached (); } else { g_error ("requested method token for %s\n", klass->name); @@ -1473,37 +1473,37 @@ is_sre_generic_instance (MonoClass *klass) static gboolean is_sre_type_builder (MonoClass *klass) { - check_corlib_type_cached (klass, "System.Reflection.Emit", "TypeBuilder"); + check_corlib_type_cached (klass, "System.Reflection.Emit", "RuntimeTypeBuilder"); } static gboolean is_sre_method_builder (MonoClass *klass) { - check_corlib_type_cached (klass, "System.Reflection.Emit", "MethodBuilder"); + check_corlib_type_cached (klass, "System.Reflection.Emit", "RuntimeMethodBuilder"); } gboolean mono_is_sre_ctor_builder (MonoClass *klass) { - check_corlib_type_cached (klass, "System.Reflection.Emit", "ConstructorBuilder"); + check_corlib_type_cached (klass, "System.Reflection.Emit", "RuntimeConstructorBuilder"); } static gboolean is_sre_field_builder (MonoClass *klass) { - check_corlib_type_cached (klass, "System.Reflection.Emit", "FieldBuilder"); + check_corlib_type_cached (klass, "System.Reflection.Emit", "RuntimeFieldBuilder"); } static gboolean is_sre_gparam_builder (MonoClass *klass) { - check_corlib_type_cached (klass, "System.Reflection.Emit", "GenericTypeParameterBuilder"); + check_corlib_type_cached (klass, "System.Reflection.Emit", "RuntimeGenericTypeParameterBuilder"); } static gboolean is_sre_enum_builder (MonoClass *klass) { - check_corlib_type_cached (klass, "System.Reflection.Emit", "EnumBuilder"); + check_corlib_type_cached (klass, "System.Reflection.Emit", "RuntimeEnumBuilder"); } gboolean @@ -1872,7 +1872,7 @@ get_prop_name_and_type (MonoObject *prop, char **name, MonoType **type, MonoErro { error_init (error); MonoClass *klass = mono_object_class (prop); - if (strcmp (klass->name, "PropertyBuilder") == 0) { + if (strcmp (klass->name, "RuntimePropertyBuilder") == 0) { MonoReflectionPropertyBuilder *pb = (MonoReflectionPropertyBuilder *)prop; *name = mono_string_to_utf8_checked_internal (pb->name, error); return_if_nok (error); @@ -1892,7 +1892,7 @@ get_field_name_and_type (MonoObject *field, char **name, MonoType **type, MonoEr { error_init (error); MonoClass *klass = mono_object_class (field); - if (strcmp (klass->name, "FieldBuilder") == 0) { + if (strcmp (klass->name, "RuntimeFieldBuilder") == 0) { MonoReflectionFieldBuilder *fb = (MonoReflectionFieldBuilder *)field; *name = mono_string_to_utf8_checked_internal (fb->name, error); return_if_nok (error); @@ -3413,7 +3413,7 @@ mono_reflection_get_dynamic_overrides (MonoClass *klass, MonoMethod ***overrides return; tb = mono_class_get_ref_info_raw (klass); /* FIXME use handles */ - g_assert (strcmp (mono_object_class (tb)->name, "TypeBuilder") == 0); + g_assert (strcmp (mono_object_class (tb)->name, "RuntimeTypeBuilder") == 0); onum = 0; if (tb->methods) { @@ -4198,7 +4198,7 @@ mono_reflection_resolve_object (MonoImage *image, MonoObject *obj, MonoClass **h } *handle_class = mono_defaults.fieldhandle_class; g_assert (result); - } else if (strcmp (oklass->name, "TypeBuilder") == 0) { + } else if (strcmp (oklass->name, "RuntimeTypeBuilder") == 0) { MonoReflectionTypeBuilderHandle tb = MONO_HANDLE_NEW (MonoReflectionTypeBuilder, (MonoReflectionTypeBuilder*)obj); MonoType *type = mono_reflection_type_get_handle (&MONO_HANDLE_RAW (tb)->type, error); goto_if_nok (error, return_null);