Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Order of items in Memberset is locale sensitive #96

Open
Itsalmo opened this issue Feb 3, 2022 · 2 comments
Open

Order of items in Memberset is locale sensitive #96

Itsalmo opened this issue Feb 3, 2022 · 2 comments

Comments

@Itsalmo
Copy link

Itsalmo commented Feb 3, 2022

Found a problem in my (client/server) application today, which was caused by the members collection in the Memberset being sorted by name:

internal MemberSet(Type type)
{
  const BindingFlags PublicInstance = BindingFlags.Public | BindingFlags.Instance;

  members = type.GetTypeAndInterfaceProperties(PublicInstance).Cast<MemberInfo().Concat(type.GetFields(PublicInstance).Cast<MemberInfo>())
  .OrderBy(x => x.Name)
  .Select(member => new Member(member)).ToArray();
}

The server part is running on a machine with en-US locale, the client on a machine with sv-SE locale. On the client machine, with the sv-SE locale, members of type X whose names begin with 'V' and 'W' are sorted in the order W, V. On the en-US server machine, the order of those same members is of course in the order V, W. I'm streaming instances of type X from server to client by enumerating the members, and was wrongfully assuming that the order of the members for the exact same Type would be equal on both machines.

Is there a reason these members are sorted instead of just keeping them in the order in which they occur in the type? And if there is, may I propose to do a:

.OrderBy(x => x.Name, StringComparer.InvariantCulture)

to make the order locale insensitive?

@mgravell
Copy link
Owner

mgravell commented Feb 4, 2022

They are sorted because the reflection APIs explicitly make no guarantees about ordering whatsoever. Yes this should be invariant.

@superrnovae
Copy link

superrnovae commented Mar 15, 2023

Reflection API now orders properties by declaration order.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants