Repository navigation
Closed
Closed
Copy link
Description
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:
runtime/src/coreclr/System.Private.CoreLib/src/System/RuntimeType.CoreCLR.cs
Lines 649 to 746 in 1be8120
| 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?
Reacted by Copilot
Activity
MihuBot commentedon Feb 14, 2026
I'm a bot. Here is a possible duplicate issue (I may be wrong):
Youssef1313 commentedon Feb 14, 2026
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:
This program prints:
I don't care about the order of members in a particular type. What I care about is that all members of
Derivedcome before all members ofBase, come before all members ofSystem.Object.stephentoub commentedon Feb 14, 2026
Historically we have not made any such guarantees, even if the implementation happens to do that today.
dotnet-policy-service commentedon Feb 14, 2026
Tagging subscribers to this area: @dotnet/area-system-reflection
See info in area-owners.md if you want to be subscribed.
Youssef1313 commentedon Feb 14, 2026
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.
steveisok commentedon Jul 10, 2026
@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.