diff --git a/src/libraries/System.Reflection.MetadataLoadContext/src/System/Reflection/DefaultBinder.cs b/src/libraries/System.Reflection.MetadataLoadContext/src/System/Reflection/DefaultBinder.cs index c45b91feb699ee..9c64b84b0f8c45 100644 --- a/src/libraries/System.Reflection.MetadataLoadContext/src/System/Reflection/DefaultBinder.cs +++ b/src/libraries/System.Reflection.MetadataLoadContext/src/System/Reflection/DefaultBinder.cs @@ -53,7 +53,7 @@ public sealed override MethodBase BindToMethod( for (i = 0; i < types.Length; i++) { realTypes[i] = types[i].UnderlyingSystemType; - if (!(IsImplementedByMetadataLoadContext(realTypes[i]) || realTypes[i].IsSignatureType())) + if (!(IsImplementedByMetadataLoadContext(realTypes[i]) || realTypes[i].IsSignatureType)) throw new ArgumentException(SR.Arg_MustBeType, nameof(types)); } types = realTypes; @@ -81,7 +81,7 @@ public sealed override MethodBase BindToMethod( continue; Type? type = types[j]; - if (type.IsSignatureType()) + if (type.IsSignatureType) { if (!(candidates[i] is MethodInfo methodInfo)) break; @@ -427,7 +427,7 @@ private static int FindMostSpecificType(Type c1, Type c2, Type t) if (c1 == c2) return 0; - if (t.IsSignatureType()) + if (t.IsSignatureType) { if (t.MatchesExactly(c1)) return 1; diff --git a/src/libraries/System.Reflection.MetadataLoadContext/src/System/Reflection/Runtime/BindingFlagSupport/MemberPolicies.cs b/src/libraries/System.Reflection.MetadataLoadContext/src/System/Reflection/Runtime/BindingFlagSupport/MemberPolicies.cs index a8c3fea5be7a6e..a139edafcb721f 100644 --- a/src/libraries/System.Reflection.MetadataLoadContext/src/System/Reflection/Runtime/BindingFlagSupport/MemberPolicies.cs +++ b/src/libraries/System.Reflection.MetadataLoadContext/src/System/Reflection/Runtime/BindingFlagSupport/MemberPolicies.cs @@ -140,7 +140,7 @@ private static bool GenericMethodAwareAreParameterTypesEqual(Type t1, Type t2) if ((t1.IsArray && t2.IsArray) || (t1.IsByRef && t2.IsByRef) || (t1.IsPointer && t2.IsPointer)) { - if (t1.IsSZArray() != t2.IsSZArray()) + if (t1.IsSZArray != t2.IsSZArray) return false; if (t1.IsArray && (t1.GetArrayRank() != t2.GetArrayRank())) @@ -169,7 +169,7 @@ private static bool GenericMethodAwareAreParameterTypesEqual(Type t1, Type t2) return true; } - if (t1.IsGenericMethodParameter() && t2.IsGenericMethodParameter()) + if (t1.IsGenericMethodParameter && t2.IsGenericMethodParameter) { // A generic method parameter. The DeclaringMethods will be different but we don't care about that - we can assume that // the declaring method will be the method that declared the parameter's whose type we're testing. We only need to diff --git a/src/libraries/System.Reflection.MetadataLoadContext/src/System/Reflection/Runtime/SignatureTypeExtensions.cs b/src/libraries/System.Reflection.MetadataLoadContext/src/System/Reflection/Runtime/SignatureTypeExtensions.cs index d9ab8d8060b501..8ad7e7ff95400d 100644 --- a/src/libraries/System.Reflection.MetadataLoadContext/src/System/Reflection/Runtime/SignatureTypeExtensions.cs +++ b/src/libraries/System.Reflection.MetadataLoadContext/src/System/Reflection/Runtime/SignatureTypeExtensions.cs @@ -17,7 +17,7 @@ internal static class SignatureTypeExtensions /// public static bool MatchesParameterTypeExactly(this Type pattern, ParameterInfo parameter) { - if (pattern.IsSignatureType()) + if (pattern.IsSignatureType) return pattern.MatchesExactly(parameter.ParameterType); else return pattern == (object)(parameter.ParameterType); @@ -32,15 +32,15 @@ public static bool MatchesParameterTypeExactly(this Type pattern, ParameterInfo /// internal static bool MatchesExactly(this Type pattern, Type actual) { - Debug.Assert(pattern.IsSignatureType()); + Debug.Assert(pattern.IsSignatureType); - if (pattern.IsSZArray()) + if (pattern.IsSZArray) { - return actual.IsSZArray() && pattern.GetElementType()!.MatchesExactly(actual.GetElementType()!); + return actual.IsSZArray && pattern.GetElementType()!.MatchesExactly(actual.GetElementType()!); } - else if (pattern.IsVariableBoundArray()) + else if (pattern.IsVariableBoundArray) { - return actual.IsVariableBoundArray() && pattern.GetArrayRank() == actual.GetArrayRank() && pattern.GetElementType()!.MatchesExactly(actual.GetElementType()!); + return actual.IsVariableBoundArray && pattern.GetArrayRank() == actual.GetArrayRank() && pattern.GetElementType()!.MatchesExactly(actual.GetElementType()!); } else if (pattern.IsByRef) { @@ -64,7 +64,7 @@ internal static bool MatchesExactly(this Type pattern, Type actual) for (int i = 0; i < count; i++) { Type patternGenericTypeArgument = patternGenericTypeArguments[i]; - if (patternGenericTypeArgument.IsSignatureType()) + if (patternGenericTypeArgument.IsSignatureType) { if (!patternGenericTypeArgument.MatchesExactly(actualGenericTypeArguments[i])) return false; @@ -77,9 +77,9 @@ internal static bool MatchesExactly(this Type pattern, Type actual) } return true; } - else if (pattern.IsGenericMethodParameter()) + else if (pattern.IsGenericMethodParameter) { - if (!actual.IsGenericMethodParameter()) + if (!actual.IsGenericMethodParameter) return false; if (pattern.GenericParameterPosition != actual.GenericParameterPosition) return false; @@ -104,19 +104,19 @@ internal static bool MatchesExactly(this Type pattern, Type actual) /// internal static Type? TryResolveAgainstGenericMethod(this Type signatureType, MethodInfo genericMethod) { - Debug.Assert(signatureType.IsSignatureType()); + Debug.Assert(signatureType.IsSignatureType); return signatureType.TryResolve(genericMethod.GetGenericArguments()); } private static Type? TryResolve(this Type signatureType, Type[] genericMethodParameters) { - Debug.Assert(signatureType.IsSignatureType()); + Debug.Assert(signatureType.IsSignatureType); - if (signatureType.IsSZArray()) + if (signatureType.IsSZArray) { return signatureType.GetElementType()!.TryResolve(genericMethodParameters)?.TryMakeArrayType(); } - else if (signatureType.IsVariableBoundArray()) + else if (signatureType.IsVariableBoundArray) { return signatureType.GetElementType()!.TryResolve(genericMethodParameters)?.TryMakeArrayType(signatureType.GetArrayRank()); } @@ -136,7 +136,7 @@ internal static bool MatchesExactly(this Type pattern, Type actual) for (int i = 0; i < count; i++) { Type genericTypeArgument = genericTypeArguments[i]; - if (genericTypeArgument.IsSignatureType()) + if (genericTypeArgument.IsSignatureType) { newGenericTypeArguments[i] = genericTypeArgument.TryResolve(genericMethodParameters); if (newGenericTypeArguments[i] == null) @@ -149,7 +149,7 @@ internal static bool MatchesExactly(this Type pattern, Type actual) } return signatureType.GetGenericTypeDefinition().TryMakeGenericType(newGenericTypeArguments!); } - else if (signatureType.IsGenericMethodParameter()) + else if (signatureType.IsGenericMethodParameter) { int position = signatureType.GenericParameterPosition; if (position >= genericMethodParameters.Length) diff --git a/src/libraries/System.Reflection.MetadataLoadContext/src/System/Reflection/TypeLoading/General/Assignability.cs b/src/libraries/System.Reflection.MetadataLoadContext/src/System/Reflection/TypeLoading/General/Assignability.cs index c010322090339d..d6c2a1ada55889 100644 --- a/src/libraries/System.Reflection.MetadataLoadContext/src/System/Reflection/TypeLoading/General/Assignability.cs +++ b/src/libraries/System.Reflection.MetadataLoadContext/src/System/Reflection/TypeLoading/General/Assignability.cs @@ -72,8 +72,8 @@ private static bool CanCastTo(this Type fromTypeInfo, Type toTypeInfo, CoreTypes if (rank != toTypeInfo.GetArrayRank()) return false; - bool fromTypeIsSzArray = fromTypeInfo.IsSZArray(); - bool toTypeIsSzArray = toTypeInfo.IsSZArray(); + bool fromTypeIsSzArray = fromTypeInfo.IsSZArray; + bool toTypeIsSzArray = toTypeInfo.IsSZArray; if (fromTypeIsSzArray != toTypeIsSzArray) { // T[] is assignable to T[*] but not vice-versa. diff --git a/src/libraries/System.Reflection.MetadataLoadContext/src/System/Reflection/TypeLoading/General/TypeExtensions.net.cs b/src/libraries/System.Reflection.MetadataLoadContext/src/System/Reflection/TypeLoading/General/TypeExtensions.net.cs index 6ddb2717b1b9cc..3acc0bac5714c9 100644 --- a/src/libraries/System.Reflection.MetadataLoadContext/src/System/Reflection/TypeLoading/General/TypeExtensions.net.cs +++ b/src/libraries/System.Reflection.MetadataLoadContext/src/System/Reflection/TypeLoading/General/TypeExtensions.net.cs @@ -5,17 +5,6 @@ namespace System.Reflection.TypeLoading { - internal static class NetCoreApiEmulators - { - // On NetCore, call the real thing. - - public static bool IsSignatureType(this Type type) => type.IsSignatureType; - public static bool IsSZArray(this Type type) => type.IsSZArray; - public static bool IsVariableBoundArray(this Type type) => type.IsVariableBoundArray; - public static bool IsGenericMethodParameter(this Type type) => type.IsGenericMethodParameter; - public static Type MakeSignatureGenericType(this Type genericTypeDefinition, Type[] typeArguments) => Type.MakeGenericSignatureType(genericTypeDefinition, typeArguments); - } - /// /// Another layer of base types. Empty for NetCore. /// diff --git a/src/libraries/System.Reflection.MetadataLoadContext/src/System/Reflection/TypeLoading/General/TypeExtensions.netstandard.cs b/src/libraries/System.Reflection.MetadataLoadContext/src/System/Reflection/TypeLoading/General/TypeExtensions.netstandard.cs index d838f7762ba98f..aa5df70f8b2976 100644 --- a/src/libraries/System.Reflection.MetadataLoadContext/src/System/Reflection/TypeLoading/General/TypeExtensions.netstandard.cs +++ b/src/libraries/System.Reflection.MetadataLoadContext/src/System/Reflection/TypeLoading/General/TypeExtensions.netstandard.cs @@ -5,18 +5,28 @@ namespace System.Reflection.TypeLoading { - // For code that have to interact with "Type" rather than "RoType", some handy extension methods that "add" the NetCore reflection apis to NetStandard. + // For code that has to interact with "Type" rather than "RoType", some handy extensions that "add" the NetCore reflection APIs to NetStandard. internal static class NetCoreApiEmulators { - // On NetStandard, have to do with slower emulations. #pragma warning disable IDE0060 - public static bool IsSignatureType(this Type type) => false; - public static bool IsSZArray(this Type type) => type.IsArray && type.GetArrayRank() == 1 && type.Name.EndsWith("[]", StringComparison.Ordinal); - public static bool IsVariableBoundArray(this Type type) => type.IsArray && !type.IsSZArray(); - public static bool IsGenericMethodParameter(this Type type) => type.IsGenericParameter && type.DeclaringMethod != null; + // On NetStandard, have to do with slower emulations. + extension(Type type) + { +#pragma warning disable CA1822 // Mark members as static + public bool IsSignatureType => false; +#pragma warning restore CA1822 // Mark members as static + + public bool IsSZArray => type.IsArray && type.GetArrayRank() == 1 && type.Name.EndsWith("[]", StringComparison.Ordinal); + + public bool IsVariableBoundArray => type.IsArray && !type.IsSZArray; - // Signature Types do not exist on NetStandard 2.0 but it's possible we could reach this if a NetCore app uses the NetStandard build of this library. - public static Type MakeSignatureGenericType(this Type genericTypeDefinition, Type[] typeArguments) => throw new NotSupportedException(SR.NotSupported_MakeGenericType_SignatureTypes); + public bool IsGenericMethodParameter => type.IsGenericParameter && type.DeclaringMethod != null; + } + extension(Type) + { + // Signature Types do not exist on NetStandard 2.0 but it's possible we could reach this if a NetCore app uses the NetStandard build of this library. + public static Type MakeGenericSignatureType(Type genericTypeDefinition, params Type[] typeArguments) => throw new NotSupportedException(SR.NotSupported_MakeGenericType_SignatureTypes); + } #pragma warning restore IDE0060 } diff --git a/src/libraries/System.Reflection.MetadataLoadContext/src/System/Reflection/TypeLoading/Types/RoDefinitionType.cs b/src/libraries/System.Reflection.MetadataLoadContext/src/System/Reflection/TypeLoading/Types/RoDefinitionType.cs index 66a241e4eda1e2..0412e477e66dfe 100644 --- a/src/libraries/System.Reflection.MetadataLoadContext/src/System/Reflection/TypeLoading/Types/RoDefinitionType.cs +++ b/src/libraries/System.Reflection.MetadataLoadContext/src/System/Reflection/TypeLoading/Types/RoDefinitionType.cs @@ -105,7 +105,7 @@ public sealed override Type MakeGenericType(params Type[] typeArguments) Type typeArgument = typeArguments[i]; if (typeArgument == null) throw new ArgumentNullException(); - if (typeArgument.IsSignatureType()) + if (typeArgument.IsSignatureType) { foundSigType = true; } @@ -117,7 +117,7 @@ public sealed override Type MakeGenericType(params Type[] typeArguments) } } if (foundSigType) - return this.MakeSignatureGenericType(typeArguments); + return Type.MakeGenericSignatureType(this, typeArguments); // We are intentionally not validating constraints as constraint validation is an execution-time issue that does not block our // library and should not block a metadata inspection tool. diff --git a/src/libraries/System.Reflection.MetadataLoadContext/src/System/Reflection/TypeLoading/Types/RoModifiedType.cs b/src/libraries/System.Reflection.MetadataLoadContext/src/System/Reflection/TypeLoading/Types/RoModifiedType.cs index 64b3789a4566e4..1ec881fd27a9a3 100644 --- a/src/libraries/System.Reflection.MetadataLoadContext/src/System/Reflection/TypeLoading/Types/RoModifiedType.cs +++ b/src/libraries/System.Reflection.MetadataLoadContext/src/System/Reflection/TypeLoading/Types/RoModifiedType.cs @@ -124,7 +124,7 @@ public override Type[] GetOptionalCustomModifiers() public override bool IsGenericTypeDefinition => _unmodifiedType.IsGenericTypeDefinition; protected override bool HasElementTypeImpl() => _unmodifiedType.Call_HasElementTypeImpl(); protected override bool IsArrayImpl() => _unmodifiedType.Call_IsArrayImpl(); - public override bool IsSZArray => _unmodifiedType.IsSZArray(); + public override bool IsSZArray => _unmodifiedType.IsSZArray; public override bool IsVariableBoundArray => _unmodifiedType.IsVariableBoundArray; protected override bool IsByRefImpl() => _unmodifiedType.Call_IsByRefImpl(); protected override bool IsPointerImpl() => _unmodifiedType.Call_IsPointerImpl();