Skip to content

Document reflection guarantees around ordering when retrieving all members of type #124420

Closed

Description

@Youssef1313

Looking into (for example, Type.GetMethods) implementation details, it seems that the implementation always retrieves methods from the current type, then from the base types one by one. This behavior is guaranteed as far as I can tell via this loop:

do
{
int vtableSlots = RuntimeTypeHandle.GetNumVirtuals(declaringType);
foreach (RuntimeMethodHandleInternal methodHandle in RuntimeTypeHandle.GetIntroducedMethods(declaringType))
{
if (filter.RequiresStringComparison())
{
if (!filter.Match(RuntimeMethodHandle.GetUtf8Name(methodHandle)))
continue;
}
#region Loop through all methods on the current type
Debug.Assert(!methodHandle.IsNullHandle());
MethodAttributes methodAttributes = RuntimeMethodHandle.GetAttributes(methodHandle);
MethodAttributes methodAccess = methodAttributes & MethodAttributes.MemberAccessMask;
#region Continue if this is a constructor
Debug.Assert(
(RuntimeMethodHandle.GetAttributes(methodHandle) & MethodAttributes.RTSpecialName) == 0 ||
RuntimeMethodHandle.GetName(methodHandle).Equals(".ctor") ||
RuntimeMethodHandle.GetName(methodHandle).Equals(".cctor"));
if ((methodAttributes & MethodAttributes.RTSpecialName) != 0)
continue;
#endregion
#region Continue if this is a private declared on a base type
bool isVirtual = false;
int methodSlot = 0;
if ((methodAttributes & MethodAttributes.Virtual) != 0)
{
// only virtual if actually in the vtableslot range, but GetSlot will
// assert if an EnC method, which can't be virtual, so narrow down first
// before calling GetSlot
methodSlot = RuntimeMethodHandle.GetSlot(methodHandle);
isVirtual = (methodSlot < vtableSlots);
}
bool isInherited = declaringType != ReflectedType;
bool isPrivate = methodAccess == MethodAttributes.Private;
if (isInherited && isPrivate && !isVirtual)
continue;
#endregion
// Filter out deleted method before setting override state, so that a deleted override in a subclass does not hide override in an ancestor.
if (MetadataUpdater.IsSupported &&
RuntimeTypeMetadataUpdateHandler.FilterDeletedMembers &&
RuntimeTypeMetadataUpdateHandler.IsMetadataUpdateDeleted(declaringType.GetRuntimeModule(), RuntimeMethodHandle.GetMethodDef(methodHandle)))
{
continue;
}
#region Continue if this is a virtual and is already overridden
if (isVirtual)
{
Debug.Assert(
(methodAttributes & MethodAttributes.Abstract) != 0 ||
(methodAttributes & MethodAttributes.Virtual) != 0 ||
RuntimeMethodHandle.GetDeclaringType(methodHandle) != declaringType);
if (overrides[methodSlot])
continue;
overrides[methodSlot] = true;
}
else if (isValueType)
{
if ((methodAttributes & (MethodAttributes.Virtual | MethodAttributes.Abstract)) != 0)
continue;
}
else
{
Debug.Assert((methodAttributes & (MethodAttributes.Virtual | MethodAttributes.Abstract)) == 0);
}
#endregion
#region Calculate Binding Flags
bool isPublic = methodAccess == MethodAttributes.Public;
bool isStatic = (methodAttributes & MethodAttributes.Static) != 0;
BindingFlags bindingFlags = FilterPreCalculate(isPublic, isInherited, isStatic);
#endregion
// get the unboxing stub or instantiating stub if needed
RuntimeMethodHandleInternal instantiatedHandle = RuntimeMethodHandle.GetStubIfNeeded(methodHandle, declaringType, null);
RuntimeMethodInfo runtimeMethodInfo = new RuntimeMethodInfo(
instantiatedHandle, declaringType, m_runtimeTypeCache, methodAttributes, bindingFlags, null);
list.Add(runtimeMethodInfo);
#endregion
}
declaringType = declaringType.GetParentType()!;
} while (declaringType != null);

The loop starts with DeclaringType and then walks the inheritance hierarchy with declaringType = declaringType.GetParentType()!; at the end of each iteration.

Is it safe to rely on that behavior and assume that Type.GetMethods will always return methods from current type before any from base types? If guaranteed, can this be documented?

Activity

  1. added
    untriaged
    on Feb 14, 2026
  2. added
    needs-area-label
    on Feb 14, 2026
  3. MihuBot commented on Feb 14, 2026

    @MihuBot

    I'm a bot. Here is a possible duplicate issue (I may be wrong):

  4. Youssef1313 commented on Feb 14, 2026

    @Youssef1313
    MemberAuthor

    Note: The question here is not about ordering of members in the same type. It's about ordering of members coming through inheritance hierarchy which today's implementation seems to preserve.

    For example:

    using System;
    
    foreach (var m in typeof(Derived).GetMethods())
    {
        Console.WriteLine($"{m.DeclaringType}.{m.Name}");
    }
    
    public class Base
    {
        public void M() { }
        
        public void M2() { }
        
        public void M3() { }
    }
    
    public class Derived : Base
    {
        public new void M() { }
        public void M4() { }
    }

    This program prints:

    Derived.M
    Derived.M4
    Base.M
    Base.M2
    Base.M3
    System.Object.GetType
    System.Object.ToString
    System.Object.Equals
    System.Object.GetHashCode
    

    I don't care about the order of members in a particular type. What I care about is that all members of Derived come before all members of Base, come before all members of System.Object.

  5. stephentoub commented on Feb 14, 2026

    @stephentoub
    Member

    Historically we have not made any such guarantees, even if the implementation happens to do that today.

  6. added and removed
    needs-area-label
    on Feb 14, 2026
  7. dotnet-policy-service commented on Feb 14, 2026

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @dotnet/area-system-reflection
    See info in area-owners.md if you want to be subscribed.

  8. Youssef1313 commented on Feb 14, 2026

    @Youssef1313
    MemberAuthor

    Can that be clearly documented that callers shouldn't rely on inheritance hierarchy ordering? Or, if the behavior is unlikely to change in the future, maybe this can be documented as guaranteed?

    Either way, I would like the documentation to be clear whether this is guaranteed or not.

  9. removed
    untriaged
    on Jul 10, 2026
  10. added this to the 11.0.0 milestone on Jul 10, 2026
  11. steveisok commented on Jul 10, 2026

    @steveisok
    Member

    @jkotas pointed me to https://learn.microsoft.com/en-us/dotnet/api/system.type.getmethods?view=net-10.0 where we make a remark about not guaranteeing a particular order. I don't think we need to get more specific than what we already have.

  12. locked and limited conversation to collaborators on Aug 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions