diff --git a/src/coreclr/System.Private.CoreLib/System.Private.CoreLib.csproj b/src/coreclr/System.Private.CoreLib/System.Private.CoreLib.csproj index ac9e8d9c3a7c5f..1592a5b94bd26a 100644 --- a/src/coreclr/System.Private.CoreLib/System.Private.CoreLib.csproj +++ b/src/coreclr/System.Private.CoreLib/System.Private.CoreLib.csproj @@ -163,7 +163,6 @@ - @@ -177,9 +176,6 @@ - - - diff --git a/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeModuleBuilder.cs b/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeModuleBuilder.cs index d607dc939782a1..637f7cd6c7e95f 100644 --- a/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeModuleBuilder.cs +++ b/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeModuleBuilder.cs @@ -400,11 +400,11 @@ private static MethodBase GetGenericMethodBaseDefinition(MethodBase methodBase) if (methodBase is MethodOnTypeBuilderInstantiation motbi) { - methDef = motbi.m_method; + methDef = motbi._method; } else if (methodBase is ConstructorOnTypeBuilderInstantiation cotbi) { - methDef = cotbi.m_ctor; + methDef = cotbi._ctor; } else if (methodBase is MethodBuilder || methodBase is ConstructorBuilder) { @@ -445,15 +445,15 @@ internal SignatureHelper GetMemberRefSignature(MethodBase? method, int cGenericP return methodBuilder.GetMethodSignature(); case RuntimeConstructorBuilder constructorBuilder: return constructorBuilder.GetMethodSignature(); - case MethodOnTypeBuilderInstantiation motbi when motbi.m_method is RuntimeMethodBuilder methodBuilder: + case MethodOnTypeBuilderInstantiation motbi when motbi._method is RuntimeMethodBuilder methodBuilder: return methodBuilder.GetMethodSignature(); case MethodOnTypeBuilderInstantiation motbi: - method = motbi.m_method; + method = motbi._method; break; - case ConstructorOnTypeBuilderInstantiation cotbi when cotbi.m_ctor is RuntimeConstructorBuilder constructorBuilder: + case ConstructorOnTypeBuilderInstantiation cotbi when cotbi._ctor is RuntimeConstructorBuilder constructorBuilder: return constructorBuilder.GetMethodSignature(); case ConstructorOnTypeBuilderInstantiation cotbi: - method = cotbi.m_ctor; + method = cotbi._ctor; break; } diff --git a/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/XXXOnTypeBuilderInstantiation.cs b/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/XXXOnTypeBuilderInstantiation.cs deleted file mode 100644 index 1841a8cfe0ff1e..00000000000000 --- a/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/XXXOnTypeBuilderInstantiation.cs +++ /dev/null @@ -1,265 +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; -using System.Diagnostics; -using System.Diagnostics.CodeAnalysis; - -namespace System.Reflection.Emit -{ - internal sealed class MethodOnTypeBuilderInstantiation : MethodInfo - { - #region Private Static Members - internal static MethodInfo GetMethod(MethodInfo method, TypeBuilderInstantiation type) - { - return new MethodOnTypeBuilderInstantiation(method, type); - } - #endregion - - #region Private Data Members - internal MethodInfo m_method; - private TypeBuilderInstantiation m_type; - #endregion - - #region Constructor - internal MethodOnTypeBuilderInstantiation(MethodInfo method, TypeBuilderInstantiation type) - { - Debug.Assert(method is MethodBuilder || method is RuntimeMethodInfo); - - m_method = method; - m_type = type; - } - #endregion - - internal override Type[] GetParameterTypes() - { - return m_method.GetParameterTypes(); - } - - #region MemberInfo Overrides - public override MemberTypes MemberType => m_method.MemberType; - public override string Name => m_method.Name; - public override Type? DeclaringType => m_type; - public override Type? ReflectedType => m_type; - public override object[] GetCustomAttributes(bool inherit) { return m_method.GetCustomAttributes(inherit); } - public override object[] GetCustomAttributes(Type attributeType, bool inherit) { return m_method.GetCustomAttributes(attributeType, inherit); } - public override bool IsDefined(Type attributeType, bool inherit) { return m_method.IsDefined(attributeType, inherit); } - public override Module Module => m_method.Module; - #endregion - - #region MethodBase Members - public override ParameterInfo[] GetParameters() { return m_method.GetParameters(); } - public override MethodImplAttributes GetMethodImplementationFlags() { return m_method.GetMethodImplementationFlags(); } - public override RuntimeMethodHandle MethodHandle => m_method.MethodHandle; - public override MethodAttributes Attributes => m_method.Attributes; - public override object Invoke(object? obj, BindingFlags invokeAttr, Binder? binder, object?[]? parameters, CultureInfo? culture) - { - throw new NotSupportedException(); - } - public override CallingConventions CallingConvention => m_method.CallingConvention; - public override Type[] GetGenericArguments() { return m_method.GetGenericArguments(); } - public override MethodInfo GetGenericMethodDefinition() { return m_method; } - public override bool IsGenericMethodDefinition => m_method.IsGenericMethodDefinition; - public override bool ContainsGenericParameters => m_method.ContainsGenericParameters; - - [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[] typeArgs) - { - if (!IsGenericMethodDefinition) - throw new InvalidOperationException(SR.Format(SR.Arg_NotGenericMethodDefinition, this)); - - return MethodBuilderInstantiation.MakeGenericMethod(this, typeArgs); - } - - public override bool IsGenericMethod => m_method.IsGenericMethod; - - #endregion - - #region Public Abstract\Virtual Members - public override Type ReturnType => m_method.ReturnType; - public override ParameterInfo ReturnParameter => throw new NotSupportedException(); - public override ICustomAttributeProvider ReturnTypeCustomAttributes => throw new NotSupportedException(); - public override MethodInfo GetBaseDefinition() { throw new NotSupportedException(); } - #endregion - } - - internal sealed class ConstructorOnTypeBuilderInstantiation : ConstructorInfo - { - #region Private Static Members - internal static ConstructorInfo GetConstructor(ConstructorInfo Constructor, TypeBuilderInstantiation type) - { - return new ConstructorOnTypeBuilderInstantiation(Constructor, type); - } - #endregion - - #region Private Data Members - internal ConstructorInfo m_ctor; - private TypeBuilderInstantiation m_type; - #endregion - - #region Constructor - internal ConstructorOnTypeBuilderInstantiation(ConstructorInfo constructor, TypeBuilderInstantiation type) - { - Debug.Assert(constructor is ConstructorBuilder || constructor is RuntimeConstructorInfo); - - m_ctor = constructor; - m_type = type; - } - #endregion - - internal override Type[] GetParameterTypes() - { - return m_ctor.GetParameterTypes(); - } - - internal override Type GetReturnType() - { - return m_type; - } - - #region MemberInfo Overrides - public override MemberTypes MemberType => m_ctor.MemberType; - public override string Name => m_ctor.Name; - public override Type? DeclaringType => m_type; - public override Type? ReflectedType => m_type; - public override object[] GetCustomAttributes(bool inherit) { return m_ctor.GetCustomAttributes(inherit); } - public override object[] GetCustomAttributes(Type attributeType, bool inherit) { return m_ctor.GetCustomAttributes(attributeType, inherit); } - public override bool IsDefined(Type attributeType, bool inherit) { return m_ctor.IsDefined(attributeType, inherit); } - public override int MetadataToken - { - get - { - ConstructorBuilder? cb = m_ctor as ConstructorBuilder; - - if (cb != null) - return cb.MetadataToken; - else - { - Debug.Assert(m_ctor is RuntimeConstructorInfo); - return m_ctor.MetadataToken; - } - } - } - public override Module Module => m_ctor.Module; - #endregion - - #region MethodBase Members - public override ParameterInfo[] GetParameters() { return m_ctor.GetParameters(); } - public override MethodImplAttributes GetMethodImplementationFlags() { return m_ctor.GetMethodImplementationFlags(); } - public override RuntimeMethodHandle MethodHandle => m_ctor.MethodHandle; - public override MethodAttributes Attributes => m_ctor.Attributes; - public override object Invoke(object? obj, BindingFlags invokeAttr, Binder? binder, object?[]? parameters, CultureInfo? culture) - { - throw new NotSupportedException(); - } - public override CallingConventions CallingConvention => m_ctor.CallingConvention; - public override Type[] GetGenericArguments() { return m_ctor.GetGenericArguments(); } - public override bool IsGenericMethodDefinition => false; - public override bool ContainsGenericParameters => false; - - public override bool IsGenericMethod => false; - #endregion - - #region ConstructorInfo Members - public override object Invoke(BindingFlags invokeAttr, Binder? binder, object?[]? parameters, CultureInfo? culture) - { - throw new InvalidOperationException(); - } - #endregion - } - - internal sealed class FieldOnTypeBuilderInstantiation : FieldInfo - { - #region Private Static Members - internal static FieldInfo GetField(FieldInfo Field, TypeBuilderInstantiation type) - { - FieldInfo m; - - // This ifdef was introduced when non-generic collections were pulled from - // silverlight. See code:Dictionary#DictionaryVersusHashtableThreadSafety - // for information about this change. - // - // There is a pre-existing race condition in this code with the side effect - // that the second thread's value clobbers the first in the hashtable. This is - // an acceptable race condition since we make no guarantees that this will return the - // same object. - // - // We're not entirely sure if this cache helps any specific scenarios, so - // long-term, one could investigate whether it's needed. In any case, this - // method isn't expected to be on any critical paths for performance. - if (type.m_hashtable.Contains(Field)) - { - m = (type.m_hashtable[Field] as FieldInfo)!; - } - else - { - m = new FieldOnTypeBuilderInstantiation(Field, type); - type.m_hashtable[Field] = m; - } - - return m; - } - #endregion - - #region Private Data Members - private FieldInfo m_field; - private TypeBuilderInstantiation m_type; - #endregion - - #region Constructor - internal FieldOnTypeBuilderInstantiation(FieldInfo field, TypeBuilderInstantiation type) - { - Debug.Assert(field is FieldBuilder || field is RuntimeFieldInfo); - - m_field = field; - m_type = type; - } - #endregion - - internal FieldInfo FieldInfo => m_field; - - #region MemberInfo Overrides - public override MemberTypes MemberType => System.Reflection.MemberTypes.Field; - public override string Name => m_field.Name; - public override Type? DeclaringType => m_type; - public override Type? ReflectedType => m_type; - public override object[] GetCustomAttributes(bool inherit) { return m_field.GetCustomAttributes(inherit); } - public override object[] GetCustomAttributes(Type attributeType, bool inherit) { return m_field.GetCustomAttributes(attributeType, inherit); } - public override bool IsDefined(Type attributeType, bool inherit) { return m_field.IsDefined(attributeType, inherit); } - public override int MetadataToken - { - get - { - FieldBuilder? fb = m_field as FieldBuilder; - - if (fb != null) - return fb.MetadataToken; - else - { - Debug.Assert(m_field is RuntimeFieldInfo); - return m_field.MetadataToken; - } - } - } - public override Module Module => m_field.Module; - #endregion - - #region Public Abstract\Virtual Members - public override Type[] GetRequiredCustomModifiers() { return m_field.GetRequiredCustomModifiers(); } - public override Type[] GetOptionalCustomModifiers() { return m_field.GetOptionalCustomModifiers(); } - public override void SetValueDirect(TypedReference obj, object value) - { - throw new NotImplementedException(); - } - public override object GetValueDirect(TypedReference obj) - { - throw new NotImplementedException(); - } - public override RuntimeFieldHandle FieldHandle => throw new NotImplementedException(); - public override Type FieldType => throw new NotImplementedException(); - public override object GetValue(object? obj) { throw new InvalidOperationException(); } - public override void SetValue(object? obj, object? value, BindingFlags invokeAttr, Binder? binder, CultureInfo? culture) { throw new InvalidOperationException(); } - public override FieldAttributes Attributes => m_field.Attributes; - #endregion - } -} diff --git a/src/coreclr/System.Private.CoreLib/src/System/Reflection/MethodBase.CoreCLR.cs b/src/coreclr/System.Private.CoreLib/src/System/Reflection/MethodBase.CoreCLR.cs index 2aba47a3e79ba5..afed33fe1f3bc2 100644 --- a/src/coreclr/System.Private.CoreLib/src/System/Reflection/MethodBase.CoreCLR.cs +++ b/src/coreclr/System.Private.CoreLib/src/System/Reflection/MethodBase.CoreCLR.cs @@ -48,24 +48,5 @@ public abstract partial class MethodBase : MemberInfo internal virtual ParameterInfo[] GetParametersNoCopy() { return GetParameters(); } #endregion - - #region Internal Methods - // helper method to construct the string representation of the parameter list - - internal virtual Type[] GetParameterTypes() - { - ParameterInfo[] paramInfo = GetParametersNoCopy(); - if (paramInfo.Length == 0) - { - return Type.EmptyTypes; - } - - Type[] parameterTypes = new Type[paramInfo.Length]; - for (int i = 0; i < paramInfo.Length; i++) - parameterTypes[i] = paramInfo[i].ParameterType; - - return parameterTypes; - } - #endregion } } 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 7fa24c7ab00f82..dab34b18c3bb41 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 @@ -638,15 +638,19 @@ + + + + @@ -656,7 +660,9 @@ + + diff --git a/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/ConstructorOnTypeBuilderInstantiation.cs b/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/ConstructorOnTypeBuilderInstantiation.cs new file mode 100644 index 00000000000000..98ef5f8cccedc6 --- /dev/null +++ b/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/ConstructorOnTypeBuilderInstantiation.cs @@ -0,0 +1,92 @@ +// 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; +using System.Globalization; + +namespace System.Reflection.Emit +{ + internal sealed partial class ConstructorOnTypeBuilderInstantiation : ConstructorInfo + { + #region Private Static Members + internal static ConstructorInfo GetConstructor(ConstructorInfo Constructor, TypeBuilderInstantiation type) + { + return new ConstructorOnTypeBuilderInstantiation(Constructor, type); + } + #endregion + + #region Private Data Members + internal ConstructorInfo _ctor; + private TypeBuilderInstantiation _type; + #endregion + + #region Constructor + internal ConstructorOnTypeBuilderInstantiation(ConstructorInfo constructor, TypeBuilderInstantiation type) + { + Debug.Assert(constructor is ConstructorBuilder || constructor is RuntimeConstructorInfo); + + _ctor = constructor; + _type = type; + } + #endregion + + #region Internal Overrides + internal override Type[] GetParameterTypes() + { + return _ctor.GetParameterTypes(); + } + #endregion + + #region MemberInfo Overrides + public override MemberTypes MemberType => _ctor.MemberType; + public override string Name => _ctor.Name; + public override Type? DeclaringType => _type; + public override Type? ReflectedType => _type; + public override object[] GetCustomAttributes(bool inherit) { return _ctor.GetCustomAttributes(inherit); } + public override object[] GetCustomAttributes(Type attributeType, bool inherit) { return _ctor.GetCustomAttributes(attributeType, inherit); } + public override bool IsDefined(Type attributeType, bool inherit) { return _ctor.IsDefined(attributeType, inherit); } + public override int MetadataToken + { + get + { + ConstructorBuilder? cb = _ctor as ConstructorBuilder; + + if (cb != null) + { + return cb.MetadataToken; + } + else + { + Debug.Assert(_ctor is RuntimeConstructorInfo); + return _ctor.MetadataToken; + } + } + } + public override Module Module => _ctor.Module; + #endregion + + #region MethodBase Members + public override ParameterInfo[] GetParameters() { return _ctor.GetParameters(); } + public override MethodImplAttributes GetMethodImplementationFlags() { return _ctor.GetMethodImplementationFlags(); } + public override RuntimeMethodHandle MethodHandle => _ctor.MethodHandle; + public override MethodAttributes Attributes => _ctor.Attributes; + public override object Invoke(object? obj, BindingFlags invokeAttr, Binder? binder, object?[]? parameters, CultureInfo? culture) + { + throw new NotSupportedException(); + } + public override CallingConventions CallingConvention => _ctor.CallingConvention; + public override Type[] GetGenericArguments() { return _ctor.GetGenericArguments(); } + public override bool IsGenericMethodDefinition => false; + public override bool ContainsGenericParameters => false; + + public override bool IsGenericMethod => false; + #endregion + + #region ConstructorInfo Members + public override object Invoke(BindingFlags invokeAttr, Binder? binder, object?[]? parameters, CultureInfo? culture) + { + throw new InvalidOperationException(); + } + #endregion + } +} diff --git a/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/FieldOnTypeBuilderInstantiation.cs b/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/FieldOnTypeBuilderInstantiation.cs new file mode 100644 index 00000000000000..bd851d3cf6dc12 --- /dev/null +++ b/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/FieldOnTypeBuilderInstantiation.cs @@ -0,0 +1,94 @@ +// 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; +using System.Globalization; + +namespace System.Reflection.Emit +{ + internal sealed partial class FieldOnTypeBuilderInstantiation : FieldInfo + { + #region Private Static Members + internal static FieldInfo GetField(FieldInfo Field, TypeBuilderInstantiation type) + { + FieldInfo m; + + if (type._hashtable.Contains(Field)) + { + m = (type._hashtable[Field] as FieldInfo)!; + } + else + { + m = new FieldOnTypeBuilderInstantiation(Field, type); + type._hashtable[Field] = m; + } + + return m; + } + #endregion + + #region Private Data Members + private FieldInfo _field; + private TypeBuilderInstantiation _type; + #endregion + + #region Constructor + internal FieldOnTypeBuilderInstantiation(FieldInfo field, TypeBuilderInstantiation type) + { + Debug.Assert(field is FieldBuilder || field is RuntimeFieldInfo); + + _field = field; + _type = type; + } + #endregion + + internal FieldInfo FieldInfo => _field; + + #region MemberInfo Overrides + public override MemberTypes MemberType => System.Reflection.MemberTypes.Field; + public override string Name => _field.Name; + public override Type? DeclaringType => _type; + public override Type? ReflectedType => _type; + public override object[] GetCustomAttributes(bool inherit) { return _field.GetCustomAttributes(inherit); } + public override object[] GetCustomAttributes(Type attributeType, bool inherit) { return _field.GetCustomAttributes(attributeType, inherit); } + public override bool IsDefined(Type attributeType, bool inherit) { return _field.IsDefined(attributeType, inherit); } + public override int MetadataToken + { + get + { + FieldBuilder? fb = _field as FieldBuilder; + + if (fb != null) + { + return fb.MetadataToken; + } + else + { + Debug.Assert(_field is RuntimeFieldInfo); + return _field.MetadataToken; + } + } + } + public override Module Module => _field.Module; + #endregion + + #region Public Abstract\Virtual Members + public override Type[] GetRequiredCustomModifiers() { return _field.GetRequiredCustomModifiers(); } + public override Type[] GetOptionalCustomModifiers() { return _field.GetOptionalCustomModifiers(); } + public override void SetValueDirect(TypedReference obj, object value) + { + throw new NotImplementedException(); + } + public override object GetValueDirect(TypedReference obj) + { + throw new NotImplementedException(); + } + public override RuntimeFieldHandle FieldHandle => throw new NotImplementedException(); + public override Type FieldType => throw new NotImplementedException(); + public override object GetValue(object? obj) { throw new InvalidOperationException(); } + public override void SetValue(object? obj, object? value, BindingFlags invokeAttr, Binder? binder, CultureInfo? culture) { throw new InvalidOperationException(); } + public override FieldAttributes Attributes => _field.Attributes; + #endregion + + } +} diff --git a/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/MethodBuilderInstantiation.cs b/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/MethodBuilderInstantiation.cs similarity index 70% rename from src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/MethodBuilderInstantiation.cs rename to src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/MethodBuilderInstantiation.cs index 655f018c24f139..c9f09711b15f51 100644 --- a/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/MethodBuilderInstantiation.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/MethodBuilderInstantiation.cs @@ -1,4 +1,4 @@ -// Licensed to the .NET Foundation under one or more agreements. +// 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; @@ -20,59 +20,63 @@ internal static MethodInfo MakeGenericMethod(MethodInfo method, Type[] inst) #endregion #region Private Data Members - internal MethodInfo m_method; - private Type[] m_inst; + internal MethodInfo _method; + private Type[] _inst; #endregion #region Constructor internal MethodBuilderInstantiation(MethodInfo method, Type[] inst) { - m_method = method; - m_inst = inst; + _method = method; + _inst = inst; } #endregion internal override Type[] GetParameterTypes() { - return m_method.GetParameterTypes(); + return _method.GetParameterTypes(); } #region MemberBase - public override MemberTypes MemberType => m_method.MemberType; - public override string Name => m_method.Name; - public override Type? DeclaringType => m_method.DeclaringType; - public override Type? ReflectedType => m_method.ReflectedType; - public override object[] GetCustomAttributes(bool inherit) { return m_method.GetCustomAttributes(inherit); } - public override object[] GetCustomAttributes(Type attributeType, bool inherit) { return m_method.GetCustomAttributes(attributeType, inherit); } - public override bool IsDefined(Type attributeType, bool inherit) { return m_method.IsDefined(attributeType, inherit); } - public override Module Module => m_method.Module; + public override MemberTypes MemberType => _method.MemberType; + public override string Name => _method.Name; + public override Type? DeclaringType => _method.DeclaringType; + public override Type? ReflectedType => _method.ReflectedType; + public override object[] GetCustomAttributes(bool inherit) { return _method.GetCustomAttributes(inherit); } + public override object[] GetCustomAttributes(Type attributeType, bool inherit) { return _method.GetCustomAttributes(attributeType, inherit); } + public override bool IsDefined(Type attributeType, bool inherit) { return _method.IsDefined(attributeType, inherit); } + public override Module Module => _method.Module; #endregion #region MethodBase Members public override ParameterInfo[] GetParameters() { throw new NotSupportedException(); } - public override MethodImplAttributes GetMethodImplementationFlags() { return m_method.GetMethodImplementationFlags(); } + public override MethodImplAttributes GetMethodImplementationFlags() { return _method.GetMethodImplementationFlags(); } public override RuntimeMethodHandle MethodHandle => throw new NotSupportedException(SR.NotSupported_DynamicModule); - public override MethodAttributes Attributes => m_method.Attributes; + public override MethodAttributes Attributes => _method.Attributes; public override object Invoke(object? obj, BindingFlags invokeAttr, Binder? binder, object?[]? parameters, CultureInfo? culture) { throw new NotSupportedException(); } - public override CallingConventions CallingConvention => m_method.CallingConvention; - public override Type[] GetGenericArguments() { return m_inst; } - public override MethodInfo GetGenericMethodDefinition() { return m_method; } + public override CallingConventions CallingConvention => _method.CallingConvention; + public override Type[] GetGenericArguments() { return _inst; } + public override MethodInfo GetGenericMethodDefinition() { return _method; } public override bool IsGenericMethodDefinition => false; public override bool ContainsGenericParameters { get { - for (int i = 0; i < m_inst.Length; i++) + for (int i = 0; i < _inst.Length; i++) { - if (m_inst[i].ContainsGenericParameters) + if (_inst[i].ContainsGenericParameters) + { return true; + } } if (DeclaringType != null && DeclaringType.ContainsGenericParameters) + { return true; + } return false; } @@ -89,7 +93,7 @@ public override MethodInfo MakeGenericMethod(params Type[] arguments) #endregion #region Public Abstract\Virtual Members - public override Type ReturnType => m_method.ReturnType; + public override Type ReturnType => _method.ReturnType; public override ParameterInfo ReturnParameter => throw new NotSupportedException(); public override ICustomAttributeProvider ReturnTypeCustomAttributes => throw new NotSupportedException(); diff --git a/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/MethodOnTypeBuilderInstantiation.cs b/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/MethodOnTypeBuilderInstantiation.cs new file mode 100644 index 00000000000000..33b480df296a07 --- /dev/null +++ b/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/MethodOnTypeBuilderInstantiation.cs @@ -0,0 +1,121 @@ +// 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.Diagnostics; +using System.Globalization; + +namespace System.Reflection.Emit +{ + internal sealed partial class MethodOnTypeBuilderInstantiation : MethodInfo + { + #region Internal Static Members + internal static MethodInfo GetMethod(MethodInfo method, TypeBuilderInstantiation type) + { + return new MethodOnTypeBuilderInstantiation(method, type); + } + #endregion + + #region Private Data Members + internal MethodInfo _method; + private Type _type; + #endregion + + #region Constructor + internal MethodOnTypeBuilderInstantiation(MethodInfo method, Type type) + { + Debug.Assert(method is MethodBuilder || method is RuntimeMethodInfo); + + _method = method; + _type = type; + } + #endregion + + [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode", + Justification = "Reflection.Emit is not subject to trimming")] + private static MethodInfo ExtractBaseMethod(MethodInfo info) + { + if (info is MethodBuilder) + return info; + if (info is MethodOnTypeBuilderInstantiation mbi) + return mbi._method; + + if (info.IsGenericMethod) + info = info.GetGenericMethodDefinition(); + + Type t = info.DeclaringType!; + if (!t.IsGenericType || t.IsGenericTypeDefinition) + return info; + + return (MethodInfo)t.Module.ResolveMethod(info.MetadataToken)!; + } + + #region MemberInfo Overrides + public override MemberTypes MemberType => _method.MemberType; + public override string Name => _method.Name; + public override Type? DeclaringType => _type; + public override Type? ReflectedType => _type; + public override object[] GetCustomAttributes(bool inherit) { return _method.GetCustomAttributes(inherit); } + public override object[] GetCustomAttributes(Type attributeType, bool inherit) { return _method.GetCustomAttributes(attributeType, inherit); } + public override bool IsDefined(Type attributeType, bool inherit) { return _method.IsDefined(attributeType, inherit); } + public override Module Module => _method.Module; + #endregion + + #region MethodBase Members + public override ParameterInfo[] GetParameters() { return _method.GetParameters(); } + public override MethodImplAttributes GetMethodImplementationFlags() { return _method.GetMethodImplementationFlags(); } + public override RuntimeMethodHandle MethodHandle => _method.MethodHandle; + public override MethodAttributes Attributes => _method.Attributes; + public override object Invoke(object? obj, BindingFlags invokeAttr, Binder? binder, object?[]? parameters, CultureInfo? culture) + { + throw new NotSupportedException(); + } + public override CallingConventions CallingConvention => _method.CallingConvention; +#if !MONO + public override MethodInfo GetGenericMethodDefinition() { return _method; } + public override bool IsGenericMethodDefinition => _method.IsGenericMethodDefinition; + public override Type[] GetGenericArguments() + { + return _method.GetGenericArguments(); + } + public override bool ContainsGenericParameters + { + get + { + if (_method.ContainsGenericParameters) + return true; + if (!_method.IsGenericMethodDefinition) + throw new NotSupportedException(); + + return _method.ContainsGenericParameters; + } + } + [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[] typeArgs) + { + if (!IsGenericMethodDefinition) + { + throw new InvalidOperationException(SR.Format(SR.Arg_NotGenericMethodDefinition, this)); + } + + return MethodBuilderInstantiation.MakeGenericMethod(this, typeArgs); + } +#endif + public override bool IsGenericMethod => _method.IsGenericMethod; + #endregion + + #region Public Abstract\Virtual Members + public override Type ReturnType => _method.ReturnType; + public override ParameterInfo ReturnParameter => throw new NotSupportedException(); + public override ICustomAttributeProvider ReturnTypeCustomAttributes => throw new NotSupportedException(); + public override MethodInfo GetBaseDefinition() { throw new NotSupportedException(); } + #endregion + + #region Internal overrides + internal override Type[] GetParameterTypes() + { + return _method.GetParameterTypes(); + } + #endregion + } +} diff --git a/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/SymbolType.cs b/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/SymbolType.cs similarity index 85% rename from src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/SymbolType.cs rename to src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/SymbolType.cs index 5dfec4c2e73dc8..89834220d47a26 100644 --- a/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/SymbolType.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/SymbolType.cs @@ -1,7 +1,9 @@ -// Licensed to the .NET Foundation under one or more agreements. +// 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; using System.Diagnostics.CodeAnalysis; +using System.Runtime.InteropServices; using CultureInfo = System.Globalization.CultureInfo; namespace System.Reflection.Emit @@ -14,8 +16,23 @@ internal enum TypeKind } // This is a kind of Type object that will represent the compound expression of a parameter type or field type. - internal sealed class SymbolType : TypeInfo + internal sealed partial class SymbolType : TypeInfo { + #region Data Members + #region Fields need to be kept in order + // For Mono runtime its important to keep this declaration order in sync with MonoReflectionSymbolType struct in object-internals.h + internal Type _baseType = null!; + internal TypeKind _typeKind; + internal int _rank; // count of dimension + #endregion + // If LowerBound and UpperBound is equal, that means one element. + // If UpperBound is less than LowerBound, then the size is not specified. + internal int[] _iaLowerBound; + internal int[] _iaUpperBound; // count of dimension + private string? _format; // format string to form the full name. + private bool _isSzArray = true; + #endregion + public override bool IsAssignableFrom([NotNullWhen(true)] TypeInfo? typeInfo) { if (typeInfo == null) return false; @@ -46,14 +63,11 @@ public override bool IsAssignableFrom([NotNullWhen(true)] TypeInfo? typeInfo) return baseType; } - - - if (format[curIndex] == '&') { // ByRef case - symbolType = new SymbolType(TypeKind.IsByRef); + symbolType = new SymbolType(baseType, TypeKind.IsByRef); symbolType.SetFormat(format, curIndex, 1); curIndex++; @@ -61,14 +75,13 @@ public override bool IsAssignableFrom([NotNullWhen(true)] TypeInfo? typeInfo) // ByRef has to be the last char!! throw new ArgumentException(SR.Argument_BadSigFormat); - symbolType.SetElementType(baseType); return symbolType; } if (format[curIndex] == '[') { // Array type. - symbolType = new SymbolType(TypeKind.IsArray); + symbolType = new SymbolType(baseType, TypeKind.IsArray); int startIndex = curIndex; curIndex++; @@ -83,7 +96,7 @@ public override bool IsAssignableFrom([NotNullWhen(true)] TypeInfo? typeInfo) { if (format[curIndex] == '*') { - symbolType.m_isSzArray = false; + symbolType._isSzArray = false; curIndex++; } // consume, one dimension at a time @@ -181,18 +194,15 @@ public override bool IsAssignableFrom([NotNullWhen(true)] TypeInfo? typeInfo) symbolType.SetFormat(format, startIndex, curIndex - startIndex); - // set the base type of array - symbolType.SetElementType(baseType); return FormCompoundType(format, symbolType, curIndex); } else if (format[curIndex] == '*') { // pointer type. - symbolType = new SymbolType(TypeKind.IsPointer); + symbolType = new SymbolType(baseType, TypeKind.IsPointer); symbolType.SetFormat(format, curIndex, 1); curIndex++; - symbolType.SetElementType(baseType); return FormCompoundType(format, symbolType, curIndex); } @@ -201,63 +211,47 @@ public override bool IsAssignableFrom([NotNullWhen(true)] TypeInfo? typeInfo) #endregion - #region Data Members - internal TypeKind m_typeKind; - internal Type m_baseType = null!; - internal int m_cRank; // count of dimension - // If LowerBound and UpperBound is equal, that means one element. - // If UpperBound is less than LowerBound, then the size is not specified. - internal int[] m_iaLowerBound; - internal int[] m_iaUpperBound; // count of dimension - private string? m_format; // format string to form the full name. - private bool m_isSzArray = true; - #endregion - #region Constructor - internal SymbolType(TypeKind typeKind) + internal SymbolType(Type baseType, TypeKind typeKind) { - m_typeKind = typeKind; - m_iaLowerBound = new int[4]; - m_iaUpperBound = new int[4]; + ArgumentNullException.ThrowIfNull(baseType); + + _baseType = baseType; + _typeKind = typeKind; + _iaLowerBound = new int[4]; + _iaUpperBound = new int[4]; } #endregion #region Internal Members - internal void SetElementType(Type baseType) - { - ArgumentNullException.ThrowIfNull(baseType); - - m_baseType = baseType; - } - private void SetBounds(int lower, int upper) { // Increase the rank, set lower and upper bound if (lower != 0 || upper != -1) - m_isSzArray = false; + _isSzArray = false; - if (m_iaLowerBound.Length <= m_cRank) + if (_iaLowerBound.Length <= _rank) { // resize the bound array - int[] iaTemp = new int[m_cRank * 2]; - Array.Copy(m_iaLowerBound, iaTemp, m_cRank); - m_iaLowerBound = iaTemp; - Array.Copy(m_iaUpperBound, iaTemp, m_cRank); - m_iaUpperBound = iaTemp; + int[] iaTemp = new int[_rank * 2]; + Array.Copy(_iaLowerBound, iaTemp, _rank); + _iaLowerBound = iaTemp; + Array.Copy(_iaUpperBound, iaTemp, _rank); + _iaUpperBound = iaTemp; } - m_iaLowerBound[m_cRank] = lower; - m_iaUpperBound[m_cRank] = upper; - m_cRank++; + _iaLowerBound[_rank] = lower; + _iaUpperBound[_rank] = upper; + _rank++; } internal void SetFormat(string format, int curIndex, int length) { // Cache the text display format for this SymbolType - m_format = format.Substring(curIndex, length); + _format = format.Substring(curIndex, length); } #endregion @@ -265,27 +259,27 @@ internal void SetFormat(string format, int curIndex, int length) public override bool IsTypeDefinition => false; - public override bool IsSZArray => m_cRank <= 1 && m_isSzArray; + public override bool IsSZArray => _rank <= 1 && _isSzArray; public override Type MakePointerType() { - return SymbolType.FormCompoundType(m_format + "*", m_baseType, 0)!; + return FormCompoundType(_format + "*", _baseType, 0)!; } public override Type MakeByRefType() { - return SymbolType.FormCompoundType(m_format + "&", m_baseType, 0)!; + return FormCompoundType(_format + "&", _baseType, 0)!; } public override Type MakeArrayType() { - return SymbolType.FormCompoundType(m_format + "[]", m_baseType, 0)!; + return FormCompoundType(_format + "[]", _baseType, 0)!; } public override Type MakeArrayType(int rank) { string s = GetRankString(rank); - SymbolType? st = SymbolType.FormCompoundType(m_format + s, m_baseType, 0) as SymbolType; + SymbolType? st = FormCompoundType(_format + s, _baseType, 0) as SymbolType; return st!; } @@ -294,7 +288,7 @@ public override int GetArrayRank() if (!IsArray) throw new NotSupportedException(SR.NotSupported_SubclassOverride); - return m_cRank; + return _rank; } public override Guid GUID => throw new NotSupportedException(SR.NotSupported_NonReflectedType); @@ -312,7 +306,7 @@ public override Module Module { Type baseType; - for (baseType = m_baseType; baseType is SymbolType; baseType = ((SymbolType)baseType).m_baseType) ; + for (baseType = _baseType; baseType is SymbolType; baseType = ((SymbolType)baseType)._baseType) ; return baseType.Module; } @@ -323,7 +317,7 @@ public override Assembly Assembly { Type baseType; - for (baseType = m_baseType; baseType is SymbolType; baseType = ((SymbolType)baseType).m_baseType) ; + for (baseType = _baseType; baseType is SymbolType; baseType = ((SymbolType)baseType)._baseType) ; return baseType.Assembly; } @@ -336,10 +330,10 @@ public override string Name get { Type baseType; - string? sFormat = m_format; + string? sFormat = _format; - for (baseType = m_baseType; baseType is SymbolType; baseType = ((SymbolType)baseType).m_baseType) - sFormat = ((SymbolType)baseType).m_format + sFormat; + for (baseType = _baseType; baseType is SymbolType; baseType = ((SymbolType)baseType)._baseType) + sFormat = ((SymbolType)baseType)._format + sFormat; return baseType.Name + sFormat; } @@ -354,7 +348,7 @@ public override string ToString() return TypeNameBuilder.ToString(this, TypeNameBuilder.Format.ToString)!; } - public override string? Namespace => m_baseType.Namespace; + public override string? Namespace => _baseType.Namespace; public override Type BaseType => typeof(System.Array); @@ -473,23 +467,23 @@ protected override TypeAttributes GetAttributeFlagsImpl() { // Return the attribute flags of the base type? Type baseType; - for (baseType = m_baseType; baseType is SymbolType; baseType = ((SymbolType)baseType).m_baseType) ; + for (baseType = _baseType; baseType is SymbolType; baseType = ((SymbolType)baseType)._baseType) ; return baseType.Attributes; } protected override bool IsArrayImpl() { - return m_typeKind == TypeKind.IsArray; + return _typeKind == TypeKind.IsArray; } protected override bool IsPointerImpl() { - return m_typeKind == TypeKind.IsPointer; + return _typeKind == TypeKind.IsPointer; } protected override bool IsByRefImpl() { - return m_typeKind == TypeKind.IsByRef; + return _typeKind == TypeKind.IsByRef; } protected override bool IsPrimitiveImpl() @@ -511,12 +505,12 @@ protected override bool IsCOMObjectImpl() public override Type? GetElementType() { - return m_baseType; + return _baseType; } protected override bool HasElementTypeImpl() { - return m_baseType != null; + return _baseType != null; } public override Type UnderlyingSystemType => this; diff --git a/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilderInstantiation.cs b/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilderInstantiation.cs similarity index 87% rename from src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilderInstantiation.cs rename to src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilderInstantiation.cs index a12e143286e05a..f2af3be9f65fb5 100644 --- a/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilderInstantiation.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilderInstantiation.cs @@ -1,15 +1,27 @@ -// Licensed to the .NET Foundation under one or more agreements. +// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. using System.Collections; -using System.Globalization; using System.Diagnostics; using System.Diagnostics.CodeAnalysis; +using System.Globalization; namespace System.Reflection.Emit { - internal sealed class TypeBuilderInstantiation : TypeInfo + /* + * TypeBuilderInstantiation represents an instantiation of a generic TypeBuilder. + */ + internal sealed partial class TypeBuilderInstantiation : TypeInfo { + #region Fields need to be kept in order + // For Mono runtime its important to keep this declaration order in sync with MonoReflectionGenericClass struct in object-internals.h + private Type _genericType; + private Type[] _typeArguments; + #endregion + private string? _strFullQualName; + internal Hashtable _hashtable; + + public override bool IsAssignableFrom([NotNullWhen(true)] TypeInfo? typeInfo) { if (typeInfo == null) return false; @@ -33,23 +45,14 @@ internal static Type MakeGenericType(Type type, Type[] typeArguments) return new TypeBuilderInstantiation(type, typeArguments); } - - #endregion - - #region Private Data Members - private Type m_type; - private Type[] m_inst; - private string? m_strFullQualName; - internal Hashtable m_hashtable; - #endregion #region Constructor - private TypeBuilderInstantiation(Type type, Type[] inst) + internal TypeBuilderInstantiation(Type type, Type[] inst) { - m_type = type; - m_inst = inst; - m_hashtable = new Hashtable(); + _genericType = type; + _typeArguments = inst; + _hashtable = new Hashtable(); } #endregion @@ -61,13 +64,13 @@ public override string ToString() #endregion #region MemberInfo Overrides - public override Type? DeclaringType => m_type.DeclaringType; + public override Type? DeclaringType => _genericType.DeclaringType; - public override Type? ReflectedType => m_type.ReflectedType; + public override Type? ReflectedType => _genericType.ReflectedType; - public override string Name => m_type.Name; + public override string Name => _genericType.Name; - public override Module Module => m_type.Module; + public override Module Module => _genericType.Module; #endregion #region Type Overrides @@ -99,10 +102,10 @@ public override Type MakeArrayType(int rank) [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] public override object InvokeMember(string name, BindingFlags invokeAttr, Binder? binder, object? target, object?[]? args, ParameterModifier[]? modifiers, CultureInfo? culture, string[]? namedParameters) { throw new NotSupportedException(); } - public override Assembly Assembly => m_type.Assembly; + public override Assembly Assembly => _genericType.Assembly; public override RuntimeTypeHandle TypeHandle => throw new NotSupportedException(); - public override string? FullName => m_strFullQualName ??= TypeNameBuilder.ToString(this, TypeNameBuilder.Format.FullName); - public override string? Namespace => m_type.Namespace; + public override string? FullName => _strFullQualName ??= TypeNameBuilder.ToString(this, TypeNameBuilder.Format.FullName); + public override string? Namespace => _genericType.Namespace; public override string? AssemblyQualifiedName => TypeNameBuilder.ToString(this, TypeNameBuilder.Format.AssemblyQualifiedName); [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2055:UnrecognizedReflectionPattern", Justification = "The entire TypeBuilderInstantiation is serving the MakeGenericType implementation. " + @@ -145,7 +148,7 @@ public override Type? BaseType // D : B,char> get { - Type? typeBldrBase = m_type.BaseType; + Type? typeBldrBase = _genericType.BaseType; if (typeBldrBase == null) return null; @@ -212,7 +215,7 @@ public override Type? BaseType [DynamicallyAccessedMembers(GetAllMembers)] public override MemberInfo[] GetMembers(BindingFlags bindingAttr) { throw new NotSupportedException(); } - protected override TypeAttributes GetAttributeFlagsImpl() { return m_type.Attributes; } + protected override TypeAttributes GetAttributeFlagsImpl() { return _genericType.Attributes; } public override bool IsTypeDefinition => false; public override bool IsSZArray => false; @@ -225,20 +228,20 @@ public override Type? BaseType public override Type GetElementType() { throw new NotSupportedException(); } protected override bool HasElementTypeImpl() { return false; } public override Type UnderlyingSystemType => this; - public override Type[] GetGenericArguments() { return m_inst; } + public override Type[] GetGenericArguments() { return _typeArguments; } public override bool IsGenericTypeDefinition => false; public override bool IsGenericType => true; public override bool IsConstructedGenericType => true; public override bool IsGenericParameter => false; public override int GenericParameterPosition => throw new InvalidOperationException(); - protected override bool IsValueTypeImpl() { return m_type.IsValueType; } + protected override bool IsValueTypeImpl() { return _genericType.IsValueType; } public override bool ContainsGenericParameters { get { - for (int i = 0; i < m_inst.Length; i++) + for (int i = 0; i < _typeArguments.Length; i++) { - if (m_inst[i].ContainsGenericParameters) + if (_typeArguments[i].ContainsGenericParameters) return true; } @@ -246,7 +249,7 @@ public override bool ContainsGenericParameters } } public override MethodBase? DeclaringMethod => null; - public override Type GetGenericTypeDefinition() { return m_type; } + public override Type GetGenericTypeDefinition() { return _genericType; } [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[] inst) { throw new InvalidOperationException(SR.Format(SR.Arg_NotGenericTypeDefinition, this)); } diff --git a/src/libraries/System.Private.CoreLib/src/System/Reflection/MethodBase.cs b/src/libraries/System.Private.CoreLib/src/System/Reflection/MethodBase.cs index 2ab4287267048d..448dd5bfbb7ac5 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Reflection/MethodBase.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Reflection/MethodBase.cs @@ -121,6 +121,21 @@ internal static void AppendParameters(ref ValueStringBuilder sbParamList, Type[] } } + internal virtual Type[] GetParameterTypes() + { + ParameterInfo[] paramInfo = GetParametersNoCopy(); + if (paramInfo.Length == 0) + { + return Type.EmptyTypes; + } + + Type[] parameterTypes = new Type[paramInfo.Length]; + for (int i = 0; i < paramInfo.Length; i++) + parameterTypes[i] = paramInfo[i].ParameterType; + + return parameterTypes; + } + #if !NATIVEAOT private protected void ValidateInvokeTarget(object? target) { diff --git a/src/libraries/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderGetILGenerator.cs b/src/libraries/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderGetILGenerator.cs index 554b2e36be018c..2ac6b45825a19b 100644 --- a/src/libraries/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderGetILGenerator.cs +++ b/src/libraries/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderGetILGenerator.cs @@ -1,6 +1,7 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. +using Microsoft.Diagnostics.Runtime.Interop; using Xunit; namespace System.Reflection.Emit.Tests @@ -81,5 +82,63 @@ public void GetILGenerator_DifferentAttributes(MethodAttributes attributes) MethodBuilder method = type.DefineMethod(attributes.ToString(), attributes); Assert.NotNull(method.GetILGenerator()); } + + [Fact] + public void LoadPointerTypeInILGeneratedMethod() + { + TypeBuilder type = Helpers.DynamicType(TypeAttributes.Public); + Type pointerType = type.MakePointerType(); + + MethodBuilder method = type.DefineMethod("TestMethod", MethodAttributes.Public | MethodAttributes.Static, typeof(string), Type.EmptyTypes); + ILGenerator ilGenerator = method.GetILGenerator(); + + ilGenerator.Emit(OpCodes.Ldtoken, pointerType); + ilGenerator.Emit(OpCodes.Call, typeof(Type).GetMethod("GetTypeFromHandle", BindingFlags.Static | BindingFlags.Public)); + ilGenerator.Emit(OpCodes.Callvirt, typeof(Type).GetMethod("get_FullName")); + ilGenerator.Emit(OpCodes.Ret); + + Type createdType = type.CreateType(); + MethodInfo createdMethod = createdType.GetMethod("TestMethod"); + Assert.Equal("TestType*", createdMethod.Invoke(null, null)); + } + + [Fact] + public void LoadArrayTypeInILGeneratedMethod() + { + TypeBuilder type = Helpers.DynamicType(TypeAttributes.Public); + Type arrayType = type.MakeArrayType(); + + MethodBuilder method = type.DefineMethod("TestMethod", MethodAttributes.Public | MethodAttributes.Static, typeof(string), Type.EmptyTypes); + ILGenerator ilGenerator = method.GetILGenerator(); + + ilGenerator.Emit(OpCodes.Ldtoken, arrayType); + ilGenerator.Emit(OpCodes.Call, typeof(Type).GetMethod("GetTypeFromHandle", BindingFlags.Static | BindingFlags.Public)); + ilGenerator.Emit(OpCodes.Callvirt, typeof(Type).GetMethod("get_FullName")); + ilGenerator.Emit(OpCodes.Ret); + + Type createdType = type.CreateType(); + MethodInfo createdMethod = createdType.GetMethod("TestMethod"); + Assert.Equal("TestType[]", createdMethod.Invoke(null, null)); + } + + [Fact] + [ActiveIssue("https://github.com/dotnet/runtime/issues/82257", TestRuntimes.Mono)] + public void LoadByRefTypeInILGeneratedMethod() + { + TypeBuilder type = Helpers.DynamicType(TypeAttributes.Public); + Type byrefType = type.MakeByRefType(); + + MethodBuilder method = type.DefineMethod("TestMethod", MethodAttributes.Public | MethodAttributes.Static, typeof(string), Type.EmptyTypes); + ILGenerator ilGenerator = method.GetILGenerator(); + + ilGenerator.Emit(OpCodes.Ldtoken, byrefType); + ilGenerator.Emit(OpCodes.Call, typeof(Type).GetMethod("GetTypeFromHandle", BindingFlags.Static | BindingFlags.Public)); + ilGenerator.Emit(OpCodes.Callvirt, typeof(Type).GetMethod("get_FullName")); + ilGenerator.Emit(OpCodes.Ret); + + Type createdType = type.CreateType(); + MethodInfo createdMethod = createdType.GetMethod("TestMethod"); + Assert.Equal("TestType&", createdMethod.Invoke(null, null)); + } } } diff --git a/src/mono/System.Private.CoreLib/System.Private.CoreLib.csproj b/src/mono/System.Private.CoreLib/System.Private.CoreLib.csproj index c5b0ad5e9363fa..22567dc0f2e0fe 100644 --- a/src/mono/System.Private.CoreLib/System.Private.CoreLib.csproj +++ b/src/mono/System.Private.CoreLib/System.Private.CoreLib.csproj @@ -219,19 +219,16 @@ - + - - - + - + - @@ -243,7 +240,8 @@ - + + @@ -313,8 +311,8 @@ + DestinationFolder="$(RuntimeBinDir)" + SkipUnchangedFiles="true" /> 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 deleted file mode 100644 index ba4ffe2403d50d..00000000000000 --- a/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/ConstructorOnTypeBuilderInst.cs +++ /dev/null @@ -1,258 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -// -// System.Reflection.Emit/ConstructorOnTypeBuilderInst.cs -// -// Author: -// Zoltan Varga (vargaz@gmail.com) -// -// -// Copyright (C) 2008 Novell, Inc (http://www.novell.com) -// -// Permission is hereby granted, free of charge, to any person obtaining -// a copy of this software and associated documentation files (the -// "Software"), to deal in the Software without restriction, including -// without limitation the rights to use, copy, modify, merge, publish, -// distribute, sublicense, and/or sell copies of the Software, and to -// permit persons to whom the Software is furnished to do so, subject to -// the following conditions: -// -// The above copyright notice and this permission notice shall be -// included in all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND -// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE -// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION -// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION -// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -// - -#if MONO_FEATURE_SRE -using System.Diagnostics.CodeAnalysis; -using System.Globalization; -using System.Runtime.InteropServices; - -namespace System.Reflection.Emit -{ - /* - * This class represents a ctor of an instantiation of a generic type builder. - */ - internal sealed class ConstructorOnTypeBuilderInst : ConstructorInfo - { - internal TypeBuilderInstantiation instantiation; - internal ConstructorInfo cb; - - public ConstructorOnTypeBuilderInst(TypeBuilderInstantiation instantiation, ConstructorInfo cb) - { - this.instantiation = instantiation; - this.cb = cb; - } - - // - // MemberInfo members - // - - public override Type DeclaringType - { - get - { - return instantiation; - } - } - - public override string Name - { - get - { - return cb.Name; - } - } - - public override Type ReflectedType - { - get - { - return instantiation; - } - } - - public override Module Module - { - get - { - return cb.Module; - } - } - - public override bool IsDefined(Type attributeType, bool inherit) - { - return cb.IsDefined(attributeType, inherit); - } - - public override object[] GetCustomAttributes(bool inherit) - { - return cb.GetCustomAttributes(inherit); - } - - public override object[] GetCustomAttributes(Type attributeType, bool inherit) - { - return cb.GetCustomAttributes(attributeType, inherit); - } - - // - // MethodBase members - // - - public override MethodImplAttributes GetMethodImplementationFlags() - { - return cb.GetMethodImplementationFlags(); - } - - public override ParameterInfo[] GetParameters() - { - /*FIXME, maybe the right thing to do when the type is creates is to retrieve from the inflated type*/ - if (!instantiation.IsCreated) - throw new NotSupportedException(); - - return GetParametersInternal(); - } - - internal override ParameterInfo[] GetParametersInternal() - { - ParameterInfo[] res; - if (cb is RuntimeConstructorBuilder cbuilder) - { - res = new ParameterInfo[cbuilder.parameters!.Length]; - for (int i = 0; i < cbuilder.parameters.Length; i++) - { - Type? type = instantiation.InflateType(cbuilder.parameters[i]); - res[i] = RuntimeParameterInfo.New(cbuilder.pinfo?[i], type, this, i + 1); - } - } - else - { - ParameterInfo[] parms = cb.GetParameters(); - res = new ParameterInfo[parms.Length]; - for (int i = 0; i < parms.Length; i++) - { - Type? type = instantiation.InflateType(parms[i].ParameterType); - res[i] = RuntimeParameterInfo.New(parms[i], type, this, i + 1); - } - } - return res; - } - - internal override Type[] GetParameterTypes() - { - if (cb is RuntimeConstructorBuilder builder) - { - return builder.parameters!; - } - else - { - ParameterInfo[] parms = cb.GetParameters(); - var res = new Type[parms.Length]; - for (int i = 0; i < parms.Length; i++) - { - res[i] = parms[i].ParameterType; - } - return res; - } - } - - // Called from the runtime to return the corresponding finished ConstructorInfo object - [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2075:UnrecognizedReflectionPattern", - Justification = "Reflection.Emit is not subject to trimming")] - internal ConstructorInfo RuntimeResolve() - { - Type type = instantiation.InternalResolve(); - return type.GetConstructor(cb); - } - - public override int MetadataToken - { - get - { - return base.MetadataToken; - } - } - - internal override int GetParametersCount() - { - return cb.GetParametersCount(); - } - - public override object? Invoke(object? obj, BindingFlags invokeAttr, Binder? binder, object?[]? parameters, CultureInfo? culture) - { - return cb.Invoke(obj, invokeAttr, binder, parameters, culture); - } - - public override RuntimeMethodHandle MethodHandle - { - get - { - return cb.MethodHandle; - } - } - - public override MethodAttributes Attributes - { - get - { - return cb.Attributes; - } - } - - public override CallingConventions CallingConvention - { - get - { - return cb.CallingConvention; - } - } - - public override Type[] GetGenericArguments() - { - return cb.GetGenericArguments(); - } - - public override bool ContainsGenericParameters - { - get - { - return false; - } - } - - public override bool IsGenericMethodDefinition - { - get - { - return false; - } - } - - public override bool IsGenericMethod - { - get - { - return false; - } - } - - // - // MethodBase members - // - - public override object Invoke(BindingFlags invokeAttr, Binder? binder, object?[]? parameters, - CultureInfo? culture) - { - throw new InvalidOperationException(); - } - } -} - -#endif diff --git a/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/ConstructorOnTypeBuilderInstantiation.Mono.cs b/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/ConstructorOnTypeBuilderInstantiation.Mono.cs new file mode 100644 index 00000000000000..948abec87d88ef --- /dev/null +++ b/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/ConstructorOnTypeBuilderInstantiation.Mono.cs @@ -0,0 +1,56 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +// +// System.Reflection.Emit/ConstructorOnTypeBuilderInstantiation.cs +// +// Author: +// Zoltan Varga (vargaz@gmail.com) +// +// +// Copyright (C) 2008 Novell, Inc (http://www.novell.com) +// +// Permission is hereby granted, free of charge, to any person obtaining +// a copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to +// permit persons to whom the Software is furnished to do so, subject to +// the following conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +// + +using System.Diagnostics.CodeAnalysis; + +namespace System.Reflection.Emit +{ + /* + * This class represents a ctor of an instantiation of a generic type builder. + */ + internal partial class ConstructorOnTypeBuilderInstantiation + { + // Called from the runtime to return the corresponding finished ConstructorInfo object + [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2075:UnrecognizedReflectionPattern", + Justification = "Reflection.Emit is not subject to trimming")] + internal ConstructorInfo RuntimeResolve() + { + Type type = _type.InternalResolve(); + return type.GetConstructor(_ctor); + } + + internal override int GetParametersCount() + { + return _ctor.GetParametersCount(); + } + } +} diff --git a/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/DerivedTypes.Mono.cs b/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/DerivedTypes.Mono.cs deleted file mode 100644 index c6fbbb10a2fa7e..00000000000000 --- a/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/DerivedTypes.Mono.cs +++ /dev/null @@ -1,502 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -// -// System.Reflection.Emit.DerivedTypes.cs -// -// Authors: -// Rodrigo Kumpera -// -// -// Copyright (C) 2009 Novell, Inc (http://www.novell.com) -// -// Permission is hereby granted, free of charge, to any person obtaining -// a copy of this software and associated documentation files (the -// "Software"), to deal in the Software without restriction, including -// without limitation the rights to use, copy, modify, merge, publish, -// distribute, sublicense, and/or sell copies of the Software, and to -// permit persons to whom the Software is furnished to do so, subject to -// the following conditions: -// -// The above copyright notice and this permission notice shall be -// included in all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND -// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE -// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION -// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION -// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -// - -#if MONO_FEATURE_SRE -using System.Diagnostics.CodeAnalysis; -using System.Globalization; -using System.Runtime.InteropServices; -using System.Text; - -namespace System.Reflection.Emit -{ - [StructLayout(LayoutKind.Sequential)] - internal abstract partial class SymbolType : TypeInfo - { -#region Sync with MonoReflectionDerivedType in object-internals.h - private protected Type m_baseType; -#endregion - - [DynamicDependency(nameof(m_baseType))] // Automatically keeps all previous fields too due to StructLayout - internal SymbolType(Type elementType) - { - this.m_baseType = elementType; - } - - [return: NotNullIfNotNull(nameof(elementName))] - internal abstract string? FormatName(string? elementName); - - protected override bool IsArrayImpl() - { - return false; - } - - protected override bool IsByRefImpl() - { - return false; - } - - protected override bool IsPointerImpl() - { - return false; - } - - public override Type MakeArrayType() - { - return new ArrayType(this, 0); - } - - public override Type MakeArrayType(int rank) - { - if (rank < 1) - throw new IndexOutOfRangeException(); - return new ArrayType(this, rank); - } - - public override Type MakeByRefType() - { - return new ByRefType(this); - } - - public override Type MakePointerType() - { - return new PointerType(this); - } - - public override string ToString() - { - return FormatName(m_baseType.ToString()); - } - - public override string? AssemblyQualifiedName - { - get - { - string? fullName = FormatName(m_baseType.FullName); - if (fullName == null) - return null; - return fullName + ", " + m_baseType.Assembly.FullName; - } - } - - - public override string? FullName - { - get - { - return FormatName(m_baseType.FullName); - } - } - - public override string Name - { - get - { - return FormatName(m_baseType.Name); - } - } - - public override Type UnderlyingSystemType - { - get - { - return this; - } - } - - internal override bool IsUserType - { - get - { - return m_baseType.IsUserType; - } - } - - // Called from the runtime to return the corresponding finished Type object - internal override Type RuntimeResolve() - { - return InternalResolve(); - } - - public override Guid GUID => throw new NotSupportedException(SR.NotSupported_NonReflectedType); - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] - public override object? InvokeMember(string name, BindingFlags invokeAttr, Binder? binder, object? target, - object?[]? args, ParameterModifier[]? modifiers, CultureInfo? culture, string[]? namedParameters) - { - throw new NotSupportedException(SR.NotSupported_NonReflectedType); - } - - public override Module Module - { - get - { - Type baseType; - - for (baseType = m_baseType; baseType is SymbolType; baseType = ((SymbolType)baseType).m_baseType) ; - - return baseType.Module; - } - } - public override Assembly Assembly - { - get - { - Type baseType; - - for (baseType = m_baseType; baseType is SymbolType; baseType = ((SymbolType)baseType).m_baseType) ; - - return baseType.Assembly; - } - } - - public override RuntimeTypeHandle TypeHandle => throw new NotSupportedException(SR.NotSupported_NonReflectedType); - - public override string? Namespace - { - get { return m_baseType.Namespace; } - } - - public override Type BaseType - { - get { return typeof(System.Array); } - } - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)] - protected override ConstructorInfo? GetConstructorImpl(BindingFlags bindingAttr, Binder? binder, - CallingConventions callConvention, Type[]? types, ParameterModifier[]? modifiers) - { - throw new NotSupportedException(SR.NotSupported_NonReflectedType); - } - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)] - public override ConstructorInfo[] GetConstructors(BindingFlags bindingAttr) - { - throw new NotSupportedException(SR.NotSupported_NonReflectedType); - } - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.NonPublicMethods)] - protected override MethodInfo? GetMethodImpl(string name, BindingFlags bindingAttr, Binder? binder, - CallingConventions callConvention, Type[]? types, ParameterModifier[]? modifiers) - { - throw new NotSupportedException(SR.NotSupported_NonReflectedType); - } - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.NonPublicMethods)] - public override MethodInfo[] GetMethods(BindingFlags bindingAttr) - { - throw new NotSupportedException(SR.NotSupported_NonReflectedType); - } - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)] - public override FieldInfo GetField(string name, BindingFlags bindingAttr) - { - throw new NotSupportedException(SR.NotSupported_NonReflectedType); - } - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)] - public override FieldInfo[] GetFields(BindingFlags bindingAttr) - { - throw new NotSupportedException(SR.NotSupported_NonReflectedType); - } - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.Interfaces)] - [return: DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.Interfaces)] - public override Type GetInterface(string name, bool ignoreCase) - { - throw new NotSupportedException(SR.NotSupported_NonReflectedType); - } - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.Interfaces)] - public override Type[] GetInterfaces() - { - throw new NotSupportedException(SR.NotSupported_NonReflectedType); - } - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicEvents | DynamicallyAccessedMemberTypes.NonPublicEvents)] - public override EventInfo GetEvent(string name, BindingFlags bindingAttr) - { - throw new NotSupportedException(SR.NotSupported_NonReflectedType); - } - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicEvents)] - public override EventInfo[] GetEvents() - { - throw new NotSupportedException(SR.NotSupported_NonReflectedType); - } - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypes.NonPublicProperties)] - protected override PropertyInfo? GetPropertyImpl(string name, BindingFlags bindingAttr, Binder? binder, - Type? returnType, Type[]? types, ParameterModifier[]? modifiers) - { - throw new NotSupportedException(SR.NotSupported_NonReflectedType); - } - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypes.NonPublicProperties)] - public override PropertyInfo[] GetProperties(BindingFlags bindingAttr) - { - throw new NotSupportedException(SR.NotSupported_NonReflectedType); - } - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicNestedTypes | DynamicallyAccessedMemberTypes.NonPublicNestedTypes)] - public override Type[] GetNestedTypes(BindingFlags bindingAttr) - { - throw new NotSupportedException(SR.NotSupported_NonReflectedType); - } - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicNestedTypes | DynamicallyAccessedMemberTypes.NonPublicNestedTypes)] - public override Type GetNestedType(string name, BindingFlags bindingAttr) - { - throw new NotSupportedException(SR.NotSupported_NonReflectedType); - } - - [DynamicallyAccessedMembers(GetAllMembers)] - public override MemberInfo[] GetMember(string name, MemberTypes type, BindingFlags bindingAttr) - { - throw new NotSupportedException(SR.NotSupported_NonReflectedType); - } - - [DynamicallyAccessedMembers(GetAllMembers)] - public override MemberInfo[] GetMembers(BindingFlags bindingAttr) - { - throw new NotSupportedException(SR.NotSupported_NonReflectedType); - } - - public override InterfaceMapping GetInterfaceMap([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.NonPublicMethods)] Type interfaceType) - { - throw new NotSupportedException(SR.NotSupported_NonReflectedType); - } - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicEvents | DynamicallyAccessedMemberTypes.NonPublicEvents)] - public override EventInfo[] GetEvents(BindingFlags bindingAttr) - { - throw new NotSupportedException(SR.NotSupported_NonReflectedType); - } - - protected override TypeAttributes GetAttributeFlagsImpl() - { - // Return the attribute flags of the base type? - Type baseType; - for (baseType = m_baseType; baseType is SymbolType; baseType = ((SymbolType)baseType).m_baseType) ; - return baseType.Attributes; - } - - protected override bool IsPrimitiveImpl() - { - return false; - } - - protected override bool IsValueTypeImpl() - { - return false; - } - - protected override bool IsCOMObjectImpl() - { - return false; - } - - public override bool IsConstructedGenericType - { - get - { - return false; - } - } - - public override Type GetElementType() - { - return m_baseType; - } - - protected override bool HasElementTypeImpl() - { - return m_baseType != null; - } - - public override object[] GetCustomAttributes(bool inherit) - { - throw new NotSupportedException(SR.NotSupported_NonReflectedType); - } - - public override object[] GetCustomAttributes(Type attributeType, bool inherit) - { - throw new NotSupportedException(SR.NotSupported_NonReflectedType); - } - - public override bool IsDefined(Type attributeType, bool inherit) - { - throw new NotSupportedException(SR.NotSupported_NonReflectedType); - } - } - - [StructLayout(LayoutKind.Sequential)] - internal sealed class ArrayType : SymbolType - { -#region Sync with MonoReflectionArrayType in object-internals.h - private int rank; -#endregion - - [DynamicDependency(nameof(rank))] // Automatically keeps all previous fields too due to StructLayout - internal ArrayType(Type elementType, int rank) : base(elementType) - { - this.rank = rank; - } - - internal int GetEffectiveRank() - { - return rank; - } - - internal override Type InternalResolve() - { - Type et = m_baseType.InternalResolve(); - if (rank == 0) - return et.MakeArrayType(); - return et.MakeArrayType(rank); - } - - internal override Type RuntimeResolve() - { - Type et = m_baseType.RuntimeResolve(); - if (rank == 0) - return et.MakeArrayType(); - return et.MakeArrayType(rank); - } - - protected override bool IsArrayImpl() - { - return true; - } - - public override bool IsSZArray - { - get - { - return rank == 0; - } - } - - public override int GetArrayRank() - { - return (rank == 0) ? 1 : rank; - } - - [return: NotNullIfNotNull(nameof(elementName))] - internal override string? FormatName(string? elementName) - { - if (elementName == null) - return null; - StringBuilder sb = new StringBuilder(elementName); - sb.Append('['); - for (int i = 1; i < rank; ++i) - sb.Append(','); - if (rank == 1) - sb.Append('*'); - sb.Append(']'); - return sb.ToString(); - } - } - - [StructLayout(LayoutKind.Sequential)] - internal sealed class ByRefType : SymbolType - { - internal ByRefType(Type elementType) : base(elementType) - { - } - - internal override Type InternalResolve() - { - return m_baseType.InternalResolve().MakeByRefType(); - } - - protected override bool IsByRefImpl() - { - return true; - } - - [return: NotNullIfNotNull(nameof(elementName))] - internal override string? FormatName(string? elementName) - { - if (elementName == null) - return null; - return elementName + "&"; - } - - public override Type MakeArrayType() - { - throw new ArgumentException(SR.NotSupported_ByRefLikeArray); - } - - public override Type MakeArrayType(int rank) - { - throw new ArgumentException(SR.NotSupported_ByRefLikeArray); - } - - public override Type MakeByRefType() - { - throw new ArgumentException("Cannot create a byref type of an already byref type"); - } - - public override Type MakePointerType() - { - throw new ArgumentException("Cannot create a pointer type of a byref type"); - } - } - - [StructLayout(LayoutKind.Sequential)] - internal sealed class PointerType : SymbolType - { - internal PointerType(Type elementType) : base(elementType) - { - } - - internal override Type InternalResolve() - { - return m_baseType.InternalResolve().MakePointerType(); - } - - protected override bool IsPointerImpl() - { - return true; - } - - [return: NotNullIfNotNull(nameof(elementName))] - internal override string? FormatName(string? elementName) - { - if (elementName == null) - return null; - return elementName + "*"; - } - } - -} -#endif 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 deleted file mode 100644 index 86daf33c633879..00000000000000 --- a/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/EventOnTypeBuilderInst.cs +++ /dev/null @@ -1,136 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -// -// System.Reflection.Emit/EventOnTypeBuilderInst.cs -// -// Author: -// Rodrigo Kumpera (rkumpera@novell.com) -// -// -// Copyright (C) 2009 Novell, Inc (http://www.novell.com) -// -// Permission is hereby granted, free of charge, to any person obtaining -// a copy of this software and associated documentation files (the -// "Software"), to deal in the Software without restriction, including -// without limitation the rights to use, copy, modify, merge, publish, -// distribute, sublicense, and/or sell copies of the Software, and to -// permit persons to whom the Software is furnished to do so, subject to -// the following conditions: -// -// The above copyright notice and this permission notice shall be -// included in all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND -// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE -// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION -// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION -// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -// - -#if MONO_FEATURE_SRE -using System.Collections.Generic; -using System.Runtime.InteropServices; - -namespace System.Reflection.Emit -{ - /* - * This class represents an event of an instantiation of a generic type builder. - */ - [StructLayout(LayoutKind.Sequential)] - internal sealed class EventOnTypeBuilderInst : EventInfo - { - private TypeBuilderInstantiation instantiation; - private RuntimeEventBuilder? event_builder; - private EventInfo? event_info; - - internal EventOnTypeBuilderInst(TypeBuilderInstantiation instantiation, RuntimeEventBuilder evt) - { - this.instantiation = instantiation; - this.event_builder = evt; - } - - internal EventOnTypeBuilderInst(TypeBuilderInstantiation instantiation, EventInfo evt) - { - this.instantiation = instantiation; - this.event_info = evt; - } - - public override EventAttributes Attributes - { - get { return event_builder != null ? event_builder.attrs : event_info!.Attributes; } - } - - public override MethodInfo? GetAddMethod(bool nonPublic) - { - MethodInfo? add = event_builder != null ? event_builder.add_method : event_info!.GetAddMethod(nonPublic); - if (add == null || (!nonPublic && !add.IsPublic)) - return null; - return TypeBuilder.GetMethod(instantiation, add); - } - - public override MethodInfo? GetRaiseMethod(bool nonPublic) - { - MethodInfo? raise = event_builder != null ? event_builder.raise_method : event_info!.GetRaiseMethod(nonPublic); - if (raise == null || (!nonPublic && !raise.IsPublic)) - return null; - return TypeBuilder.GetMethod(instantiation, raise); - } - - public override MethodInfo? GetRemoveMethod(bool nonPublic) - { - MethodInfo? remove = event_builder != null ? event_builder.remove_method : event_info!.GetRemoveMethod(nonPublic); - if (remove == null || (!nonPublic && !remove.IsPublic)) - return null; - return TypeBuilder.GetMethod(instantiation, remove); - } - - public override MethodInfo[] GetOtherMethods(bool nonPublic) - { - MethodInfo[]? other = event_builder != null ? event_builder.other_methods : event_info!.GetOtherMethods(nonPublic); - if (other == null) - return Array.Empty(); - - List res = new List(); - foreach (MethodInfo method in other) - { - if (nonPublic || method.IsPublic) - res.Add(TypeBuilder.GetMethod(instantiation, method)); - } - return res.ToArray(); - } - - public override Type DeclaringType - { - get { return instantiation; } - } - - public override string Name - { - get { return event_builder != null ? event_builder.name : event_info!.Name; } - } - - public override Type ReflectedType - { - get { return instantiation; } - } - - public override bool IsDefined(Type attributeType, bool inherit) - { - throw new NotSupportedException(); - } - - public override object[] GetCustomAttributes(bool inherit) - { - throw new NotSupportedException(); - } - - public override object[] GetCustomAttributes(Type attributeType, bool inherit) - { - throw new NotSupportedException(); - } - } -} -#endif diff --git a/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/FieldOnTypeBuilderInst.cs b/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/FieldOnTypeBuilderInst.cs deleted file mode 100644 index f5a2050bba8cc1..00000000000000 --- a/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/FieldOnTypeBuilderInst.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. - -// -// System.Reflection.Emit/FieldOnTypeBuilderInst.cs -// -// Author: -// Zoltan Varga (vargaz@gmail.com) -// -// -// Copyright (C) 2008 Novell, Inc (http://www.novell.com) -// -// Permission is hereby granted, free of charge, to any person obtaining -// a copy of this software and associated documentation files (the -// "Software"), to deal in the Software without restriction, including -// without limitation the rights to use, copy, modify, merge, publish, -// distribute, sublicense, and/or sell copies of the Software, and to -// permit persons to whom the Software is furnished to do so, subject to -// the following conditions: -// -// The above copyright notice and this permission notice shall be -// included in all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND -// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE -// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION -// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION -// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -// - -#if MONO_FEATURE_SRE -using System.Globalization; -using System.Runtime.InteropServices; - -namespace System.Reflection.Emit -{ - /* - * This class represents a field of an instantiation of a generic type builder. - */ - internal sealed class FieldOnTypeBuilderInst : FieldInfo - { - internal TypeBuilderInstantiation instantiation; - internal FieldInfo fb; - - public FieldOnTypeBuilderInst(TypeBuilderInstantiation instantiation, FieldInfo fb) - { - this.instantiation = instantiation; - this.fb = fb; - } - - // - // MemberInfo members - // - - public override Type DeclaringType - { - get - { - return instantiation; - } - } - - public override string Name - { - get - { - return fb.Name; - } - } - - public override Type ReflectedType - { - get - { - return instantiation; - } - } - - public override bool IsDefined(Type attributeType, bool inherit) - { - throw new NotSupportedException(); - } - - public override object[] GetCustomAttributes(bool inherit) - { - throw new NotSupportedException(); - } - - public override object[] GetCustomAttributes(Type attributeType, bool inherit) - { - throw new NotSupportedException(); - } - - public override string ToString() - { - return fb.FieldType.ToString() + " " + Name; - } - // - // FieldInfo members - // - - public override FieldAttributes Attributes - { - get - { - return fb.Attributes; - } - } - - public override RuntimeFieldHandle FieldHandle - { - get - { - throw new NotSupportedException(); - } - } - - public override int MetadataToken - { - get - { - throw new InvalidOperationException(); - } - } - - public override Type FieldType - { - get - { - throw new NotSupportedException(); - } - } - - public override object? GetValue(object? obj) - { - throw new NotSupportedException(); - } - - public override void SetValue(object? obj, object? value, BindingFlags invokeAttr, Binder? binder, CultureInfo? culture) - { - throw new NotSupportedException(); - } - - // Called from the runtime to return the corresponding finished FieldInfo object - internal FieldInfo RuntimeResolve() - { - Type type = instantiation.RuntimeResolve(); - return type.GetField(fb); - } - } -} -#endif diff --git a/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/FieldOnTypeBuilderInstantiation.Mono.cs b/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/FieldOnTypeBuilderInstantiation.Mono.cs new file mode 100644 index 00000000000000..c6f65c44abb263 --- /dev/null +++ b/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/FieldOnTypeBuilderInstantiation.Mono.cs @@ -0,0 +1,44 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +// +// System.Reflection.Emit/FieldOnTypeBuilderInstantiation.cs +// +// Author: +// Zoltan Varga (vargaz@gmail.com) +// +// +// Copyright (C) 2008 Novell, Inc (http://www.novell.com) +// +// Permission is hereby granted, free of charge, to any person obtaining +// a copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to +// permit persons to whom the Software is furnished to do so, subject to +// the following conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +// + +namespace System.Reflection.Emit +{ + internal partial class FieldOnTypeBuilderInstantiation + { + // Called from the runtime to return the corresponding finished FieldInfo object + internal FieldInfo RuntimeResolve() + { + Type type = _type.RuntimeResolve(); + return type.GetField(_field); + } + } +} diff --git a/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/MethodOnTypeBuilderInst.cs b/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/MethodOnTypeBuilderInst.cs deleted file mode 100644 index 91525190483f11..00000000000000 --- a/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/MethodOnTypeBuilderInst.cs +++ /dev/null @@ -1,346 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -// -// System.Reflection.Emit/MethodOnTypeBuilderInst.cs -// -// Author: -// Zoltan Varga (vargaz@gmail.com) -// -// -// Copyright (C) 2008 Novell, Inc (http://www.novell.com) -// -// Permission is hereby granted, free of charge, to any person obtaining -// a copy of this software and associated documentation files (the -// "Software"), to deal in the Software without restriction, including -// without limitation the rights to use, copy, modify, merge, publish, -// distribute, sublicense, and/or sell copies of the Software, and to -// permit persons to whom the Software is furnished to do so, subject to -// the following conditions: -// -// The above copyright notice and this permission notice shall be -// included in all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND -// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE -// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION -// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION -// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -// - -#if MONO_FEATURE_SRE -using System.Globalization; -using System.Text; -using System.Diagnostics.CodeAnalysis; -using System.Runtime.InteropServices; - -namespace System.Reflection.Emit -{ - /* - * This class represents a method of an instantiation of a generic type builder. - */ - internal sealed class MethodOnTypeBuilderInst : MethodInfo - { - private Type instantiation; - private MethodInfo base_method; /*This is the base method definition, it must be non-inflated and belong to a non-inflated type.*/ - private Type[]? method_arguments; - - private MethodInfo? generic_method_definition; - - internal MethodOnTypeBuilderInst(Type instantiation, MethodInfo base_method) - { - this.instantiation = instantiation; - this.base_method = base_method; - } - - internal MethodOnTypeBuilderInst(MethodOnTypeBuilderInst gmd, Type[] typeArguments) - : this(gmd.instantiation, gmd.base_method) - { - this.method_arguments = new Type[typeArguments.Length]; - typeArguments.CopyTo(this.method_arguments, 0); - this.generic_method_definition = gmd; - } - - internal MethodOnTypeBuilderInst(MethodInfo method, Type[] typeArguments) - : this(method.DeclaringType!, ExtractBaseMethod(method)) - { - this.method_arguments = new Type[typeArguments.Length]; - typeArguments.CopyTo(this.method_arguments, 0); - if (base_method != method) - this.generic_method_definition = method; - } - - [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode", - Justification = "Reflection.Emit is not subject to trimming")] - private static MethodInfo ExtractBaseMethod(MethodInfo info) - { - if (info is MethodBuilder) - return info; - if (info is MethodOnTypeBuilderInst) - return ((MethodOnTypeBuilderInst)info).base_method; - - if (info.IsGenericMethod) - info = info.GetGenericMethodDefinition(); - - Type t = info.DeclaringType!; - if (!t.IsGenericType || t.IsGenericTypeDefinition) - return info; - - return (MethodInfo)t.Module.ResolveMethod(info.MetadataToken)!; - } - - internal Type[]? GetTypeArgs() - { - if (!instantiation.IsGenericType || instantiation.IsGenericParameter) - return null; - - return instantiation.GetGenericArguments(); - } - - // Called from the runtime to return the corresponding finished MethodInfo object - [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2060:MakeGenericMethod", - Justification = "MethodOnTypeBuilderInst is Reflection.Emit's underlying implementation of MakeGenericMethod. " + - "Callers of the outer calls to MakeGenericMethod will be warned as appropriate.")] - internal MethodInfo RuntimeResolve() - { - Type type = instantiation.InternalResolve(); - MethodInfo m = type.GetMethod(base_method); - if (method_arguments != null) - { - var args = new Type[method_arguments.Length]; - for (int i = 0; i < method_arguments.Length; ++i) - args[i] = method_arguments[i].InternalResolve(); - m = m.MakeGenericMethod(args); - } - return m; - } - - // - // MemberInfo members - // - - public override Type DeclaringType - { - get - { - return instantiation; - } - } - - public override string Name - { - get - { - return base_method.Name; - } - } - - public override Type ReflectedType - { - get - { - return instantiation; - } - } - - public override Type ReturnType - { - get - { - return base_method.ReturnType; - } - } - - public override Module Module - { - get - { - return base_method.Module; - } - } - - public override bool IsDefined(Type attributeType, bool inherit) - { - throw new NotSupportedException(); - } - - public override object[] GetCustomAttributes(bool inherit) - { - throw new NotSupportedException(); - } - - public override object[] GetCustomAttributes(Type attributeType, bool inherit) - { - throw new NotSupportedException(); - } - - public override string ToString() - { - //IEnumerable`1 get_Item(TKey) - StringBuilder sb = new StringBuilder(ReturnType.ToString()); - sb.Append(' '); - sb.Append(base_method.Name); - sb.Append('('); - sb.Append(')'); - return sb.ToString(); - } - // - // MethodBase members - // - - public override MethodImplAttributes GetMethodImplementationFlags() - { - return base_method.GetMethodImplementationFlags(); - } - - public override ParameterInfo[] GetParameters() - { - return GetParametersInternal(); - } - - internal override ParameterInfo[] GetParametersInternal() - { - throw new NotSupportedException(); - } - - public override int MetadataToken - { - get - { - return base.MetadataToken; - } - } - - internal override int GetParametersCount() - { - return base_method.GetParametersCount(); - } - - public override object? Invoke(object? obj, BindingFlags invokeAttr, Binder? binder, object?[]? parameters, CultureInfo? culture) - { - throw new NotSupportedException(); - } - - public override RuntimeMethodHandle MethodHandle - { - get - { - throw new NotSupportedException(); - } - } - - public override MethodAttributes Attributes - { - get - { - return base_method.Attributes; - } - } - - public override CallingConventions CallingConvention - { - get - { - return base_method.CallingConvention; - } - } - - [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[] methodInstantiation) - { - if (!base_method.IsGenericMethodDefinition || (method_arguments != null)) - throw new InvalidOperationException("Method is not a generic method definition"); - - ArgumentNullException.ThrowIfNull(methodInstantiation); - - if (base_method.GetGenericArguments().Length != methodInstantiation.Length) - throw new ArgumentException("Incorrect length", nameof(methodInstantiation)); - - foreach (Type type in methodInstantiation) - { - ArgumentNullException.ThrowIfNull(type, nameof(methodInstantiation)); - } - - return new MethodOnTypeBuilderInst(this, methodInstantiation); - } - - public override Type[] GetGenericArguments() - { - if (!base_method.IsGenericMethodDefinition) - return Type.EmptyTypes; - Type[] source = method_arguments ?? base_method.GetGenericArguments(); - Type[] result = new Type[source.Length]; - source.CopyTo(result, 0); - return result; - } - - public override MethodInfo GetGenericMethodDefinition() - { - return generic_method_definition ?? base_method; - } - - public override bool ContainsGenericParameters - { - get - { - if (base_method.ContainsGenericParameters) - return true; - if (!base_method.IsGenericMethodDefinition) - throw new NotSupportedException(); - if (method_arguments == null) - return true; - foreach (Type t in method_arguments) - { - if (t.ContainsGenericParameters) - return true; - } - return false; - } - } - - public override bool IsGenericMethodDefinition - { - get - { - return base_method.IsGenericMethodDefinition && method_arguments == null; - } - } - - public override bool IsGenericMethod - { - get - { - return base_method.IsGenericMethodDefinition; - } - } - - // - // MethodInfo members - // - - public override MethodInfo GetBaseDefinition() - { - throw new NotSupportedException(); - } - - public override ParameterInfo ReturnParameter - { - get - { - throw new NotSupportedException(); - } - } - - public override ICustomAttributeProvider ReturnTypeCustomAttributes - { - get - { - throw new NotSupportedException(); - } - } - } -} - -#endif diff --git a/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/MethodOnTypeBuilderInstantiation.Mono.cs b/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/MethodOnTypeBuilderInstantiation.Mono.cs new file mode 100644 index 00000000000000..d083696cb48d81 --- /dev/null +++ b/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/MethodOnTypeBuilderInstantiation.Mono.cs @@ -0,0 +1,134 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +// +// System.Reflection.Emit/MethodOnTypeBuilderInst.cs +// +// Author: +// Zoltan Varga (vargaz@gmail.com) +// +// +// Copyright (C) 2008 Novell, Inc (http://www.novell.com) +// +// Permission is hereby granted, free of charge, to any person obtaining +// a copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to +// permit persons to whom the Software is furnished to do so, subject to +// the following conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +// + +using System.Diagnostics.CodeAnalysis; + +namespace System.Reflection.Emit +{ + internal partial class MethodOnTypeBuilderInstantiation + { + private Type[]? _typeArguments; + private MethodInfo? _genericMethodDefinition; + + internal MethodOnTypeBuilderInstantiation(MethodOnTypeBuilderInstantiation gmd, Type[] typeArguments) + : this(gmd._method, gmd._type) + { + _typeArguments = new Type[typeArguments.Length]; + typeArguments.CopyTo(_typeArguments, 0); + _genericMethodDefinition = gmd; + } + + internal MethodOnTypeBuilderInstantiation(MethodInfo method, Type[] typeArguments) + : this(ExtractBaseMethod(method), method.DeclaringType!) + { + _typeArguments = new Type[typeArguments.Length]; + typeArguments.CopyTo(_typeArguments, 0); + if (_method != method) + _genericMethodDefinition = method; + } + + public override Type[] GetGenericArguments() + { + if (!_method.IsGenericMethodDefinition) + return Type.EmptyTypes; + Type[] source = _typeArguments ?? _method.GetGenericArguments(); + Type[] result = new Type[source.Length]; + source.CopyTo(result, 0); + return result; + } + + public override bool ContainsGenericParameters + { + get + { + if (_method.ContainsGenericParameters) + return true; + if (!_method.IsGenericMethodDefinition) + throw new NotSupportedException(); + if (_typeArguments == null) + return true; + foreach (Type t in _typeArguments) + { + if (t.ContainsGenericParameters) + return true; + } + return false; + } + } + + public override bool IsGenericMethodDefinition => _method.IsGenericMethodDefinition && _typeArguments == null; + + public override MethodInfo GetGenericMethodDefinition() { return _genericMethodDefinition ?? _method; } + + [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[] typeArgs) + { + if (!_method.IsGenericMethodDefinition || (_typeArguments != null)) + throw new InvalidOperationException("Method is not a generic method definition"); + + ArgumentNullException.ThrowIfNull(typeArgs); + + if (_method.GetGenericArguments().Length != typeArgs.Length) + throw new ArgumentException("Incorrect length", nameof(typeArgs)); + + foreach (Type type in typeArgs) + { + ArgumentNullException.ThrowIfNull(type, nameof(typeArgs)); + } + + return new MethodOnTypeBuilderInstantiation(this, typeArgs); + } + + // Called from the runtime to return the corresponding finished MethodInfo object + [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2060:MakeGenericMethod", + Justification = "MethodOnTypeBuilderInst is Reflection.Emit's underlying implementation of MakeGenericMethod. " + + "Callers of the outer calls to MakeGenericMethod will be warned as appropriate.")] + internal MethodInfo RuntimeResolve() + { + Type type = _type.InternalResolve(); + MethodInfo m = type.GetMethod(_method); + if (_typeArguments != null) + { + var args = new Type[_typeArguments.Length]; + for (int i = 0; i < _typeArguments.Length; ++i) + args[i] = _typeArguments[i].InternalResolve(); + m = m.MakeGenericMethod(args); + } + return m; + } + + internal override int GetParametersCount() + { + return _method.GetParametersCount(); + } + } +} diff --git a/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/PropertyOnTypeBuilderInst.cs b/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/PropertyOnTypeBuilderInst.cs deleted file mode 100644 index 0a172d3696c6b0..00000000000000 --- a/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/PropertyOnTypeBuilderInst.cs +++ /dev/null @@ -1,173 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -// -// System.Reflection.Emit/PropertyOnTypeBuilderInst.cs -// -// Author: -// Rodrigo Kumpera (rkumpera@novell.com) -// -// -// Copyright (C) 2009 Novell, Inc (http://www.novell.com) -// -// Permission is hereby granted, free of charge, to any person obtaining -// a copy of this software and associated documentation files (the -// "Software"), to deal in the Software without restriction, including -// without limitation the rights to use, copy, modify, merge, publish, -// distribute, sublicense, and/or sell copies of the Software, and to -// permit persons to whom the Software is furnished to do so, subject to -// the following conditions: -// -// The above copyright notice and this permission notice shall be -// included in all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND -// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE -// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION -// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION -// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -// - -#if MONO_FEATURE_SRE -using System.Globalization; -using System.Runtime.InteropServices; - -namespace System.Reflection.Emit -{ - /* - * This class represents a property of an instantiation of a generic type builder. - */ - [StructLayout(LayoutKind.Sequential)] - internal sealed class PropertyOnTypeBuilderInst : PropertyInfo - { - private TypeBuilderInstantiation instantiation; - private PropertyInfo prop; - - internal PropertyOnTypeBuilderInst(TypeBuilderInstantiation instantiation, PropertyInfo prop) - { - this.instantiation = instantiation; - this.prop = prop; - } - - public override PropertyAttributes Attributes - { - get { throw new NotSupportedException(); } - } - - public override bool CanRead - { - get { throw new NotSupportedException(); } - } - - public override bool CanWrite - { - get { throw new NotSupportedException(); } - } - - public override Type PropertyType - { - get { return instantiation.InflateType(prop.PropertyType)!; } - } - - public override Type? DeclaringType - { - get { return instantiation.InflateType(prop.DeclaringType); } - } - - public override Type ReflectedType - { - get { return instantiation; } - } - - public override string Name - { - get { return prop.Name; } - } - - public override MethodInfo[] GetAccessors(bool nonPublic) - { - MethodInfo? getter = GetGetMethod(nonPublic); - MethodInfo? setter = GetSetMethod(nonPublic); - - int methods = 0; - if (getter != null) - ++methods; - if (setter != null) - ++methods; - - MethodInfo[] res = new MethodInfo[methods]; - - methods = 0; - if (getter != null) - res[methods++] = getter; - if (setter != null) - res[methods] = setter; - - return res; - } - - - public override MethodInfo? GetGetMethod(bool nonPublic) - { - MethodInfo? mi = prop.GetGetMethod(nonPublic); - if (mi != null && prop.DeclaringType == instantiation.generic_type) - { - mi = TypeBuilder.GetMethod(instantiation, mi); - } - return mi; - } - - public override ParameterInfo[] GetIndexParameters() - { - MethodInfo? method = GetGetMethod(true); - if (method != null) - return method.GetParameters(); - - return Array.Empty(); - } - - public override MethodInfo? GetSetMethod(bool nonPublic) - { - MethodInfo? mi = prop.GetSetMethod(nonPublic); - if (mi != null && prop.DeclaringType == instantiation.generic_type) - { - mi = TypeBuilder.GetMethod(instantiation, mi); - } - return mi; - } - - public override string ToString() - { - return string.Format("{0} {1}", PropertyType, Name); - } - - public override object? GetValue(object? obj, BindingFlags invokeAttr, Binder? binder, object?[]? index, CultureInfo? culture) - { - throw new NotSupportedException(); - } - - public override void SetValue(object? obj, object? value, BindingFlags invokeAttr, Binder? binder, object?[]? index, CultureInfo? culture) - { - throw new NotSupportedException(); - } - - public override bool IsDefined(Type attributeType, bool inherit) - { - throw new NotSupportedException(); - } - - public override object[] GetCustomAttributes(bool inherit) - { - throw new NotSupportedException(); - } - - public override object[] GetCustomAttributes(Type attributeType, bool inherit) - { - throw new NotSupportedException(); - } - } -} - -#endif diff --git a/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeAssemblyBuilder.Mono.cs b/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeAssemblyBuilder.Mono.cs index 68449d1a75ab8b..bd50f01bc25c31 100644 --- a/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeAssemblyBuilder.Mono.cs +++ b/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeAssemblyBuilder.Mono.cs @@ -62,9 +62,8 @@ internal GenericInstanceKey(Type gtd, Type[] args) private static bool IsBoundedVector(Type type) { - ArrayType? at = type as ArrayType; - if (at != null) - return at.GetEffectiveRank() == 1; + if (type is SymbolType st && st.IsArray) + return st.GetArrayRank() == 1; return type.ToString().EndsWith("[*]", StringComparison.Ordinal); /*Super uggly hack, SR doesn't allow one to query for it */ } diff --git a/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeEnumBuilder.Mono.cs b/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeEnumBuilder.Mono.cs index e810b3bc56fa28..ab54737e986dcb 100644 --- a/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeEnumBuilder.Mono.cs +++ b/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeEnumBuilder.Mono.cs @@ -427,25 +427,24 @@ public override bool IsDefined(Type attributeType, bool inherit) [RequiresDynamicCode("The code for an array of the specified type might not be available.")] public override Type MakeArrayType() { - return new ArrayType(this, 0); + return SymbolType.FormCompoundType("[]", this, 0)!; } [RequiresDynamicCode("The code for an array of the specified type might not be available.")] public override Type MakeArrayType(int rank) { - if (rank < 1) - throw new IndexOutOfRangeException(); - return new ArrayType(this, rank); + string s = GetRankString(rank); + return SymbolType.FormCompoundType(s, this, 0)!; } public override Type MakeByRefType() { - return new ByRefType(this); + return SymbolType.FormCompoundType("&", this, 0)!; } public override Type MakePointerType() { - return new PointerType(this); + return SymbolType.FormCompoundType("*", this, 0)!; } protected override void SetCustomAttributeCore(CustomAttributeBuilder customBuilder) diff --git a/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeGenericTypeParameterBuilder.cs b/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeGenericTypeParameterBuilder.cs index c6d3740625f4b6..a1f176127f97f8 100644 --- a/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeGenericTypeParameterBuilder.cs +++ b/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeGenericTypeParameterBuilder.cs @@ -471,20 +471,19 @@ public override int GetHashCode() [RequiresDynamicCode("The code for an array of the specified type might not be available.")] public override Type MakeArrayType() { - return new ArrayType(this, 0); + return SymbolType.FormCompoundType("[]", this, 0)!; } [RequiresDynamicCode("The code for an array of the specified type might not be available.")] public override Type MakeArrayType(int rank) { - if (rank < 1) - throw new IndexOutOfRangeException(); - return new ArrayType(this, rank); + string s = GetRankString(rank); + return SymbolType.FormCompoundType(s, this, 0)!; } public override Type MakeByRefType() { - return new ByRefType(this); + return SymbolType.FormCompoundType("&", this, 0)!; } [RequiresDynamicCode("The native code for this instantiation might not be available at runtime.")] @@ -496,7 +495,7 @@ public override Type MakeGenericType(params Type[] typeArguments) public override Type MakePointerType() { - return new PointerType(this); + return SymbolType.FormCompoundType("*", this, 0)!; } internal override bool IsUserType diff --git a/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeMethodBuilder.Mono.cs b/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeMethodBuilder.Mono.cs index 8314602ab64918..1069619fed9d21 100644 --- a/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeMethodBuilder.Mono.cs +++ b/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeMethodBuilder.Mono.cs @@ -543,7 +543,7 @@ public override MethodInfo MakeGenericMethod(params Type[] typeArguments) ArgumentNullException.ThrowIfNull(type, nameof(typeArguments)); } - return new MethodOnTypeBuilderInst(this, typeArguments); + return new MethodOnTypeBuilderInstantiation(this, typeArguments); } public override bool IsGenericMethodDefinition diff --git a/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeModuleBuilder.Mono.cs b/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeModuleBuilder.Mono.cs index 1e9bb754db34de..1870dd0c596b39 100644 --- a/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeModuleBuilder.Mono.cs +++ b/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeModuleBuilder.Mono.cs @@ -600,11 +600,11 @@ private int GetPseudoToken(MemberInfo member, bool create_open_instance) // allocated by the runtime if (member is TypeBuilderInstantiation || member is SymbolType) token = typespec_tokengen--; - else if (member is FieldOnTypeBuilderInst) + else if (member is FieldOnTypeBuilderInstantiation) token = memberref_tokengen--; - else if (member is ConstructorOnTypeBuilderInst) + else if (member is ConstructorOnTypeBuilderInstantiation) token = memberref_tokengen--; - else if (member is MethodOnTypeBuilderInst) + else if (member is MethodOnTypeBuilderInstantiation) token = memberref_tokengen--; else if (member is FieldBuilder) token = memberref_tokengen--; @@ -659,8 +659,9 @@ internal int GetToken(MemberInfo member) internal int GetToken(MemberInfo member, bool create_open_instance) { - if (member is TypeBuilderInstantiation || member is FieldOnTypeBuilderInst || member is ConstructorOnTypeBuilderInst || member is MethodOnTypeBuilderInst || member is SymbolType || member is FieldBuilder || member is TypeBuilder || member is ConstructorBuilder || member is MethodBuilder || member is GenericTypeParameterBuilder || - member is EnumBuilder) + if (member is TypeBuilderInstantiation || member is FieldOnTypeBuilderInstantiation || member is ConstructorOnTypeBuilderInstantiation || + member is MethodOnTypeBuilderInstantiation || member is SymbolType || member is FieldBuilder || member is TypeBuilder || + member is ConstructorBuilder || member is MethodBuilder || member is GenericTypeParameterBuilder || member is EnumBuilder) return GetPseudoToken(member, create_open_instance); return getToken(this, member, create_open_instance); } @@ -715,11 +716,11 @@ internal static object RuntimeResolve(object obj) return fb.RuntimeResolve(); if (obj is RuntimeGenericTypeParameterBuilder gtpb) return gtpb.RuntimeResolve(); - if (obj is FieldOnTypeBuilderInst fotbi) + if (obj is FieldOnTypeBuilderInstantiation fotbi) return fotbi.RuntimeResolve(); - if (obj is MethodOnTypeBuilderInst motbi) + if (obj is MethodOnTypeBuilderInstantiation motbi) return motbi.RuntimeResolve(); - if (obj is ConstructorOnTypeBuilderInst cotbi) + if (obj is ConstructorOnTypeBuilderInstantiation cotbi) return cotbi.RuntimeResolve(); if (obj is Type t) return t.RuntimeResolve(); diff --git a/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeTypeBuilder.Mono.cs b/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeTypeBuilder.Mono.cs index 35e6b027d8eafe..ff4b95d0b5caf2 100644 --- a/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeTypeBuilder.Mono.cs +++ b/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeTypeBuilder.Mono.cs @@ -34,7 +34,6 @@ // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // -#if MONO_FEATURE_SRE using System.Collections.Generic; using System.Diagnostics; using System.Diagnostics.CodeAnalysis; @@ -137,7 +136,7 @@ public static FieldInfo GetField(Type type, FieldInfo field) if (field.DeclaringType != type.GetGenericTypeDefinition()) throw new ArgumentException(SR.Argument_InvalidFieldDeclaringType, nameof(type)); - if (field is FieldOnTypeBuilderInst) + if (field is FieldOnTypeBuilderInstantiation) throw new ArgumentException("The specified field must be declared on a generic type definition.", nameof(field)); FieldInfo res = type.GetField(field); @@ -1360,20 +1359,19 @@ public override bool IsSZArray [RequiresDynamicCode("The code for an array of the specified type might not be available.")] public override Type MakeArrayType() { - return new ArrayType(this, 0); + return SymbolType.FormCompoundType("[]", this, 0)!; } [RequiresDynamicCode("The code for an array of the specified type might not be available.")] public override Type MakeArrayType(int rank) { - if (rank < 1) - throw new IndexOutOfRangeException(); - return new ArrayType(this, rank); + string s = GetRankString(rank); + return SymbolType.FormCompoundType(s, this, 0)!; } public override Type MakeByRefType() { - return new ByRefType(this); + return SymbolType.FormCompoundType("&", this, 0)!; } [RequiresDynamicCode("The native code for this instantiation might not be available at runtime.")] @@ -1401,7 +1399,7 @@ public override Type MakeGenericType(params Type[] typeArguments) public override Type MakePointerType() { - return new PointerType(this); + return SymbolType.FormCompoundType("*", this, 0)!; } public override RuntimeTypeHandle TypeHandle @@ -1954,4 +1952,3 @@ private static void throw_argument_ConstantDoesntMatch() public override bool IsByRefLike => false; } } -#endif diff --git a/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/SymbolType.Mono.cs b/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/SymbolType.Mono.cs new file mode 100644 index 00000000000000..8351ba208b08d6 --- /dev/null +++ b/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/SymbolType.Mono.cs @@ -0,0 +1,77 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +// +// System.Reflection.Emit.DerivedTypes.cs +// +// Authors: +// Rodrigo Kumpera +// +// +// Copyright (C) 2009 Novell, Inc (http://www.novell.com) +// +// Permission is hereby granted, free of charge, to any person obtaining +// a copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to +// permit persons to whom the Software is furnished to do so, subject to +// the following conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +// + +using System.Runtime.InteropServices; + +namespace System.Reflection.Emit +{ + [StructLayout(LayoutKind.Sequential)] + internal partial class SymbolType + { + // Sequence of _baseType, _typeKind and _rank fields should kept in sync with MonoReflectionSymbolType in object-internals.h + + internal override Type InternalResolve() + { + switch (_typeKind) + { + case TypeKind.IsArray: + { + Type et = _baseType.InternalResolve(); + if (_rank == 1) + return et.MakeArrayType(); + return et.MakeArrayType(_rank); + } + case TypeKind.IsByRef: return _baseType.InternalResolve().MakeByRefType(); + case TypeKind.IsPointer: return _baseType.InternalResolve().MakePointerType(); + } + + throw new NotSupportedException(); + } + + // Called from the runtime to return the corresponding finished Type object + internal override Type RuntimeResolve() + { + if (_typeKind == TypeKind.IsArray) + { + Type et = _baseType.RuntimeResolve(); + if (_rank == 1) + { + return et.MakeArrayType(); + } + + return et.MakeArrayType(_rank); + } + + return InternalResolve(); + } + } +} diff --git a/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilderInstantiation.Mono.cs b/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilderInstantiation.Mono.cs new file mode 100644 index 00000000000000..51e9159ab4d8db --- /dev/null +++ b/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilderInstantiation.Mono.cs @@ -0,0 +1,100 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +// +// System.Reflection.Emit.TypeBuilderInstantiation +// +// Sean MacIsaac (macisaac@ximian.com) +// Paolo Molaro (lupus@ximian.com) +// Patrik Torstensson (patrik.torstensson@labs2.com) +// +// (C) 2001 Ximian, Inc. +// + +// +// Copyright (C) 2004 Novell, Inc (http://www.novell.com) +// +// Permission is hereby granted, free of charge, to any person obtaining +// a copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to +// permit persons to whom the Software is furnished to do so, subject to +// the following conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +// + +using System.Diagnostics.CodeAnalysis; +using System.Runtime.InteropServices; + +namespace System.Reflection.Emit +{ + [StructLayout(LayoutKind.Sequential)] + internal partial class TypeBuilderInstantiation + { + // Sequence of _genericType and _typeArguments fields should kept in sync with MonoReflectionGenericClass in object-internals.h + + [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2055:UnrecognizedReflectionPattern", + Justification = "Reflection.Emit is not subject to trimming")] + internal override Type InternalResolve() + { + Type gtd = _genericType.InternalResolve(); + Type[] args = new Type[_typeArguments.Length]; + for (int i = 0; i < _typeArguments.Length; ++i) + args[i] = _typeArguments[i].InternalResolve(); + return gtd.MakeGenericType(args); + } + + // Called from the runtime to return the corresponding finished Type object + internal override Type RuntimeResolve() + { + if (_genericType is TypeBuilder tb && !tb.IsCreated()) + throw new NotImplementedException(); + for (int i = 0; i < _typeArguments.Length; ++i) + { + Type t = _typeArguments[i]; + if (t is TypeBuilder ttb && !ttb.IsCreated()) + throw new NotImplementedException(); + } + return InternalResolve(); + } + + internal override bool IsUserType + { + get + { + foreach (Type t in _typeArguments) + { + if (t.IsUserType) + return true; + } + return false; + } + } + + internal override MethodInfo GetMethod(MethodInfo fromNonInstantiated) + { + return new MethodOnTypeBuilderInstantiation(fromNonInstantiated, this); + } + + internal override ConstructorInfo GetConstructor(ConstructorInfo fromNoninstanciated) + { + return new ConstructorOnTypeBuilderInstantiation(fromNoninstanciated, this); + } + + internal override FieldInfo GetField(FieldInfo fromNoninstanciated) + { + return FieldOnTypeBuilderInstantiation.GetField(fromNoninstanciated, this); + } + } +} 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 deleted file mode 100644 index abf9999b10fae8..00000000000000 --- a/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilderInstantiation.cs +++ /dev/null @@ -1,565 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -// -// System.Reflection.Emit.TypeBuilderInstantiation -// -// Sean MacIsaac (macisaac@ximian.com) -// Paolo Molaro (lupus@ximian.com) -// Patrik Torstensson (patrik.torstensson@labs2.com) -// -// (C) 2001 Ximian, Inc. -// - -// -// Copyright (C) 2004 Novell, Inc (http://www.novell.com) -// -// Permission is hereby granted, free of charge, to any person obtaining -// a copy of this software and associated documentation files (the -// "Software"), to deal in the Software without restriction, including -// without limitation the rights to use, copy, modify, merge, publish, -// distribute, sublicense, and/or sell copies of the Software, and to -// permit persons to whom the Software is furnished to do so, subject to -// the following conditions: -// -// The above copyright notice and this permission notice shall be -// included in all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND -// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE -// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION -// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION -// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -// - -#if MONO_FEATURE_SRE -using System.Collections.Generic; -using System.Globalization; -using System.Text; -using System.Runtime.InteropServices; -using System.Diagnostics.CodeAnalysis; - -namespace System.Reflection.Emit -{ - /* - * TypeBuilderInstantiation represents an instantiation of a generic TypeBuilder. - */ - [StructLayout(LayoutKind.Sequential)] - internal sealed class TypeBuilderInstantiation : - TypeInfo - { -#region Keep in sync with object-internals.h MonoReflectionGenericClass -#pragma warning disable 649 - internal Type generic_type; - private Type[] type_arguments; -#pragma warning restore 649 -#endregion - - private Dictionary? fields; - private Dictionary? ctors; - private Dictionary? methods; - - internal TypeBuilderInstantiation() - { - // this should not be used - throw new InvalidOperationException(); - } - - internal TypeBuilderInstantiation(Type tb, Type[] args) - { - this.generic_type = tb; - this.type_arguments = args; - } - - [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2055:UnrecognizedReflectionPattern", - Justification = "Reflection.Emit is not subject to trimming")] - internal override Type InternalResolve() - { - Type gtd = generic_type.InternalResolve(); - Type[] args = new Type[type_arguments.Length]; - for (int i = 0; i < type_arguments.Length; ++i) - args[i] = type_arguments[i].InternalResolve(); - return gtd.MakeGenericType(args); - } - - // Called from the runtime to return the corresponding finished Type object - internal override Type RuntimeResolve() - { - if (generic_type is TypeBuilder tb && !tb.IsCreated()) - throw new NotImplementedException(); - for (int i = 0; i < type_arguments.Length; ++i) - { - Type t = type_arguments[i]; - if (t is TypeBuilder ttb && !ttb.IsCreated()) - throw new NotImplementedException(); - } - return InternalResolve(); - } - - internal bool IsCreated - { - get - { - return generic_type is RuntimeTypeBuilder tb ? tb.is_created : true; - } - } - - private const BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance | BindingFlags.DeclaredOnly; - - private Type? GetParentType() - { - return InflateType(generic_type.BaseType); - } - - internal Type? InflateType(Type? type) - { - return InflateType(type, type_arguments, null); - } - - internal Type? InflateType(Type type, Type[] method_args) - { - return InflateType(type, type_arguments, method_args); - } - - [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2055:UnrecognizedReflectionPattern", - Justification = "Reflection emitted types have all of their members")] - internal static Type? InflateType(Type? type, Type[]? type_args, Type[]? method_args) - { - if (type == null) - return null; - if (!type.IsGenericParameter && !type.ContainsGenericParameters) - return type; - if (type.IsGenericParameter) - { - if (type.DeclaringMethod == null) - return type_args == null ? type : type_args[type.GenericParameterPosition]; - return method_args == null ? type : method_args[type.GenericParameterPosition]; - } - if (type.IsPointer) - return InflateType(type.GetElementType(), type_args, method_args)!.MakePointerType(); - if (type.IsByRef) - return InflateType(type.GetElementType(), type_args, method_args)!.MakeByRefType(); - if (type.IsArray) - { - if (type.GetArrayRank() > 1) - return InflateType(type.GetElementType(), type_args, method_args)!.MakeArrayType(type.GetArrayRank()); - - if (type.ToString().EndsWith("[*]", StringComparison.Ordinal)) /*FIXME, the reflection API doesn't offer a way around this*/ - return InflateType(type.GetElementType(), type_args, method_args)!.MakeArrayType(1); - return InflateType(type.GetElementType(), type_args, method_args)!.MakeArrayType(); - } - - Type[] args = type.GetGenericArguments(); - for (int i = 0; i < args.Length; ++i) - args[i] = InflateType(args[i], type_args, method_args)!; - - Type gtd = type.IsGenericTypeDefinition ? type : type.GetGenericTypeDefinition(); - return gtd.MakeGenericType(args); - } - - public override Type? BaseType - { - get { return generic_type.BaseType; } - } - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.Interfaces)] - public override Type[] GetInterfaces() - { - throw new NotSupportedException(); - } - - protected override bool IsValueTypeImpl() - { - return generic_type.IsValueType; - } - - internal override MethodInfo GetMethod(MethodInfo fromNoninstanciated) - { - methods ??= new Dictionary(); - if (!methods.TryGetValue(fromNoninstanciated, out MethodInfo? mi)) - { - methods[fromNoninstanciated] = mi = new MethodOnTypeBuilderInst(this, fromNoninstanciated); - } - return mi; - } - - internal override ConstructorInfo GetConstructor(ConstructorInfo fromNoninstanciated) - { - ctors ??= new Dictionary(); - if (!ctors.TryGetValue(fromNoninstanciated, out ConstructorInfo? ci)) - { - ctors[fromNoninstanciated] = ci = new ConstructorOnTypeBuilderInst(this, fromNoninstanciated); - } - return ci; - } - - internal override FieldInfo GetField(FieldInfo fromNoninstanciated) - { - fields ??= new Dictionary(); - if (!fields.TryGetValue(fromNoninstanciated, out FieldInfo? fi)) - { - fields[fromNoninstanciated] = fi = new FieldOnTypeBuilderInst(this, fromNoninstanciated); - } - return fi; - } - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.NonPublicMethods)] - public override MethodInfo[] GetMethods(BindingFlags bf) - { - throw new NotSupportedException(); - } - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)] - public override ConstructorInfo[] GetConstructors(BindingFlags bf) - { - throw new NotSupportedException(); - } - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)] - public override FieldInfo[] GetFields(BindingFlags bf) - { - throw new NotSupportedException(); - } - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypes.NonPublicProperties)] - public override PropertyInfo[] GetProperties(BindingFlags bf) - { - throw new NotSupportedException(); - } - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicEvents | DynamicallyAccessedMemberTypes.NonPublicEvents)] - public override EventInfo[] GetEvents(BindingFlags bf) - { - throw new NotSupportedException(); - } - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicNestedTypes | DynamicallyAccessedMemberTypes.NonPublicNestedTypes)] - public override Type[] GetNestedTypes(BindingFlags bf) - { - throw new NotSupportedException(); - } - - public override bool IsAssignableFrom(Type? c) - { - throw new NotSupportedException(); - } - - public override Type UnderlyingSystemType - { - get { return this; } - } - - public override Assembly Assembly - { - get { return generic_type.Assembly; } - } - - public override Module Module - { - get { return generic_type.Module; } - } - - public override string Name - { - get { return generic_type.Name; } - } - - public override string? Namespace - { - get { return generic_type.Namespace; } - } - - public override string? FullName - { - get { return format_name(true, false); } - } - - public override string? AssemblyQualifiedName - { - get { return format_name(true, true); } - } - - public override Guid GUID - { - get { throw new NotSupportedException(); } - } - - private string? format_name(bool full_name, bool assembly_qualified) - { - StringBuilder sb = new StringBuilder(generic_type.FullName); - - sb.Append('['); - for (int i = 0; i < type_arguments.Length; ++i) - { - if (i > 0) - sb.Append(','); - - string? name; - if (full_name) - { - string? assemblyName = type_arguments[i].Assembly.FullName; - name = type_arguments[i].FullName; - if (name != null && assemblyName != null) - name = name + ", " + assemblyName; - } - else - { - name = type_arguments[i].ToString(); - } - if (name == null) - { - return null; - } - if (full_name) - sb.Append('['); - sb.Append(name); - if (full_name) - sb.Append(']'); - } - sb.Append(']'); - if (assembly_qualified) - { - sb.Append(", "); - sb.Append(generic_type.Assembly.FullName); - } - return sb.ToString(); - } - - public override string ToString() - { - return format_name(false, false)!; - } - - public override Type GetGenericTypeDefinition() - { - return generic_type; - } - - public override Type[] GetGenericArguments() - { - Type[] ret = new Type[type_arguments.Length]; - type_arguments.CopyTo(ret, 0); - return ret; - } - - public override bool ContainsGenericParameters - { - get - { - foreach (Type t in type_arguments) - { - if (t.ContainsGenericParameters) - return true; - } - return false; - } - } - - public override bool IsGenericTypeDefinition - { - get { return false; } - } - - public override bool IsGenericType - { - get { return true; } - } - - public override Type? DeclaringType - { - get { return generic_type.DeclaringType; } - } - - public override RuntimeTypeHandle TypeHandle - { - get - { - throw new NotSupportedException(); - } - } - - public override Type MakeArrayType() - { - return new ArrayType(this, 0); - } - - public override Type MakeArrayType(int rank) - { - if (rank < 1) - throw new IndexOutOfRangeException(); - return new ArrayType(this, rank); - } - - public override Type MakeByRefType() - { - return new ByRefType(this); - } - - public override Type MakePointerType() - { - return new PointerType(this); - } - - public override Type GetElementType() - { - throw new NotSupportedException(); - } - - protected override bool HasElementTypeImpl() - { - return false; - } - - protected override bool IsCOMObjectImpl() - { - return false; - } - - protected override bool IsPrimitiveImpl() - { - return false; - } - - protected override bool IsArrayImpl() - { - return false; - } - - protected override bool IsByRefImpl() - { - return false; - } - - protected override bool IsPointerImpl() - { - return false; - } - - protected override TypeAttributes GetAttributeFlagsImpl() - { - return generic_type.Attributes; - } - - //stuff that throws - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.Interfaces)] - [return: DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.Interfaces)] - public override Type GetInterface(string name, bool ignoreCase) - { - throw new NotSupportedException(); - } - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicEvents | DynamicallyAccessedMemberTypes.NonPublicEvents)] - public override EventInfo GetEvent(string name, BindingFlags bindingAttr) - { - throw new NotSupportedException(); - } - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)] - public override FieldInfo GetField(string name, BindingFlags bindingAttr) - { - throw new NotSupportedException(); - } - - [DynamicallyAccessedMembers(GetAllMembers)] - public override MemberInfo[] GetMembers(BindingFlags bindingAttr) - { - throw new NotSupportedException(); - } - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicNestedTypes | DynamicallyAccessedMemberTypes.NonPublicNestedTypes)] - public override Type GetNestedType(string name, BindingFlags bindingAttr) - { - throw new NotSupportedException(); - } - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] - public override object? InvokeMember(string name, BindingFlags invokeAttr, - Binder? binder, object? target, object?[]? args, - ParameterModifier[]? modifiers, - CultureInfo? culture, string[]? namedParameters) - { - throw new NotSupportedException(); - } - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.NonPublicMethods)] - protected override MethodInfo? GetMethodImpl(string name, BindingFlags bindingAttr, Binder? binder, - CallingConventions callConvention, Type[]? types, - ParameterModifier[]? modifiers) - { - throw new NotSupportedException(); - } - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypes.NonPublicProperties)] - protected override PropertyInfo? GetPropertyImpl(string name, BindingFlags bindingAttr, Binder? binder, - Type? returnType, Type[]? types, ParameterModifier[]? modifiers) - { - throw new NotSupportedException(); - } - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)] - protected override ConstructorInfo? GetConstructorImpl(BindingFlags bindingAttr, - Binder? binder, - CallingConventions callConvention, - Type[]? types, - ParameterModifier[]? modifiers) - { - throw new NotSupportedException(); - } - - //MemberInfo - public override bool IsDefined(Type attributeType, bool inherit) - { - throw new NotSupportedException(); - } - - public override object[] GetCustomAttributes(bool inherit) - { - if (IsCreated) - return generic_type.GetCustomAttributes(inherit); - throw new NotSupportedException(); - } - - public override object[] GetCustomAttributes(Type attributeType, bool inherit) - { - if (IsCreated) - return generic_type.GetCustomAttributes(attributeType, inherit); - throw new NotSupportedException(); - } - - internal override bool IsUserType - { - get - { - foreach (Type t in type_arguments) - { - if (t.IsUserType) - return true; - } - return false; - } - } - - internal static Type MakeGenericType(Type type, Type[] typeArguments) - { - return new TypeBuilderInstantiation(type, typeArguments); - } - - public override bool IsTypeDefinition => false; - - public override bool IsConstructedGenericType => true; - } -} -#else -namespace System.Reflection.Emit -{ - abstract class TypeBuilderInstantiation : TypeInfo - { - internal static Type MakeGenericType (Type type, Type[] typeArguments) - { - throw new NotSupportedException("User types are not supported under full aot"); - } - } -} -#endif diff --git a/src/mono/System.Private.CoreLib/src/System/Reflection/MethodBase.Mono.cs b/src/mono/System.Private.CoreLib/src/System/Reflection/MethodBase.Mono.cs index 02ae6eebd1a99b..f498bebafc19f3 100644 --- a/src/mono/System.Private.CoreLib/src/System/Reflection/MethodBase.Mono.cs +++ b/src/mono/System.Private.CoreLib/src/System/Reflection/MethodBase.Mono.cs @@ -59,17 +59,6 @@ internal virtual Type GetParameterType(int pos) throw new NotImplementedException(); } - internal virtual Type[] GetParameterTypes() - { - ParameterInfo[] paramInfo = GetParametersNoCopy(); - - Type[] parameterTypes = new Type[paramInfo.Length]; - for (int i = 0; i < paramInfo.Length; i++) - parameterTypes[i] = paramInfo[i].ParameterType; - - return parameterTypes; - } - internal virtual int get_next_table_index(int table, int count) { throw new NotImplementedException(); diff --git a/src/mono/System.Private.CoreLib/src/System/Reflection/RuntimeMethodInfo.Mono.cs b/src/mono/System.Private.CoreLib/src/System/Reflection/RuntimeMethodInfo.Mono.cs index 1c6f60307f3f51..975b5096c93565 100644 --- a/src/mono/System.Private.CoreLib/src/System/Reflection/RuntimeMethodInfo.Mono.cs +++ b/src/mono/System.Private.CoreLib/src/System/Reflection/RuntimeMethodInfo.Mono.cs @@ -640,7 +640,7 @@ public override MethodInfo MakeGenericMethod(Type[] methodInstantiation) if (hasUserType) { if (RuntimeFeature.IsDynamicCodeSupported) - return new MethodOnTypeBuilderInst(this, methodInstantiation); + return new MethodOnTypeBuilderInstantiation(this, methodInstantiation); throw new NotSupportedException("User types are not supported under full aot"); } diff --git a/src/mono/mono/metadata/object-internals.h b/src/mono/mono/metadata/object-internals.h index 0a50a6428e71f7..58a01fbe609b84 100644 --- a/src/mono/mono/metadata/object-internals.h +++ b/src/mono/mono/metadata/object-internals.h @@ -1237,19 +1237,12 @@ struct _MonoReflectionTypeBuilder { typedef struct { MonoReflectionType type; MonoReflectionType *element_type; + gint32 type_kind; gint32 rank; -} MonoReflectionArrayType; +} MonoReflectionSymbolType; -/* Safely access System.Reflection.Emit.ArrayType (in DerivedTypes.cs) from native code */ -TYPED_HANDLE_DECL (MonoReflectionArrayType); - -typedef struct { - MonoReflectionType type; - MonoReflectionType *element_type; -} MonoReflectionDerivedType; - -/* Safely access System.Reflection.Emit.SymbolType and subclasses (in DerivedTypes.cs) from native code */ -TYPED_HANDLE_DECL (MonoReflectionDerivedType); +/* Safely access System.Reflection.Emit.SymbolType from native code */ +TYPED_HANDLE_DECL (MonoReflectionSymbolType); typedef struct { MonoReflectionType type; diff --git a/src/mono/mono/metadata/sre.c b/src/mono/mono/metadata/sre.c index 2be4921243b240..b582c5cf4219bc 100644 --- a/src/mono/mono/metadata/sre.c +++ b/src/mono/mono/metadata/sre.c @@ -82,9 +82,7 @@ static gboolean reflection_setup_class_hierarchy (GHashTable *unparented, MonoEr static char* type_get_qualified_name (MonoType *type, MonoAssembly *ass); static MonoReflectionTypeHandle mono_reflection_type_get_underlying_system_type (MonoReflectionTypeHandle t, MonoError *error); -static gboolean is_sre_array (MonoClass *klass); -static gboolean is_sre_byref (MonoClass *klass); -static gboolean is_sre_pointer (MonoClass *klass); +static gboolean is_sre_symboltype (MonoClass *klass); static gboolean is_sre_generic_instance (MonoClass *klass); static gboolean is_sre_type_builder (MonoClass *klass); static gboolean is_sre_gparam_builder (MonoClass *klass); @@ -1445,21 +1443,9 @@ mono_type_array_get_and_resolve_with_modifiers (MonoArrayHandle types, MonoArray #ifndef DISABLE_REFLECTION_EMIT static gboolean -is_sre_array (MonoClass *klass) +is_sre_symboltype (MonoClass *klass) { - check_corlib_type_cached (klass, "System.Reflection.Emit", "ArrayType"); -} - -static gboolean -is_sre_byref (MonoClass *klass) -{ - check_corlib_type_cached (klass, "System.Reflection.Emit", "ByRefType"); -} - -static gboolean -is_sre_pointer (MonoClass *klass) -{ - check_corlib_type_cached (klass, "System.Reflection.Emit", "PointerType"); + check_corlib_type_cached (klass, "System.Reflection.Emit", "SymbolType"); } static gboolean @@ -1525,13 +1511,13 @@ is_sre_enum_builder (MonoClass *klass) gboolean mono_is_sre_method_on_tb_inst (MonoClass *klass) { - check_corlib_type_cached (klass, "System.Reflection.Emit", "MethodOnTypeBuilderInst"); + check_corlib_type_cached (klass, "System.Reflection.Emit", "MethodOnTypeBuilderInstantiation"); } gboolean mono_is_sre_ctor_on_tb_inst (MonoClass *klass) { - check_corlib_type_cached (klass, "System.Reflection.Emit", "ConstructorOnTypeBuilderInst"); + check_corlib_type_cached (klass, "System.Reflection.Emit", "ConstructorOnTypeBuilderInstantiation"); } static MonoReflectionTypeHandle @@ -1709,42 +1695,39 @@ mono_reflection_type_handle_mono_type (MonoReflectionTypeHandle ref, MonoError * MonoClass *klass; klass = mono_handle_class (ref); - if (is_sre_array (klass)) { - MonoReflectionArrayTypeHandle sre_array = MONO_HANDLE_CAST (MonoReflectionArrayType, ref); - MonoReflectionTypeHandle ref_element = MONO_HANDLE_NEW_GET (MonoReflectionType, sre_array, element_type); + if (is_sre_symboltype (klass)) { + MonoReflectionSymbolTypeHandle sre_symbol = MONO_HANDLE_CAST (MonoReflectionSymbolType, ref); + MonoReflectionTypeHandle ref_element = MONO_HANDLE_NEW_GET (MonoReflectionType, sre_symbol, element_type); MonoType *base = mono_reflection_type_handle_mono_type (ref_element, error); goto_if_nok (error, leave); g_assert (base); - uint8_t rank = GINT32_TO_UINT8 (MONO_HANDLE_GETVAL (sre_array, rank)); - MonoClass *eclass = mono_class_from_mono_type_internal (base); - result = mono_image_new0 (eclass->image, MonoType, 1); - if (rank == 0) { - result->type = MONO_TYPE_SZARRAY; - result->data.klass = eclass; - } else { - MonoArrayType *at = (MonoArrayType *)mono_image_alloc0 (eclass->image, sizeof (MonoArrayType)); - result->type = MONO_TYPE_ARRAY; - result->data.array = at; - at->eklass = eclass; - at->rank = rank; + uint8_t type_kind = GINT32_TO_UINT8 (MONO_HANDLE_GETVAL (sre_symbol, type_kind)); + switch (type_kind) + { + case 1 : { + uint8_t rank = GINT32_TO_UINT8 (MONO_HANDLE_GETVAL (sre_symbol, rank)); + MonoClass *eclass = mono_class_from_mono_type_internal (base); + result = mono_image_new0 (eclass->image, MonoType, 1); + if (rank == 0) { + result->type = MONO_TYPE_SZARRAY; + result->data.klass = eclass; + } else { + MonoArrayType *at = (MonoArrayType *)mono_image_alloc0 (eclass->image, sizeof (MonoArrayType)); + result->type = MONO_TYPE_ARRAY; + result->data.array = at; + at->eklass = eclass; + at->rank = rank; + } + } + break; + case 2 : result = m_class_get_byval_arg (mono_class_create_ptr (base)); + break; + case 3 : result = &mono_class_from_mono_type_internal (base)->this_arg; + break; + default: + break; } MONO_HANDLE_SETVAL (ref, type, MonoType*, result); - } else if (is_sre_byref (klass)) { - MonoReflectionDerivedTypeHandle sre_byref = MONO_HANDLE_CAST (MonoReflectionDerivedType, ref); - MonoReflectionTypeHandle ref_element = MONO_HANDLE_NEW_GET (MonoReflectionType, sre_byref, element_type); - MonoType *base = mono_reflection_type_handle_mono_type (ref_element, error); - goto_if_nok (error, leave); - g_assert (base); - result = &mono_class_from_mono_type_internal (base)->this_arg; - MONO_HANDLE_SETVAL (ref, type, MonoType*, result); - } else if (is_sre_pointer (klass)) { - MonoReflectionDerivedTypeHandle sre_pointer = MONO_HANDLE_CAST (MonoReflectionDerivedType, ref); - MonoReflectionTypeHandle ref_element = MONO_HANDLE_NEW_GET (MonoReflectionType, sre_pointer, element_type); - MonoType *base = mono_reflection_type_handle_mono_type (ref_element, error); - goto_if_nok (error, leave); - g_assert (base); - result = m_class_get_byval_arg (mono_class_create_ptr (base)); - MONO_HANDLE_SETVAL (ref, type, MonoType*, result); } else if (is_sre_generic_instance (klass)) { result = reflection_instance_handle_mono_type (MONO_HANDLE_CAST (MonoReflectionGenericClass, ref), error); } else if (is_sre_gparam_builder (klass)) { @@ -4306,12 +4289,10 @@ mono_reflection_resolve_object (MonoImage *image, MonoObject *obj, MonoClass **h mono_is_sre_field_builder (oklass) || is_sre_gparam_builder (oklass) || is_sre_generic_instance (oklass) || - is_sre_array (oklass) || - is_sre_byref (oklass) || - is_sre_pointer (oklass) || - !strcmp (oklass->name, "FieldOnTypeBuilderInst") || - !strcmp (oklass->name, "MethodOnTypeBuilderInst") || - !strcmp (oklass->name, "ConstructorOnTypeBuilderInst")) { + is_sre_symboltype (oklass) || + !strcmp (oklass->name, "FieldOnTypeBuilderInstantiation") || + !strcmp (oklass->name, "MethodOnTypeBuilderInstantiation") || + !strcmp (oklass->name, "ConstructorOnTypeBuilderInstantiation")) { static MonoMethod *resolve_method; if (!resolve_method) { MonoMethod *m = mono_class_get_method_from_name_checked (mono_class_get_module_builder_class (), "RuntimeResolve", 1, 0, error);