Skip to content

Commit

Permalink
Remove uses of TypeInfo from the reflection stack (#80833)
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
MichalStrehovsky committed Jan 19, 2023
1 parent 170587e commit 88ab5ba
Show file tree
Hide file tree
Showing 11 changed files with 29 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ public ConstructorPolicies() : base(MemberTypeIndex.Constructor) { }

[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2070:UnrecognizedReflectionPattern",
Justification = "Reflection implementation")]
public sealed override IEnumerable<ConstructorInfo> GetDeclaredMembers(TypeInfo typeInfo)
public sealed override IEnumerable<ConstructorInfo> GetDeclaredMembers(Type type)
{
return typeInfo.DeclaredConstructors;
return type.GetConstructors(DeclaredOnlyLookup);
}

public sealed override IEnumerable<ConstructorInfo> CoreGetDeclaredMembers(RuntimeTypeInfo type, NameFilter? optionalNameFilter, RuntimeTypeInfo reflectedType)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ public EventPolicies() : base(MemberTypeIndex.Event) { }

[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2070:UnrecognizedReflectionPattern",
Justification = "Reflection implementation")]
public sealed override IEnumerable<EventInfo> GetDeclaredMembers(TypeInfo typeInfo)
public sealed override IEnumerable<EventInfo> GetDeclaredMembers(Type type)
{
return typeInfo.DeclaredEvents;
return type.GetEvents(DeclaredOnlyLookup);
}

public sealed override IEnumerable<EventInfo> CoreGetDeclaredMembers(RuntimeTypeInfo type, NameFilter? optionalNameFilter, RuntimeTypeInfo reflectedType)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ public FieldPolicies() : base(MemberTypeIndex.Field) { }

[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2070:UnrecognizedReflectionPattern",
Justification = "Reflection implementation")]
public sealed override IEnumerable<FieldInfo> GetDeclaredMembers(TypeInfo typeInfo)
public sealed override IEnumerable<FieldInfo> GetDeclaredMembers(Type type)
{
return typeInfo.DeclaredFields;
return type.GetFields(DeclaredOnlyLookup);
}

public sealed override IEnumerable<FieldInfo> CoreGetDeclaredMembers(RuntimeTypeInfo type, NameFilter? optionalNameFilter, RuntimeTypeInfo reflectedType)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<M> GetDeclaredMembers(TypeInfo typeInfo);
public abstract IEnumerable<M> GetDeclaredMembers(Type type);

//
// Returns all of the directly declared members on the given TypeInfo whose name matches optionalNameFilter. If optionalNameFilter is null,
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ public MethodPolicies() : base(MemberTypeIndex.Method) { }

[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2070:UnrecognizedReflectionPattern",
Justification = "Reflection implementation")]
public sealed override IEnumerable<MethodInfo> GetDeclaredMembers(TypeInfo typeInfo)
public sealed override IEnumerable<MethodInfo> GetDeclaredMembers(Type type)
{
return typeInfo.DeclaredMethods;
return type.GetMethods(DeclaredOnlyLookup);
}

public sealed override IEnumerable<MethodInfo> CoreGetDeclaredMembers(RuntimeTypeInfo type, NameFilter? optionalNameFilter, RuntimeTypeInfo reflectedType)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ public NestedTypePolicies() : base(MemberTypeIndex.NestedType) { }

[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2070:UnrecognizedReflectionPattern",
Justification = "Reflection implementation")]
public sealed override IEnumerable<Type> GetDeclaredMembers(TypeInfo typeInfo)
public sealed override IEnumerable<Type> GetDeclaredMembers(Type type)
{
return typeInfo.DeclaredNestedTypes;
return type.GetNestedTypes(DeclaredOnlyLookup);
}

public sealed override IEnumerable<Type> CoreGetDeclaredMembers(RuntimeTypeInfo type, NameFilter? optionalNameFilter, RuntimeTypeInfo reflectedType)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ public PropertyPolicies() : base(MemberTypeIndex.Property) { }

[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2070:UnrecognizedReflectionPattern",
Justification = "Reflection implementation")]
public sealed override IEnumerable<PropertyInfo> GetDeclaredMembers(TypeInfo typeInfo)
public sealed override IEnumerable<PropertyInfo> GetDeclaredMembers(Type type)
{
return typeInfo.DeclaredProperties;
return type.GetProperties(DeclaredOnlyLookup);
}

public sealed override IEnumerable<PropertyInfo> CoreGetDeclaredMembers(RuntimeTypeInfo type, NameFilter? optionalNameFilter, RuntimeTypeInfo reflectedType)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,16 +156,16 @@ public static M GetImplicitlyOverriddenBaseClassMember<M>(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)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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);
Expand Down Expand Up @@ -1144,7 +1144,7 @@ public LowLevelList<RuntimeTypeHandle> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down

0 comments on commit 88ab5ba

Please sign in to comment.