diff options
Diffstat (limited to 'src/System.Private.Reflection.Core')
19 files changed, 150 insertions, 319 deletions
diff --git a/src/System.Private.Reflection.Core/src/Internal/Reflection/Core/Execution/ExecutionDomain.cs b/src/System.Private.Reflection.Core/src/Internal/Reflection/Core/Execution/ExecutionDomain.cs index a1c58ccb6..bd3d6d299 100644 --- a/src/System.Private.Reflection.Core/src/Internal/Reflection/Core/Execution/ExecutionDomain.cs +++ b/src/System.Private.Reflection.Core/src/Internal/Reflection/Core/Execution/ExecutionDomain.cs @@ -124,7 +124,9 @@ namespace Internal.Reflection.Core.Execution } else { - RuntimeNamedMethodInfo runtimeNamedMethodInfo = RuntimeNamedMethodInfo.GetRuntimeNamedMethodInfo(methodHandle, definingTypeInfo, contextTypeInfo); + // RuntimeMethodHandles always yield methods whose ReflectedType is the DeclaringType. + RuntimeTypeInfo reflectedType = contextTypeInfo; + RuntimeNamedMethodInfo runtimeNamedMethodInfo = RuntimeNamedMethodInfo.GetRuntimeNamedMethodInfo(methodHandle, definingTypeInfo, contextTypeInfo, reflectedType); if (!runtimeNamedMethodInfo.IsGenericMethod) { return runtimeNamedMethodInfo; diff --git a/src/System.Private.Reflection.Core/src/System.Private.Reflection.Core.csproj b/src/System.Private.Reflection.Core/src/System.Private.Reflection.Core.csproj index 1be0bf056..29e0976ad 100644 --- a/src/System.Private.Reflection.Core/src/System.Private.Reflection.Core.csproj +++ b/src/System.Private.Reflection.Core/src/System.Private.Reflection.Core.csproj @@ -46,7 +46,6 @@ <Compile Include="System\Reflection\Runtime\Assemblies\RuntimeAssemblyName.cs" /> <Compile Include="System\Reflection\Runtime\BindingFlagSupport\DefaultBinder.cs" /> <Compile Include="System\Reflection\Runtime\BindingFlagSupport\DefaultBinder.LimitedBinder.cs" /> - <Compile Include="System\Reflection\Runtime\BindingFlagSupport\InheritedPropertyInfo.cs" /> <Compile Include="System\Reflection\Runtime\BindingFlagSupport\MemberEnumerator.cs" /> <Compile Include="System\Reflection\Runtime\BindingFlagSupport\MemberPolicies.cs" /> <Compile Include="System\Reflection\Runtime\BindingFlagSupport\MemberTypeIndex.cs" /> diff --git a/src/System.Private.Reflection.Core/src/System/Reflection/Runtime/BindingFlagSupport/InheritedPropertyInfo.cs b/src/System.Private.Reflection.Core/src/System/Reflection/Runtime/BindingFlagSupport/InheritedPropertyInfo.cs deleted file mode 100644 index c0022ec46..000000000 --- a/src/System.Private.Reflection.Core/src/System/Reflection/Runtime/BindingFlagSupport/InheritedPropertyInfo.cs +++ /dev/null @@ -1,260 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -using System; -using System.Reflection; -using System.Diagnostics; -using System.Globalization; -using System.Collections.Generic; - -namespace System.Reflection.Runtime.BindingFlagSupport -{ - // - // This class exists for desktop compatibility. If one uses an api such as Type.GetProperty(string) to retrieve a member - // from a base class, the desktop returns a special MemberInfo object that is blocked from seeing or invoking private - // set or get methods on that property. That is, the type used to find the member is part of that member's object identity. - // - internal sealed class InheritedPropertyInfo : PropertyInfo - { - private readonly PropertyInfo _underlyingPropertyInfo; - private readonly Type _reflectedType; - - internal InheritedPropertyInfo(PropertyInfo underlyingPropertyInfo, Type reflectedType) - { - // If the reflectedType is the declaring type, the caller should have used the original PropertyInfo. - // This assert saves us from having to check this throughout. - Debug.Assert(!(reflectedType.Equals(underlyingPropertyInfo.DeclaringType)), "reflectedType must be a proper base type of (and not equal to) underlyingPropertyInfo.DeclaringType."); - - _underlyingPropertyInfo = underlyingPropertyInfo; - _reflectedType = reflectedType; - return; - } - - public sealed override PropertyAttributes Attributes - { - get { return _underlyingPropertyInfo.Attributes; } - } - - public sealed override bool CanRead - { - get { return GetMethod != null; } - } - - public sealed override bool CanWrite - { - get { return SetMethod != null; } - } - - public sealed override ParameterInfo[] GetIndexParameters() - { - return _underlyingPropertyInfo.GetIndexParameters(); - } - - public sealed override Type PropertyType - { - get { return _underlyingPropertyInfo.PropertyType; } - } - - public sealed override Type DeclaringType - { - get { return _underlyingPropertyInfo.DeclaringType; } - } - - public sealed override String Name - { - get { return _underlyingPropertyInfo.Name; } - } - - public sealed override IEnumerable<CustomAttributeData> CustomAttributes - { - get { return _underlyingPropertyInfo.CustomAttributes; } - } - - public sealed override bool Equals(Object obj) - { - InheritedPropertyInfo other = obj as InheritedPropertyInfo; - if (other == null) - { - return false; - } - - if (!(_underlyingPropertyInfo.Equals(other._underlyingPropertyInfo))) - { - return false; - } - - if (!(_reflectedType.Equals(other._reflectedType))) - { - return false; - } - - return true; - } - - public sealed override int GetHashCode() - { - int hashCode = _reflectedType.GetHashCode(); - hashCode ^= _underlyingPropertyInfo.GetHashCode(); - return hashCode; - } - - public sealed override Object GetConstantValue() - { - return _underlyingPropertyInfo.GetConstantValue(); - } - - public sealed override MethodInfo GetMethod - { - get - { - MethodInfo accessor = _underlyingPropertyInfo.GetMethod; - return Filter(accessor); - } - } - - public sealed override object GetValue(object obj, BindingFlags invokeAttr, Binder binder, object[] index, CultureInfo culture) - { - if (GetMethod == null) - { - throw new ArgumentException(SR.Arg_GetMethNotFnd); - } - - return _underlyingPropertyInfo.GetValue(obj, invokeAttr, binder, index, culture); - } - - public sealed override Module Module - { - get { return _underlyingPropertyInfo.Module; } - } - - public sealed override String ToString() - { - return _underlyingPropertyInfo.ToString(); - } - - public sealed override MethodInfo SetMethod - { - get - { - MethodInfo accessor = _underlyingPropertyInfo.SetMethod; - return Filter(accessor); - } - } - - public sealed override void SetValue(object obj, object value, BindingFlags invokeAttr, Binder binder, object[] index, CultureInfo culture) - { - if (SetMethod == null) - { - throw new ArgumentException(SR.Arg_SetMethNotFnd); - } - - _underlyingPropertyInfo.SetValue(obj, value, invokeAttr, binder, index, culture); - } - - public sealed override MemberTypes MemberType { get { return MemberTypes.Property; } } - - public sealed override MethodInfo[] GetAccessors(bool nonPublic) - { - MethodInfo[] accessors = _underlyingPropertyInfo.GetAccessors(nonPublic); - MethodInfo[] survivors = new MethodInfo[accessors.Length]; - int numSurvivors = 0; - for (int i = 0; i < accessors.Length; i++) - { - MethodInfo survivor = Filter(accessors[i]); - if (survivor != null) - { - survivors[numSurvivors++] = survivor; - } - } - Array.Resize(ref survivors, numSurvivors); - return survivors; - } - - public sealed override MethodInfo GetGetMethod(bool nonPublic) - { - MethodInfo accessor = _underlyingPropertyInfo.GetGetMethod(nonPublic); - return Filter(accessor); - } - - public sealed override MethodInfo GetSetMethod(bool nonPublic) - { - MethodInfo accessor = _underlyingPropertyInfo.GetSetMethod(nonPublic); - return Filter(accessor); - } - - public sealed override object[] GetCustomAttributes(bool inherit) - { - return _underlyingPropertyInfo.GetCustomAttributes(inherit); - } - - public sealed override object[] GetCustomAttributes(Type attributeType, bool inherit) - { - return _underlyingPropertyInfo.GetCustomAttributes(attributeType, inherit); - } - - public sealed override bool IsDefined(Type attributeType, bool inherit) - { - return _underlyingPropertyInfo.IsDefined(attributeType, inherit); - } - - public sealed override Type ReflectedType - { - get { return _underlyingPropertyInfo.ReflectedType; } - } - - public sealed override IList<CustomAttributeData> GetCustomAttributesData() - { - return _underlyingPropertyInfo.GetCustomAttributesData(); - } - - public sealed override Type[] GetOptionalCustomModifiers() - { - return _underlyingPropertyInfo.GetOptionalCustomModifiers(); - } - - public sealed override object GetRawConstantValue() - { - return _underlyingPropertyInfo.GetRawConstantValue(); - } - - public sealed override Type[] GetRequiredCustomModifiers() - { - return _underlyingPropertyInfo.GetRequiredCustomModifiers(); - } - - public sealed override int MetadataToken - { - get { return _underlyingPropertyInfo.MetadataToken; } - } - -#if DEBUG - public sealed override object GetValue(object obj, object[] index) - { - return base.GetValue(obj, index); - } - - public sealed override void SetValue(object obj, object value, object[] index) - { - base.SetValue(obj, value, index); - } -#endif - - private MethodInfo Filter(MethodInfo accessor) - { - // - // For desktop compat, hide inherited accessors that are marked private. - // - // Q: Why don't we also hide cross-assembly "internal" accessors? - // A: That inconsistency is also desktop-compatible. - // - if (accessor == null || accessor.IsPrivate) - { - return null; - } - - return accessor; - } - } -} - diff --git a/src/System.Private.Reflection.Core/src/System/Reflection/Runtime/BindingFlagSupport/MemberPolicies.cs b/src/System.Private.Reflection.Core/src/System/Reflection/Runtime/BindingFlagSupport/MemberPolicies.cs index 95535cf2e..aafed62c3 100644 --- a/src/System.Private.Reflection.Core/src/System/Reflection/Runtime/BindingFlagSupport/MemberPolicies.cs +++ b/src/System.Private.Reflection.Core/src/System/Reflection/Runtime/BindingFlagSupport/MemberPolicies.cs @@ -65,15 +65,6 @@ namespace System.Reflection.Runtime.BindingFlagSupport public abstract bool IsSuppressedByMoreDerivedMember(M member, M[] priorMembers, int startIndex, int endIndex); // - // Policy to create a wrapper MemberInfo (if appropriate). This is due to the fact that MemberInfo's actually have their identity - // tied to the type they were queried off of and this unfortunate fact shows up in certain api behaviors. - // - public virtual M GetInheritedMemberInfo(M underlyingMemberInfo, Type reflectedType) - { - return underlyingMemberInfo; - } - - // // Helper method for determining whether two methods are signature-compatible for the purpose of implicit overriding. // protected static bool AreNamesAndSignaturesEqual(MethodInfo method1, MethodInfo method2) @@ -313,6 +304,19 @@ namespace System.Reflection.Runtime.BindingFlagSupport public sealed override void GetMemberAttributes(PropertyInfo member, out MethodAttributes visibility, out bool isStatic, out bool isVirtual, out bool isNewSlot) { MethodInfo accessorMethod = GetAccessorMethod(member); + if (accessorMethod == null) + { + // If we got here, this is a inherited PropertyInfo that only had private accessors and is now refusing to give them out + // because that's what the rules of inherited PropertyInfo's are. Such a PropertyInfo is also considered private and will never be + // given out of a Type.GetProperty() call. So all we have to do is set its visibility to Private and it will get filtered out. + // Other values need to be set to satisify C# but they are meaningless. + visibility = MethodAttributes.Private; + isStatic = false; + isVirtual = false; + isNewSlot = true; + return; + } + MethodAttributes methodAttributes = accessorMethod.Attributes; visibility = methodAttributes & MethodAttributes.MemberAccessMask; isStatic = (0 != (methodAttributes & MethodAttributes.Static)); @@ -346,11 +350,6 @@ namespace System.Reflection.Runtime.BindingFlagSupport return false; } - public sealed override PropertyInfo GetInheritedMemberInfo(PropertyInfo underlyingMemberInfo, Type reflectedType) - { - return new InheritedPropertyInfo(underlyingMemberInfo, reflectedType); - } - private MethodInfo GetAccessorMethod(PropertyInfo property) { MethodInfo accessor = property.GetMethod; diff --git a/src/System.Private.Reflection.Core/src/System/Reflection/Runtime/BindingFlagSupport/QueriedMemberList.cs b/src/System.Private.Reflection.Core/src/System/Reflection/Runtime/BindingFlagSupport/QueriedMemberList.cs index 7b04093b9..935facd0b 100644 --- a/src/System.Private.Reflection.Core/src/System/Reflection/Runtime/BindingFlagSupport/QueriedMemberList.cs +++ b/src/System.Private.Reflection.Core/src/System/Reflection/Runtime/BindingFlagSupport/QueriedMemberList.cs @@ -137,14 +137,7 @@ namespace System.Reflection.Runtime.BindingFlagSupport allFlagsThatMustMatch |= BindingFlags.FlattenHierarchy; allFlagsThatMustMatch |= ((visibility == MethodAttributes.Public) ? BindingFlags.Public : BindingFlags.NonPublic); - if (inBaseClass) - { - queriedMembers.Add(policies.GetInheritedMemberInfo(member, reflectedType), allFlagsThatMustMatch); - } - else - { - queriedMembers.Add(member, allFlagsThatMustMatch); - } + queriedMembers.Add(member, allFlagsThatMustMatch); } if (!inBaseClass) diff --git a/src/System.Private.Reflection.Core/src/System/Reflection/Runtime/Dispensers/DefaultDispenserPolicy.cs b/src/System.Private.Reflection.Core/src/System/Reflection/Runtime/Dispensers/DefaultDispenserPolicy.cs index bacda5a42..880c1b3af 100644 --- a/src/System.Private.Reflection.Core/src/System/Reflection/Runtime/Dispensers/DefaultDispenserPolicy.cs +++ b/src/System.Private.Reflection.Core/src/System/Reflection/Runtime/Dispensers/DefaultDispenserPolicy.cs @@ -14,9 +14,6 @@ namespace System.Reflection.Runtime.Dispensers { public sealed override DispenserAlgorithm GetAlgorithm(DispenserScenario scenario) { -#if TEST_CODEGEN_OPTIMIZATION - return DispenserAlgorithm.CreateAlways; -#else switch (scenario) { // Assembly + NamespaceTypeName to Type @@ -38,8 +35,6 @@ namespace System.Reflection.Runtime.Dispensers default: return DispenserAlgorithm.CreateAlways; } -#endif //!TEST_CODEGEN_OPTIMIZATION - } } } diff --git a/src/System.Private.Reflection.Core/src/System/Reflection/Runtime/EventInfos/RuntimeEventInfo.cs b/src/System.Private.Reflection.Core/src/System/Reflection/Runtime/EventInfos/RuntimeEventInfo.cs index 82b3facf4..1b17da567 100644 --- a/src/System.Private.Reflection.Core/src/System/Reflection/Runtime/EventInfos/RuntimeEventInfo.cs +++ b/src/System.Private.Reflection.Core/src/System/Reflection/Runtime/EventInfos/RuntimeEventInfo.cs @@ -45,11 +45,12 @@ namespace System.Reflection.Runtime.EventInfos // // We don't report any DeclaredMembers for arrays or generic parameters so those don't apply. // - private RuntimeEventInfo(EventHandle eventHandle, RuntimeNamedTypeInfo definingTypeInfo, RuntimeTypeInfo contextTypeInfo) + private RuntimeEventInfo(EventHandle eventHandle, RuntimeNamedTypeInfo definingTypeInfo, RuntimeTypeInfo contextTypeInfo, RuntimeTypeInfo reflectedType) { _eventHandle = eventHandle; _definingTypeInfo = definingTypeInfo; _contextTypeInfo = contextTypeInfo; + _reflectedType = reflectedType; _reader = definingTypeInfo.Reader; _event = eventHandle.GetEvent(_reader); } @@ -68,7 +69,7 @@ namespace System.Reflection.Runtime.EventInfos MethodSemantics methodSemantics = methodSemanticsHandle.GetMethodSemantics(_reader); if (methodSemantics.Attributes == MethodSemanticsAttributes.AddOn) { - return RuntimeNamedMethodInfo.GetRuntimeNamedMethodInfo(methodSemantics.Method, _definingTypeInfo, _contextTypeInfo); + return RuntimeNamedMethodInfo.GetRuntimeNamedMethodInfo(methodSemantics.Method, _definingTypeInfo, _contextTypeInfo, _reflectedType); } } throw new BadImageFormatException(); // Added is a required method. @@ -123,6 +124,8 @@ namespace System.Reflection.Runtime.EventInfos return false; if (!(_contextTypeInfo.Equals(other._contextTypeInfo))) return false; + if (!(_reflectedType.Equals(other._reflectedType))) + return false; return true; } @@ -168,6 +171,14 @@ namespace System.Reflection.Runtime.EventInfos } } + public sealed override Type ReflectedType + { + get + { + return _reflectedType; + } + } + public sealed override MethodInfo RaiseMethod { get @@ -182,7 +193,7 @@ namespace System.Reflection.Runtime.EventInfos MethodSemantics methodSemantics = methodSemanticsHandle.GetMethodSemantics(_reader); if (methodSemantics.Attributes == MethodSemanticsAttributes.Fire) { - return RuntimeNamedMethodInfo.GetRuntimeNamedMethodInfo(methodSemantics.Method, _definingTypeInfo, _contextTypeInfo); + return RuntimeNamedMethodInfo.GetRuntimeNamedMethodInfo(methodSemantics.Method, _definingTypeInfo, _contextTypeInfo, _reflectedType); } } return null; @@ -203,7 +214,7 @@ namespace System.Reflection.Runtime.EventInfos MethodSemantics methodSemantics = methodSemanticsHandle.GetMethodSemantics(_reader); if (methodSemantics.Attributes == MethodSemanticsAttributes.RemoveOn) { - return RuntimeNamedMethodInfo.GetRuntimeNamedMethodInfo(methodSemantics.Method, _definingTypeInfo, _contextTypeInfo); + return RuntimeNamedMethodInfo.GetRuntimeNamedMethodInfo(methodSemantics.Method, _definingTypeInfo, _contextTypeInfo, _reflectedType); } } throw new BadImageFormatException(); // Removed is a required method. @@ -256,6 +267,7 @@ namespace System.Reflection.Runtime.EventInfos private readonly RuntimeNamedTypeInfo _definingTypeInfo; private readonly EventHandle _eventHandle; private readonly RuntimeTypeInfo _contextTypeInfo; + private readonly RuntimeTypeInfo _reflectedType; private readonly MetadataReader _reader; private readonly Event _event; diff --git a/src/System.Private.Reflection.Core/src/System/Reflection/Runtime/FieldInfos/RuntimeFieldInfo.cs b/src/System.Private.Reflection.Core/src/System/Reflection/Runtime/FieldInfos/RuntimeFieldInfo.cs index a7aebcd76..7be9abd89 100644 --- a/src/System.Private.Reflection.Core/src/System/Reflection/Runtime/FieldInfos/RuntimeFieldInfo.cs +++ b/src/System.Private.Reflection.Core/src/System/Reflection/Runtime/FieldInfos/RuntimeFieldInfo.cs @@ -48,11 +48,12 @@ namespace System.Reflection.Runtime.FieldInfos // // We don't report any DeclaredMembers for arrays or generic parameters so those don't apply. // - private RuntimeFieldInfo(FieldHandle fieldHandle, RuntimeNamedTypeInfo definingTypeInfo, RuntimeTypeInfo contextTypeInfo) + private RuntimeFieldInfo(FieldHandle fieldHandle, RuntimeNamedTypeInfo definingTypeInfo, RuntimeTypeInfo contextTypeInfo, RuntimeTypeInfo reflectedType) { _fieldHandle = fieldHandle; _definingTypeInfo = definingTypeInfo; _contextTypeInfo = contextTypeInfo; + _reflectedType = reflectedType; _reader = definingTypeInfo.Reader; _field = fieldHandle.GetField(_reader); } @@ -143,6 +144,14 @@ namespace System.Reflection.Runtime.FieldInfos } } + public sealed override Type ReflectedType + { + get + { + return _reflectedType; + } + } + public sealed override void SetValue(object obj, object value, BindingFlags invokeAttr, Binder binder, CultureInfo culture) { #if ENABLE_REFLECTION_TRACE @@ -175,6 +184,8 @@ namespace System.Reflection.Runtime.FieldInfos return false; if (!(_contextTypeInfo.Equals(other._contextTypeInfo))) return false; + if (!(_reflectedType.Equals(other._reflectedType))) + return false; return true; } @@ -265,6 +276,7 @@ namespace System.Reflection.Runtime.FieldInfos private readonly RuntimeNamedTypeInfo _definingTypeInfo; private readonly FieldHandle _fieldHandle; private readonly RuntimeTypeInfo _contextTypeInfo; + private readonly RuntimeTypeInfo _reflectedType; private readonly MetadataReader _reader; private readonly Field _field; diff --git a/src/System.Private.Reflection.Core/src/System/Reflection/Runtime/General/ApiToDoList.cs b/src/System.Private.Reflection.Core/src/System/Reflection/Runtime/General/ApiToDoList.cs index 9ccef7c96..b169e6a62 100644 --- a/src/System.Private.Reflection.Core/src/System/Reflection/Runtime/General/ApiToDoList.cs +++ b/src/System.Private.Reflection.Core/src/System/Reflection/Runtime/General/ApiToDoList.cs @@ -39,7 +39,6 @@ namespace System.Reflection.Runtime.MethodInfos { public sealed override MethodBody GetMethodBody() { throw new NotImplementedException(); } public sealed override RuntimeMethodHandle MethodHandle { get { throw new NotImplementedException(); } } - public sealed override Type ReflectedType { get { throw new NotImplementedException(); } } } } @@ -55,7 +54,6 @@ namespace System.Reflection.Runtime.EventInfos internal sealed partial class RuntimeEventInfo { public sealed override MethodInfo[] GetOtherMethods(bool nonPublic) { throw new NotImplementedException(); } - public sealed override Type ReflectedType { get { throw new NotImplementedException(); } } } } @@ -66,7 +64,6 @@ namespace System.Reflection.Runtime.FieldInfos public sealed override RuntimeFieldHandle FieldHandle { get { throw new NotImplementedException(); } } public sealed override Type[] GetOptionalCustomModifiers() { throw new NotImplementedException(); } public sealed override Type[] GetRequiredCustomModifiers() { throw new NotImplementedException(); } - public sealed override Type ReflectedType { get { throw new NotImplementedException(); } } } } @@ -76,7 +73,6 @@ namespace System.Reflection.Runtime.MethodInfos { public sealed override MethodBody GetMethodBody() { throw new NotImplementedException(); } public sealed override RuntimeMethodHandle MethodHandle { get { throw new NotImplementedException(); } } - public sealed override Type ReflectedType { get { throw new NotImplementedException(); } } } } @@ -115,7 +111,6 @@ namespace System.Reflection.Runtime.PropertyInfos { internal sealed partial class RuntimePropertyInfo { - public sealed override Type ReflectedType { get { throw new NotImplementedException(); } } public sealed override Type[] GetOptionalCustomModifiers() { throw new NotImplementedException(); } public sealed override Type[] GetRequiredCustomModifiers() { throw new NotImplementedException(); } } @@ -126,6 +121,5 @@ namespace System.Reflection.Runtime.TypeInfos internal abstract partial class RuntimeTypeInfo { public sealed override object InvokeMember(string name, BindingFlags invokeAttr, Binder binder, object target, object[] args, ParameterModifier[] modifiers, CultureInfo culture, string[] namedParameters) { throw new NotImplementedException(); } - public sealed override Type ReflectedType { get { throw new NotImplementedException(); } } } } diff --git a/src/System.Private.Reflection.Core/src/System/Reflection/Runtime/General/Dispensers.cs b/src/System.Private.Reflection.Core/src/System/Reflection/Runtime/General/Dispensers.cs index 4edd86e9d..e1d327f71 100644 --- a/src/System.Private.Reflection.Core/src/System/Reflection/Runtime/General/Dispensers.cs +++ b/src/System.Private.Reflection.Core/src/System/Reflection/Runtime/General/Dispensers.cs @@ -169,9 +169,9 @@ namespace System.Reflection.Runtime.FieldInfos //----------------------------------------------------------------------------------------------------------- internal sealed partial class RuntimeFieldInfo { - internal static RuntimeFieldInfo GetRuntimeFieldInfo(FieldHandle fieldHandle, RuntimeNamedTypeInfo definingTypeInfo, RuntimeTypeInfo contextTypeInfo) + internal static RuntimeFieldInfo GetRuntimeFieldInfo(FieldHandle fieldHandle, RuntimeNamedTypeInfo definingTypeInfo, RuntimeTypeInfo contextTypeInfo, RuntimeTypeInfo reflectedType) { - return new RuntimeFieldInfo(fieldHandle, definingTypeInfo, contextTypeInfo).WithDebugName(); + return new RuntimeFieldInfo(fieldHandle, definingTypeInfo, contextTypeInfo, reflectedType).WithDebugName(); } } } @@ -205,9 +205,9 @@ namespace System.Reflection.Runtime.MethodInfos //----------------------------------------------------------------------------------------------------------- internal sealed partial class RuntimeNamedMethodInfo : RuntimeMethodInfo { - internal static RuntimeNamedMethodInfo GetRuntimeNamedMethodInfo(MethodHandle methodHandle, RuntimeNamedTypeInfo definingTypeInfo, RuntimeTypeInfo contextTypeInfo) + internal static RuntimeNamedMethodInfo GetRuntimeNamedMethodInfo(MethodHandle methodHandle, RuntimeNamedTypeInfo definingTypeInfo, RuntimeTypeInfo contextTypeInfo, RuntimeTypeInfo reflectedType) { - RuntimeNamedMethodInfo method = new RuntimeNamedMethodInfo(methodHandle, definingTypeInfo, contextTypeInfo); + RuntimeNamedMethodInfo method = new RuntimeNamedMethodInfo(methodHandle, definingTypeInfo, contextTypeInfo, reflectedType); method.WithDebugName(); return method; } @@ -243,9 +243,9 @@ namespace System.Reflection.Runtime.PropertyInfos //----------------------------------------------------------------------------------------------------------- internal sealed partial class RuntimePropertyInfo { - internal static RuntimePropertyInfo GetRuntimePropertyInfo(PropertyHandle propertyHandle, RuntimeNamedTypeInfo definingTypeInfo, RuntimeTypeInfo contextTypeInfo) + internal static RuntimePropertyInfo GetRuntimePropertyInfo(PropertyHandle propertyHandle, RuntimeNamedTypeInfo definingTypeInfo, RuntimeTypeInfo contextTypeInfo, RuntimeTypeInfo reflectedType) { - return new RuntimePropertyInfo(propertyHandle, definingTypeInfo, contextTypeInfo).WithDebugName(); + return new RuntimePropertyInfo(propertyHandle, definingTypeInfo, contextTypeInfo, reflectedType).WithDebugName(); } } } @@ -257,9 +257,9 @@ namespace System.Reflection.Runtime.EventInfos //----------------------------------------------------------------------------------------------------------- internal sealed partial class RuntimeEventInfo { - internal static RuntimeEventInfo GetRuntimeEventInfo(EventHandle eventHandle, RuntimeNamedTypeInfo definingTypeInfo, RuntimeTypeInfo contextTypeInfo) + internal static RuntimeEventInfo GetRuntimeEventInfo(EventHandle eventHandle, RuntimeNamedTypeInfo definingTypeInfo, RuntimeTypeInfo contextTypeInfo, RuntimeTypeInfo reflectedType) { - return new RuntimeEventInfo(eventHandle, definingTypeInfo, contextTypeInfo).WithDebugName(); + return new RuntimeEventInfo(eventHandle, definingTypeInfo, contextTypeInfo, reflectedType).WithDebugName(); } } } diff --git a/src/System.Private.Reflection.Core/src/System/Reflection/Runtime/General/ReflectionCoreCallbacksImplementation.cs b/src/System.Private.Reflection.Core/src/System/Reflection/Runtime/General/ReflectionCoreCallbacksImplementation.cs index be4735b3c..c47091ff1 100644 --- a/src/System.Private.Reflection.Core/src/System/Reflection/Runtime/General/ReflectionCoreCallbacksImplementation.cs +++ b/src/System.Private.Reflection.Core/src/System/Reflection/Runtime/General/ReflectionCoreCallbacksImplementation.cs @@ -162,7 +162,10 @@ namespace System.Reflection.Runtime.General RuntimeTypeInfo contextTypeInfo = declaringTypeHandle.GetTypeForRuntimeTypeHandle(); RuntimeNamedTypeInfo definingTypeInfo = contextTypeInfo.AnchoringTypeDefinitionForDeclaredMembers; MetadataReader reader = definingTypeInfo.Reader; - return RuntimeFieldInfo.GetRuntimeFieldInfo(fieldHandle, definingTypeInfo, contextTypeInfo); + + // RuntimeFieldHandles always yield FieldInfo's whose ReflectedType equals the DeclaringType. + RuntimeTypeInfo reflectedType = contextTypeInfo; + return RuntimeFieldInfo.GetRuntimeFieldInfo(fieldHandle, definingTypeInfo, contextTypeInfo, reflectedType); } } } diff --git a/src/System.Private.Reflection.Core/src/System/Reflection/Runtime/MethodInfos/RuntimeConstructedGenericMethodInfo.cs b/src/System.Private.Reflection.Core/src/System/Reflection/Runtime/MethodInfos/RuntimeConstructedGenericMethodInfo.cs index e025a7d73..6cb0726c3 100644 --- a/src/System.Private.Reflection.Core/src/System/Reflection/Runtime/MethodInfos/RuntimeConstructedGenericMethodInfo.cs +++ b/src/System.Private.Reflection.Core/src/System/Reflection/Runtime/MethodInfos/RuntimeConstructedGenericMethodInfo.cs @@ -115,6 +115,14 @@ namespace System.Reflection.Runtime.MethodInfos } } + public sealed override Type ReflectedType + { + get + { + return _genericMethodDefinition.ReflectedType; + } + } + public sealed override String ToString() { return _genericMethodDefinition.ComputeToString(this); diff --git a/src/System.Private.Reflection.Core/src/System/Reflection/Runtime/MethodInfos/RuntimeConstructorInfo.cs b/src/System.Private.Reflection.Core/src/System/Reflection/Runtime/MethodInfos/RuntimeConstructorInfo.cs index 8a1959ae8..ddfba9594 100644 --- a/src/System.Private.Reflection.Core/src/System/Reflection/Runtime/MethodInfos/RuntimeConstructorInfo.cs +++ b/src/System.Private.Reflection.Core/src/System/Reflection/Runtime/MethodInfos/RuntimeConstructorInfo.cs @@ -148,6 +148,15 @@ namespace System.Reflection.Runtime.MethodInfos public abstract override int GetHashCode(); + public sealed override Type ReflectedType + { + get + { + // Constructors are always looked up as if BindingFlags.DeclaredOnly were specified. Thus, the ReflectedType will always be the DeclaringType. + return DeclaringType; + } + } + public abstract override String ToString(); protected MethodInvoker MethodInvoker diff --git a/src/System.Private.Reflection.Core/src/System/Reflection/Runtime/MethodInfos/RuntimeMethodInfo.cs b/src/System.Private.Reflection.Core/src/System/Reflection/Runtime/MethodInfos/RuntimeMethodInfo.cs index c4b0da1cf..95b4c780a 100644 --- a/src/System.Private.Reflection.Core/src/System/Reflection/Runtime/MethodInfos/RuntimeMethodInfo.cs +++ b/src/System.Private.Reflection.Core/src/System/Reflection/Runtime/MethodInfos/RuntimeMethodInfo.cs @@ -206,6 +206,8 @@ namespace System.Reflection.Runtime.MethodInfos } } + public abstract override Type ReflectedType { get; } + public sealed override ParameterInfo ReturnParameter { get diff --git a/src/System.Private.Reflection.Core/src/System/Reflection/Runtime/MethodInfos/RuntimeNamedMethodInfo.cs b/src/System.Private.Reflection.Core/src/System/Reflection/Runtime/MethodInfos/RuntimeNamedMethodInfo.cs index 73d584d42..118fdea04 100644 --- a/src/System.Private.Reflection.Core/src/System/Reflection/Runtime/MethodInfos/RuntimeNamedMethodInfo.cs +++ b/src/System.Private.Reflection.Core/src/System/Reflection/Runtime/MethodInfos/RuntimeNamedMethodInfo.cs @@ -42,10 +42,11 @@ namespace System.Reflection.Runtime.MethodInfos // // We don't report any DeclaredMembers for arrays or generic parameters so those don't apply. // - private RuntimeNamedMethodInfo(MethodHandle methodHandle, RuntimeNamedTypeInfo definingTypeInfo, RuntimeTypeInfo contextTypeInfo) + private RuntimeNamedMethodInfo(MethodHandle methodHandle, RuntimeNamedTypeInfo definingTypeInfo, RuntimeTypeInfo contextTypeInfo, RuntimeTypeInfo reflectedType) : base() { _common = new RuntimeMethodCommon(methodHandle, definingTypeInfo, contextTypeInfo); + _reflectedType = reflectedType; } public sealed override MethodAttributes Attributes @@ -146,6 +147,14 @@ namespace System.Reflection.Runtime.MethodInfos } } + public sealed override Type ReflectedType + { + get + { + return _reflectedType; + } + } + public sealed override String ToString() { return ComputeToString(this); @@ -156,7 +165,11 @@ namespace System.Reflection.Runtime.MethodInfos RuntimeNamedMethodInfo other = obj as RuntimeNamedMethodInfo; if (other == null) return false; - return _common.Equals(other._common); + if (!_common.Equals(other._common)) + return false; + if (!(_reflectedType.Equals(other._reflectedType))) + return false; + return true; } public sealed override int GetHashCode() @@ -232,7 +245,14 @@ namespace System.Reflection.Runtime.MethodInfos { // Desktop compat: Constructed generic types and their generic type definitions share the same Type objects for method generic parameters. RuntimeNamedTypeInfo genericTypeDefinition = DeclaringType.GetGenericTypeDefinition().CastToRuntimeNamedTypeInfo(); - owningMethod = RuntimeNamedMethodInfo.GetRuntimeNamedMethodInfo(Handle, genericTypeDefinition, genericTypeDefinition); + owningMethod = RuntimeNamedMethodInfo.GetRuntimeNamedMethodInfo(Handle, genericTypeDefinition, genericTypeDefinition, genericTypeDefinition); + } + else + { + // Desktop compat: DeclaringMethod always returns a MethodInfo whose ReflectedType is equal to DeclaringType. + if (!_reflectedType.Equals(_common.DeclaringType)) + owningMethod = RuntimeNamedMethodInfo.GetRuntimeNamedMethodInfo(_common.MethodHandle, _common.DefiningTypeInfo, _common.ContextTypeInfo, _common.DeclaringType); + } RuntimeTypeInfo genericParameterType = RuntimeGenericParameterTypeInfoForMethods.GetRuntimeGenericParameterTypeInfoForMethods(owningMethod, owningMethod._common.Reader, genericParameterHandle); genericTypeParameters[i++] = genericParameterType; @@ -250,5 +270,6 @@ namespace System.Reflection.Runtime.MethodInfos } private readonly RuntimeMethodCommon _common; + private readonly RuntimeTypeInfo _reflectedType; } } diff --git a/src/System.Private.Reflection.Core/src/System/Reflection/Runtime/MethodInfos/RuntimeSyntheticMethodInfo.cs b/src/System.Private.Reflection.Core/src/System/Reflection/Runtime/MethodInfos/RuntimeSyntheticMethodInfo.cs index 4010ccb98..4159dfdb8 100644 --- a/src/System.Private.Reflection.Core/src/System/Reflection/Runtime/MethodInfos/RuntimeSyntheticMethodInfo.cs +++ b/src/System.Private.Reflection.Core/src/System/Reflection/Runtime/MethodInfos/RuntimeSyntheticMethodInfo.cs @@ -115,6 +115,16 @@ namespace System.Reflection.Runtime.MethodInfos } } + public sealed override Type ReflectedType + { + get + { + // The only synthetic methods come from array types which can never be inherited from. So unless that changes, + // we don't provide a way to specify the ReflectedType. + return DeclaringType; + } + } + public sealed override String ToString() { return RuntimeMethodCommon.ComputeToString(this, Array.Empty<RuntimeTypeInfo>(), RuntimeParameters, RuntimeReturnParameter); diff --git a/src/System.Private.Reflection.Core/src/System/Reflection/Runtime/PropertyInfos/RuntimePropertyInfo.cs b/src/System.Private.Reflection.Core/src/System/Reflection/Runtime/PropertyInfos/RuntimePropertyInfo.cs index 34edd9a3b..c696dccb6 100644 --- a/src/System.Private.Reflection.Core/src/System/Reflection/Runtime/PropertyInfos/RuntimePropertyInfo.cs +++ b/src/System.Private.Reflection.Core/src/System/Reflection/Runtime/PropertyInfos/RuntimePropertyInfo.cs @@ -49,11 +49,12 @@ namespace System.Reflection.Runtime.PropertyInfos // // We don't report any DeclaredMembers for arrays or generic parameters so those don't apply. // - private RuntimePropertyInfo(PropertyHandle propertyHandle, RuntimeNamedTypeInfo definingTypeInfo, RuntimeTypeInfo contextTypeInfo) + private RuntimePropertyInfo(PropertyHandle propertyHandle, RuntimeNamedTypeInfo definingTypeInfo, RuntimeTypeInfo contextTypeInfo, RuntimeTypeInfo reflectedType) { _propertyHandle = propertyHandle; _definingTypeInfo = definingTypeInfo; _contextTypeInfo = contextTypeInfo; + _reflectedType = reflectedType; _reader = definingTypeInfo.Reader; _property = propertyHandle.GetProperty(_reader); } @@ -124,6 +125,8 @@ namespace System.Reflection.Runtime.PropertyInfos return false; if (!(_contextTypeInfo.Equals(other._contextTypeInfo))) return false; + if (!(_reflectedType.Equals(other._reflectedType))) + return false; return true; } @@ -247,6 +250,14 @@ namespace System.Reflection.Runtime.PropertyInfos } } + public sealed override Type ReflectedType + { + get + { + return _reflectedType; + } + } + public sealed override MethodInfo SetMethod { get @@ -340,7 +351,7 @@ namespace System.Reflection.Runtime.PropertyInfos MethodHandle getterHandle; if (GetAccessor(MethodSemanticsAttributes.Getter, out getterHandle)) { - return RuntimeNamedMethodInfo.GetRuntimeNamedMethodInfo(getterHandle, _definingTypeInfo, _contextTypeInfo); + return RuntimeNamedMethodInfo.GetRuntimeNamedMethodInfo(getterHandle, _definingTypeInfo, _contextTypeInfo, _reflectedType); } return null; } @@ -353,7 +364,7 @@ namespace System.Reflection.Runtime.PropertyInfos MethodHandle setterHandle; if (GetAccessor(MethodSemanticsAttributes.Setter, out setterHandle)) { - return RuntimeNamedMethodInfo.GetRuntimeNamedMethodInfo(setterHandle, _definingTypeInfo, _contextTypeInfo); + return RuntimeNamedMethodInfo.GetRuntimeNamedMethodInfo(setterHandle, _definingTypeInfo, _contextTypeInfo, _reflectedType); } return null; } @@ -361,12 +372,22 @@ namespace System.Reflection.Runtime.PropertyInfos private bool GetAccessor(MethodSemanticsAttributes methodSemanticsAttribute, out MethodHandle methodHandle) { + bool inherited = !_reflectedType.Equals(_contextTypeInfo); + foreach (MethodSemanticsHandle methodSemanticsHandle in _property.MethodSemantics) { MethodSemantics methodSemantics = methodSemanticsHandle.GetMethodSemantics(_reader); if (methodSemantics.Attributes == methodSemanticsAttribute) { methodHandle = methodSemantics.Method; + + if (inherited) + { + MethodAttributes flags = methodHandle.GetMethod(_reader).Flags; + if ((flags & MethodAttributes.MemberAccessMask) == MethodAttributes.Private) + continue; + } + return true; } } @@ -394,6 +415,7 @@ namespace System.Reflection.Runtime.PropertyInfos private readonly RuntimeNamedTypeInfo _definingTypeInfo; private readonly PropertyHandle _propertyHandle; private readonly RuntimeTypeInfo _contextTypeInfo; + private readonly RuntimeTypeInfo _reflectedType; private readonly MetadataReader _reader; private readonly Property _property; diff --git a/src/System.Private.Reflection.Core/src/System/Reflection/Runtime/TypeInfos/RuntimeTypeInfo.CoreGetDeclared.cs b/src/System.Private.Reflection.Core/src/System/Reflection/Runtime/TypeInfos/RuntimeTypeInfo.CoreGetDeclared.cs index 4f69aa03d..f043d3044 100644 --- a/src/System.Private.Reflection.Core/src/System/Reflection/Runtime/TypeInfos/RuntimeTypeInfo.CoreGetDeclared.cs +++ b/src/System.Private.Reflection.Core/src/System/Reflection/Runtime/TypeInfos/RuntimeTypeInfo.CoreGetDeclared.cs @@ -73,7 +73,7 @@ namespace System.Reflection.Runtime.TypeInfos continue; if (optionalNameFilter == null || optionalNameFilter.Matches(method.Name, reader)) - yield return RuntimeNamedMethodInfo.GetRuntimeNamedMethodInfo(methodHandle, definingType, this); + yield return RuntimeNamedMethodInfo.GetRuntimeNamedMethodInfo(methodHandle, definingType, this, reflectedType); } } @@ -93,7 +93,7 @@ namespace System.Reflection.Runtime.TypeInfos foreach (EventHandle eventHandle in definingType.DeclaredEventHandles) { if (optionalNameFilter == null || optionalNameFilter.Matches(eventHandle.GetEvent(reader).Name, reader)) - yield return RuntimeEventInfo.GetRuntimeEventInfo(eventHandle, definingType, this); + yield return RuntimeEventInfo.GetRuntimeEventInfo(eventHandle, definingType, this, reflectedType); } } } @@ -107,7 +107,7 @@ namespace System.Reflection.Runtime.TypeInfos foreach (FieldHandle fieldHandle in definingType.DeclaredFieldHandles) { if (optionalNameFilter == null || optionalNameFilter.Matches(fieldHandle.GetField(reader).Name, reader)) - yield return RuntimeFieldInfo.GetRuntimeFieldInfo(fieldHandle, definingType, this); + yield return RuntimeFieldInfo.GetRuntimeFieldInfo(fieldHandle, definingType, this, reflectedType); } } } @@ -121,7 +121,7 @@ namespace System.Reflection.Runtime.TypeInfos foreach (PropertyHandle propertyHandle in definingType.DeclaredPropertyHandles) { if (optionalNameFilter == null || optionalNameFilter.Matches(propertyHandle.GetProperty(reader).Name, reader)) - yield return RuntimePropertyInfo.GetRuntimePropertyInfo(propertyHandle, definingType, this); + yield return RuntimePropertyInfo.GetRuntimePropertyInfo(propertyHandle, definingType, this, reflectedType); } } } diff --git a/src/System.Private.Reflection.Core/src/System/Reflection/Runtime/TypeInfos/RuntimeTypeInfo.cs b/src/System.Private.Reflection.Core/src/System/Reflection/Runtime/TypeInfos/RuntimeTypeInfo.cs index 95ec6191f..7aa583957 100644 --- a/src/System.Private.Reflection.Core/src/System/Reflection/Runtime/TypeInfos/RuntimeTypeInfo.cs +++ b/src/System.Private.Reflection.Core/src/System/Reflection/Runtime/TypeInfos/RuntimeTypeInfo.cs @@ -519,6 +519,16 @@ namespace System.Reflection.Runtime.TypeInfos } } + public sealed override Type ReflectedType + { + get + { + // Desktop compat: For types, ReflectedType == DeclaringType. Nested types are always looked up as BindingFlags.DeclaredOnly was passed. + // For non-nested types, the concept of a ReflectedType doesn't even make sense. + return DeclaringType; + } + } + public abstract override StructLayoutAttribute StructLayoutAttribute { get; } public abstract override string ToString(); |