Skip to content

order of DeclaredMethods #2330

Closed
Closed

Description

@ufcpp

I found order of DeclaredMethods is a bit strange when the number of members are greater than 85.

step to repro:

https://gist.github.com/ufcpp/06e2c9e82c0c518b654c0e30eeb9cd63#file-methodorder-cs

expected result:

All of the result of Console.Write are

M00M01M02M03M04M05M06M07M08 ...

actual result:

The result using Type.GetMethods/TypeInfo.DeclaredMethods on .NET Core 3.0 or later is

M85M86M87M88M89M00M01M02M03 ...

FYI, that on .NET Core 2.2 or earlier is as expected.

Activity

  1. added
    untriaged
    on Jan 29, 2020
  2. vitek-karas commented on Jan 29, 2020

    @vitek-karas
    Member

    Please note that the user code must not rely on the ordering of the methods returned from reflection - DeclaredMethods is one such example. The API officially specified that the returned order is "undefined". The reason for the differences is that the method doesn't internally sort the list before returning it (for performance reasons), it simply walks the runtime structures. So any change to how the runtime stores these internally will be potentially observable in the order reported through the API. You might also see different order on different platforms (X86/X64, or Windows/Linux, ...). Also note that for example using Ready2Run on the assembly will likely change the order as well.

    That said we could track down what exact change in the runtime caused the difference between .NET Core 2.* and .NET Core 3.*, but I don't think it's worth the effort.

  3. added
    question
    on Jan 29, 2020
  4. removed
    question
    untriaged
    on Mar 10, 2020
  5. steveharter commented on Mar 10, 2020

    @steveharter
    Contributor

    Closing; reflection order is non-deterministic.

  6. ghost locked as resolved and limited conversation to collaborators on Dec 10, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions