From 1937454886dc096e36a8b0bee8ca2b890478e61d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Strehovsk=C3=BD?= Date: Thu, 19 Jan 2023 16:10:48 +0900 Subject: [PATCH] Remove uses of `TypeInfo` from the reflection stack (#80833) Contributes to #80165. Not aware of a good reason to keep using it. Removes `TypeDelegator` from apps that don't use it. Saves 7 kB (but also stepping stone for the larger savings later). --- .../Runtime/BindingFlagSupport/ConstructorPolicies.cs | 4 ++-- .../Runtime/BindingFlagSupport/EventPolicies.cs | 4 ++-- .../Runtime/BindingFlagSupport/FieldPolicies.cs | 4 ++-- .../Runtime/BindingFlagSupport/MemberPolicies.cs | 6 ++++-- .../Runtime/BindingFlagSupport/MethodPolicies.cs | 4 ++-- .../Runtime/BindingFlagSupport/NestedTypePolicies.cs | 4 ++-- .../Runtime/BindingFlagSupport/PropertyPolicies.cs | 4 ++-- .../Reflection/Runtime/BindingFlagSupport/Shared.cs | 8 ++++---- .../Runtime/General/TypeResolver.NativeFormat.cs | 6 +++--- .../ExecutionEnvironmentImplementation.MappingTables.cs | 8 ++++---- .../Execution/ReflectionDomainSetupImplementation.cs | 4 ++-- 11 files changed, 29 insertions(+), 27 deletions(-) diff --git a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Runtime/BindingFlagSupport/ConstructorPolicies.cs b/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Runtime/BindingFlagSupport/ConstructorPolicies.cs index 3bfe90d49c136..d2a513a81db07 100644 --- a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Runtime/BindingFlagSupport/ConstructorPolicies.cs +++ b/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Runtime/BindingFlagSupport/ConstructorPolicies.cs @@ -19,9 +19,9 @@ public ConstructorPolicies() : base(MemberTypeIndex.Constructor) { } [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2070:UnrecognizedReflectionPattern", Justification = "Reflection implementation")] - public sealed override IEnumerable GetDeclaredMembers(TypeInfo typeInfo) + public sealed override IEnumerable GetDeclaredMembers(Type type) { - return typeInfo.DeclaredConstructors; + return type.GetConstructors(DeclaredOnlyLookup); } public sealed override IEnumerable CoreGetDeclaredMembers(RuntimeTypeInfo type, NameFilter? optionalNameFilter, RuntimeTypeInfo reflectedType) diff --git a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Runtime/BindingFlagSupport/EventPolicies.cs b/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Runtime/BindingFlagSupport/EventPolicies.cs index 6c5bd52835d64..056eb843b78eb 100644 --- a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Runtime/BindingFlagSupport/EventPolicies.cs +++ b/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Runtime/BindingFlagSupport/EventPolicies.cs @@ -19,9 +19,9 @@ public EventPolicies() : base(MemberTypeIndex.Event) { } [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2070:UnrecognizedReflectionPattern", Justification = "Reflection implementation")] - public sealed override IEnumerable GetDeclaredMembers(TypeInfo typeInfo) + public sealed override IEnumerable GetDeclaredMembers(Type type) { - return typeInfo.DeclaredEvents; + return type.GetEvents(DeclaredOnlyLookup); } public sealed override IEnumerable CoreGetDeclaredMembers(RuntimeTypeInfo type, NameFilter? optionalNameFilter, RuntimeTypeInfo reflectedType) diff --git a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Runtime/BindingFlagSupport/FieldPolicies.cs b/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Runtime/BindingFlagSupport/FieldPolicies.cs index 6447680dc6ea1..b038fc89056d7 100644 --- a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Runtime/BindingFlagSupport/FieldPolicies.cs +++ b/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Runtime/BindingFlagSupport/FieldPolicies.cs @@ -19,9 +19,9 @@ public FieldPolicies() : base(MemberTypeIndex.Field) { } [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2070:UnrecognizedReflectionPattern", Justification = "Reflection implementation")] - public sealed override IEnumerable GetDeclaredMembers(TypeInfo typeInfo) + public sealed override IEnumerable GetDeclaredMembers(Type type) { - return typeInfo.DeclaredFields; + return type.GetFields(DeclaredOnlyLookup); } public sealed override IEnumerable CoreGetDeclaredMembers(RuntimeTypeInfo type, NameFilter? optionalNameFilter, RuntimeTypeInfo reflectedType) diff --git a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Runtime/BindingFlagSupport/MemberPolicies.cs b/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Runtime/BindingFlagSupport/MemberPolicies.cs index e8c5ca8efee76..db486a10486e6 100644 --- a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Runtime/BindingFlagSupport/MemberPolicies.cs +++ b/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Runtime/BindingFlagSupport/MemberPolicies.cs @@ -25,9 +25,9 @@ public MemberPolicies(int index) //================================================================================================================= // - // Returns all of the directly declared members on the given TypeInfo. + // Returns all of the directly declared members on the given Type. // - public abstract IEnumerable GetDeclaredMembers(TypeInfo typeInfo); + public abstract IEnumerable GetDeclaredMembers(Type type); // // Returns all of the directly declared members on the given TypeInfo whose name matches optionalNameFilter. If optionalNameFilter is null, @@ -189,6 +189,8 @@ private static bool GenericMethodAwareAreParameterTypesEqual(Type t1, Type t2) return false; } + protected const BindingFlags DeclaredOnlyLookup = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static | BindingFlags.DeclaredOnly; + // // This returns a fixed value from 0 to MemberIndex.Count-1 with each possible type of M // being assigned a unique index (see the MemberTypeIndex for possible values). This is useful diff --git a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Runtime/BindingFlagSupport/MethodPolicies.cs b/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Runtime/BindingFlagSupport/MethodPolicies.cs index b56f748c72c2e..ccb2883c061cc 100644 --- a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Runtime/BindingFlagSupport/MethodPolicies.cs +++ b/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Runtime/BindingFlagSupport/MethodPolicies.cs @@ -19,9 +19,9 @@ public MethodPolicies() : base(MemberTypeIndex.Method) { } [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2070:UnrecognizedReflectionPattern", Justification = "Reflection implementation")] - public sealed override IEnumerable GetDeclaredMembers(TypeInfo typeInfo) + public sealed override IEnumerable GetDeclaredMembers(Type type) { - return typeInfo.DeclaredMethods; + return type.GetMethods(DeclaredOnlyLookup); } public sealed override IEnumerable CoreGetDeclaredMembers(RuntimeTypeInfo type, NameFilter? optionalNameFilter, RuntimeTypeInfo reflectedType) diff --git a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Runtime/BindingFlagSupport/NestedTypePolicies.cs b/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Runtime/BindingFlagSupport/NestedTypePolicies.cs index 495ad6a93a851..a6b8e54793e16 100644 --- a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Runtime/BindingFlagSupport/NestedTypePolicies.cs +++ b/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Runtime/BindingFlagSupport/NestedTypePolicies.cs @@ -29,9 +29,9 @@ public NestedTypePolicies() : base(MemberTypeIndex.NestedType) { } [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2070:UnrecognizedReflectionPattern", Justification = "Reflection implementation")] - public sealed override IEnumerable GetDeclaredMembers(TypeInfo typeInfo) + public sealed override IEnumerable GetDeclaredMembers(Type type) { - return typeInfo.DeclaredNestedTypes; + return type.GetNestedTypes(DeclaredOnlyLookup); } public sealed override IEnumerable CoreGetDeclaredMembers(RuntimeTypeInfo type, NameFilter? optionalNameFilter, RuntimeTypeInfo reflectedType) diff --git a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Runtime/BindingFlagSupport/PropertyPolicies.cs b/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Runtime/BindingFlagSupport/PropertyPolicies.cs index c7eac9bb009b1..55d1892ec624a 100644 --- a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Runtime/BindingFlagSupport/PropertyPolicies.cs +++ b/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Runtime/BindingFlagSupport/PropertyPolicies.cs @@ -19,9 +19,9 @@ public PropertyPolicies() : base(MemberTypeIndex.Property) { } [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2070:UnrecognizedReflectionPattern", Justification = "Reflection implementation")] - public sealed override IEnumerable GetDeclaredMembers(TypeInfo typeInfo) + public sealed override IEnumerable GetDeclaredMembers(Type type) { - return typeInfo.DeclaredProperties; + return type.GetProperties(DeclaredOnlyLookup); } public sealed override IEnumerable CoreGetDeclaredMembers(RuntimeTypeInfo type, NameFilter? optionalNameFilter, RuntimeTypeInfo reflectedType) diff --git a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Runtime/BindingFlagSupport/Shared.cs b/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Runtime/BindingFlagSupport/Shared.cs index 27ad6e281d48a..ae8598c75e9f7 100644 --- a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Runtime/BindingFlagSupport/Shared.cs +++ b/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Runtime/BindingFlagSupport/Shared.cs @@ -156,16 +156,16 @@ public static M GetImplicitlyOverriddenBaseClassMember(this M member, MemberP return null; } string name = member.Name; - TypeInfo typeInfo = member.DeclaringType.GetTypeInfo(); + Type type = member.DeclaringType!; for (;;) { - Type? baseType = typeInfo.BaseType; + Type? baseType = type.BaseType; if (baseType == null) { return null; } - typeInfo = baseType.GetTypeInfo(); - foreach (M candidate in policies.GetDeclaredMembers(typeInfo)) + type = baseType; + foreach (M candidate in policies.GetDeclaredMembers(type)) { if (candidate.Name != name) { diff --git a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Runtime/General/TypeResolver.NativeFormat.cs b/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Runtime/General/TypeResolver.NativeFormat.cs index 1979b411cabe7..e6b89326e13cd 100644 --- a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Runtime/General/TypeResolver.NativeFormat.cs +++ b/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Runtime/General/TypeResolver.NativeFormat.cs @@ -185,13 +185,13 @@ internal static RuntimeTypeInfo ResolveTypeDefinition(this TypeDefinitionHandle if (outerTypeInfo != null) { // It was a nested type. We've already resolved the containing type recursively - just find the nested among its direct children. - TypeInfo? resolvedTypeInfo = outerTypeInfo.GetDeclaredNestedType(name); - if (resolvedTypeInfo == null) + Type? resolvedType = outerTypeInfo.GetNestedType(name, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.DeclaredOnly); + if (resolvedType == null) { exception = Helpers.CreateTypeLoadException(outerTypeInfo.FullName + "+" + name, outerTypeInfo.Assembly); return null; } - return resolvedTypeInfo.CastToRuntimeTypeInfo(); + return resolvedType.CastToRuntimeTypeInfo(); } diff --git a/src/coreclr/nativeaot/System.Private.Reflection.Execution/src/Internal/Reflection/Execution/ExecutionEnvironmentImplementation.MappingTables.cs b/src/coreclr/nativeaot/System.Private.Reflection.Execution/src/Internal/Reflection/Execution/ExecutionEnvironmentImplementation.MappingTables.cs index 0944c4379352c..23d5a27efb625 100644 --- a/src/coreclr/nativeaot/System.Private.Reflection.Execution/src/Internal/Reflection/Execution/ExecutionEnvironmentImplementation.MappingTables.cs +++ b/src/coreclr/nativeaot/System.Private.Reflection.Execution/src/Internal/Reflection/Execution/ExecutionEnvironmentImplementation.MappingTables.cs @@ -333,9 +333,9 @@ public sealed override unsafe bool TryGetConstructedGenericTypeForComponents(Run return true; } - TypeInfo typeDefinition = Type.GetTypeFromHandle(genericTypeDefinitionHandle).GetTypeInfo(); + Type typeDefinition = Type.GetTypeFromHandle(genericTypeDefinitionHandle); - TypeInfo[] typeArguments = new TypeInfo[genericTypeArgumentHandles.Length]; + Type[] typeArguments = new Type[genericTypeArgumentHandles.Length]; for (int i = 0; i < genericTypeArgumentHandles.Length; i++) { // Early out if one of the arguments is a generic definition. @@ -345,7 +345,7 @@ public sealed override unsafe bool TryGetConstructedGenericTypeForComponents(Run if (RuntimeAugments.IsGenericTypeDefinition(genericTypeArgumentHandles[i])) return false; - typeArguments[i] = Type.GetTypeFromHandle(genericTypeArgumentHandles[i]).GetTypeInfo(); + typeArguments[i] = Type.GetTypeFromHandle(genericTypeArgumentHandles[i]); } ConstraintValidator.EnsureSatisfiesClassConstraints(typeDefinition, typeArguments); @@ -1144,7 +1144,7 @@ public LowLevelList ParameterTypeHandles if (parameterType.IsByRef) result.Add(parameterType.GetElementType().TypeHandle); - else if (parameterType.GetTypeInfo().IsEnum && !parameters[i].HasDefaultValue) + else if (parameterType.IsEnum && !parameters[i].HasDefaultValue) result.Add(Enum.GetUnderlyingType(parameterType).TypeHandle); else result.Add(parameterType.TypeHandle); diff --git a/src/coreclr/nativeaot/System.Private.Reflection.Execution/src/Internal/Reflection/Execution/ReflectionDomainSetupImplementation.cs b/src/coreclr/nativeaot/System.Private.Reflection.Execution/src/Internal/Reflection/Execution/ReflectionDomainSetupImplementation.cs index 73506680de2b4..ce54d2fea2f6e 100644 --- a/src/coreclr/nativeaot/System.Private.Reflection.Execution/src/Internal/Reflection/Execution/ReflectionDomainSetupImplementation.cs +++ b/src/coreclr/nativeaot/System.Private.Reflection.Execution/src/Internal/Reflection/Execution/ReflectionDomainSetupImplementation.cs @@ -35,8 +35,8 @@ public sealed override Exception CreateNonInvokabilityException(MemberInfo perta resourceName = methodBase.IsConstructedGenericMethod ? SR.MakeGenericMethod_NoMetadata : SR.Object_NotInvokable; if (methodBase is ConstructorInfo) { - TypeInfo declaringTypeInfo = methodBase.DeclaringType.GetTypeInfo(); - if (typeof(Delegate).GetTypeInfo().IsAssignableFrom(declaringTypeInfo)) + Type declaringType = methodBase.DeclaringType; + if (typeof(Delegate).IsAssignableFrom(declaringType)) throw new PlatformNotSupportedException(SR.PlatformNotSupported_CannotInvokeDelegateCtor); } }