Skip to content

GetMemberWithSameMetadataDefinitionAs returns incorrect declaring type #67533

Closed

Description

@MichalStrehovsky
  [FAIL] System.Reflection.Tests.TypeInfoTests.GetMemberWithSameMetadataDefinitionAs(openGenericType: typeof(System.Reflection.Tests.TI_GenericTypeWithAllMembers<>), closedGenericType: typeof(System.Reflection.Tests.TI_TypeDerivedFromGenericTypeWithAllMembers<int>), checkDeclaringType: False)
  System.ArgumentException : A MemberInfo that matches 'add_PrivateStaticEvent' could not be found. (Parameter 'member')
     at System.Reflection.Runtime.TypeInfos.RuntimeTypeInfo.GetMemberWithSameMetadataDefinitionAs(MemberInfo) + 0x154
     at System.Reflection.Tests.TypeInfoTests.GetMemberWithSameMetadataDefinitionAs(Type, Type, Boolean) + 0x70
     at System.Reflection.Tests!<BaseAddress>+0x6812ca
     at System.InvokeUtils.CallDynamicInvokeMethod(Object, IntPtr, IntPtr, IntPtr, Object, Object[], BinderBundle, Boolean, Boolean) + 0x150
  [FAIL] System.Reflection.Tests.TypeInfoTests.GetMemberWithSameMetadataDefinitionAs(openGenericType: typeof(System.Reflection.Tests.TI_GenericTypeWithAllMembers<>), closedGenericType: typeof(System.Reflection.Tests.TI_TypeDerivedFromGenericTypeWithAllMembersClosed), checkDeclaringType: False)
  System.ArgumentException : A MemberInfo that matches 'add_PrivateStaticEvent' could not be found. (Parameter 'member')
     at System.Reflection.Runtime.TypeInfos.RuntimeTypeInfo.GetMemberWithSameMetadataDefinitionAs(MemberInfo) + 0x154
     at System.Reflection.Tests.TypeInfoTests.GetMemberWithSameMetadataDefinitionAs(Type, Type, Boolean) + 0x70
     at System.Reflection.Tests!<BaseAddress>+0x6812ca
     at System.InvokeUtils.CallDynamicInvokeMethod(Object, IntPtr, IntPtr, IntPtr, Object, Object[], BinderBundle, Boolean, Boolean) + 0x150
  [FAIL] System.Reflection.Tests.TypeInfoTests.GetMemberWithSameMetadataDefinitionAs(openGenericType: typeof(System.Reflection.Tests.TI_GenericTypeWithAllMembers<>), closedGenericType: typeof(System.Reflection.Tests.TI_TypeDerivedFromGenericTypeWithAllMembers<System.Reflection.Tests.TypeInfoTests+ClassWithMultipleConstructors>), checkDeclaringType: False)
  System.ArgumentException : A MemberInfo that matches 'add_PrivateStaticEvent' could not be found. (Parameter 'member')
     at System.Reflection.Runtime.TypeInfos.RuntimeTypeInfo.GetMemberWithSameMetadataDefinitionAs(MemberInfo) + 0x154
     at System.Reflection.Tests.TypeInfoTests.GetMemberWithSameMetadataDefinitionAs(Type, Type, Boolean) + 0x70
     at System.Reflection.Tests!<BaseAddress>+0x6812ca
     at System.InvokeUtils.CallDynamicInvokeMethod(Object, IntPtr, IntPtr, IntPtr, Object, Object[], BinderBundle, Boolean, Boolean) + 0x150
  [FAIL] System.Reflection.Tests.TypeInfoTests.GetMemberWithSameMetadataDefinitionAs(openGenericType: typeof(System.Reflection.Tests.TI_GenericTypeWithAllMembers), closedGenericType: typeof(System.Reflection.Tests.TI_GenericTypeWithAllMembers), checkDeclaringType: True)
  'add_PrivateStaticEvent' doesn't have the right DeclaringType
  Expected: True
  Actual:   False
     at System.Reflection.Tests.TypeInfoTests.GetMemberWithSameMetadataDefinitionAs(Type, Type, Boolean) + 0x199
     at System.Reflection.Tests!<BaseAddress>+0x6812ca
     at System.InvokeUtils.CallDynamicInvokeMethod(Object, IntPtr, IntPtr, IntPtr, Object, Object[], BinderBundle, Boolean, Boolean) + 0x150
  [FAIL] System.Reflection.Tests.TypeInfoTests.GetMemberWithSameMetadataDefinitionAs(openGenericType: typeof(System.Reflection.Tests.TI_GenericTypeWithAllMembers<>), closedGenericType: typeof(System.Reflection.Tests.TI_GenericTypeWithAllMembers), checkDeclaringType: True)
  'add_PrivateStaticEvent' doesn't have the right DeclaringType
  Expected: True
  Actual:   False
     at System.Reflection.Tests.TypeInfoTests.GetMemberWithSameMetadataDefinitionAs(Type, Type, Boolean) + 0x199
     at System.Reflection.Tests!<BaseAddress>+0x6812ca
     at System.InvokeUtils.CallDynamicInvokeMethod(Object, IntPtr, IntPtr, IntPtr, Object, Object[], BinderBundle, Boolean, Boolean) + 0x150

Activity

  1. added this to the 7.0.0 milestone on Apr 4, 2022
  2. ghost added
    untriaged
    on Apr 4, 2022
  3. removed
    untriaged
    on Apr 4, 2022
  4. self-assigned this
    on Apr 28, 2022
  5. LakshanF commented on Apr 29, 2022

    @LakshanF
    Contributor

    The test is failing from the following scenario as discovered in the PR #67575,

    A repro for the first failing scenario with the below call stack (regular .NET app output is also shown below). Specifically, in a scenario like the following,

    • OpenGenericType b, ClosedGenericType d

    b has a cctor
    d is derived from b

    • MemberInfo m1 in b where m1 is cctor. specifically,

    query b.GetMembers(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance | BindingFlags.DeclaredOnly)
    enumerate each MemberInfo from above and call d.GetMemberWithSameMetadataDefinitionAs

    • m1 Is not present in d.GetMemberWithSameMetadataDefinitionAs(m1)

    Repro.txt

    Reflection Test Scenario START
    Program+B_T`1->Program+D_T`1[[System.Int32, System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]]
            Program+B_T`1:.cctor
    Unhandled Exception: System.ArgumentException: A MemberInfo that matches '.cctor' could not be found. (Parameter 'member')
       at System.Reflection.Runtime.TypeInfos.RuntimeTypeInfo.GetMemberWithSameMetadataDefinitionAs(MemberInfo) + 0x287
       at Program.GetMemberWithSameMetadataDefinitionAs(Type, Type) + 0x1c6
       at Program.Main(String[]) + 0xa7
       at reproNative!<BaseAddress>+0x4ae467
       at reproNative!<BaseAddress>+0x4ae4f0
    

    .NET program output

    Reflection Test Scenario START
    Program+B_T`1->Program+D_T`1[[System.Int32, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]]
            Program+B_T`1:.cctor
            Program+B_T`1:.ctor
    Reflection Test Scenario END
    
    
  6. LakshanF commented on May 4, 2022

    @LakshanF
    Contributor

    Looking at a baseline case where enumerating MemberInfo's where the OpenGenericType is d (see above comment) works. Although the only member found is a ctor (not the base's cctor). The output is shown below the program (.NET program output is the same).

    Comparing the relevant nodes for these 2 programs (Repro and Baseline) of the relevant types, b and d, using DependencyGraphViewer, did not show any missing nodes although the order of the nodes that were generated in the 2 programs for both types were different.

    Baseline.txt

    Reflection Test Scenario START
    Program+D_T`1->Program+D_T`1[[System.Int32, System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]]
            Program+D_T`1:.ctor
    Reflection Test Scenario END
    
    
  7. LakshanF commented on May 5, 2022

    @LakshanF
    Contributor

    The problem seems to be in the reflection code. The CLR code walks up the type hierarchy to find the MemberInfo whereas native aot doesn't.

  8. jkotas commented on May 10, 2022

    @jkotas
    Member

    The fix was reverted

  9. reopened this on May 10, 2022
  10. LakshanF commented on May 11, 2022

    @LakshanF
    Contributor

    In NativeAOT, when a TypeDelegator is used to wrap a closed generic type, the declared type of its members are no longer RuntimeTypes. The output for .NET and NativeAot for the code snippet is as follows,

    .NET

    (t1): <U_T`1>
    MemberInfo (m1): <.ctor>
    m1.DeclaringType.Equals(t1): <True>
    t1.GetType().FullName: <System.Reflection.TypeDelegator>
    t1.UnderlyingSystemType.GetType().FullName: <System.RuntimeType>
    m1.DeclaringType.GetType().FullName: <System.RuntimeType>
    m1.DeclaringType.UnderlyingSystemType.GetType().FullName: <System.RuntimeType>
    

    NativeAot

    (t1): <U_T`1>
    MemberInfo (m1): <.ctor>
    m1.DeclaringType.Equals(t1): <False>
    t1.GetType().FullName: <System.Reflection.TypeDelegator>
    t1.UnderlyingSystemType.GetType().FullName: <$BlockedFromReflection_0_58b71190>
    m1.DeclaringType.GetType().FullName: <$BlockedFromReflection_0_58b71190>
    m1.DeclaringType.UnderlyingSystemType.GetType().FullName: <$BlockedFromReflection_0_58b71190>
    
    using System;
    using System.Reflection;
    
    public class Program
    {
        static void Main(string[] args)
        {
            BindingFlags all = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance | BindingFlags.DeclaredOnly;
            Type t1 = new TypeDelegator(typeof(U_T<A_T>));
            var m1 = t1.GetMembers(all)[0];
            Console.WriteLine($"(t1): <{t1.Name}>");
            Console.WriteLine($"MemberInfo (m1): <{m1.Name}>");
            Console.WriteLine($"m1.DeclaringType.Equals(t1): <{m1.DeclaringType.Equals(t1)}>");
            Console.WriteLine($"t1.GetType().FullName: <{t1.GetType().FullName}>");
            Console.WriteLine($"t1.UnderlyingSystemType.GetType().FullName: <{t1.UnderlyingSystemType.GetType().FullName}>");
            Console.WriteLine($"m1.DeclaringType.GetType().FullName: <{m1.DeclaringType.GetType().FullName}>");
            Console.WriteLine($"m1.DeclaringType.UnderlyingSystemType.GetType().FullName: <{m1.DeclaringType.UnderlyingSystemType.GetType().FullName}>");
        }
    
        public class U_T<T>
        {
        }
    
        public class A_T
        {
        }
    }
  11. LakshanF commented on May 12, 2022

    @LakshanF
    Contributor

    Created a separate issue, #69244, to track this

  12. ghost added
    in-pr
    on May 15, 2022
  13. ghost removed
    in-pr
    on May 17, 2022
  14. ghost locked as resolved and limited conversation to collaborators on Jun 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions