mgravell / fast-member

Automatically exported from code.google.com/p/fast-member
Apache License 2.0
1.02k stars 137 forks source link

Order of items in Memberset is locale sensitive #96

Open Itsalmo opened 2 years ago

Itsalmo commented 2 years ago

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 commented 2 years ago

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

superrnovae commented 1 year ago

Reflection API now orders properties by declaration order.