ValveKeyValue icon indicating copy to clipboard operation
ValveKeyValue copied to clipboard

Why are object members sorted by name?

Open xPaw opened this issue 2 years ago • 3 comments

This loses developer defined order in classes when serializing.

https://github.com/SteamDatabase/ValveKeyValue/blob/ed10cc104681d33935fc2b7ea3d6701970a581f5/ValveKeyValue/ValveKeyValue/ObjectCopier.cs#L120

cc @yaakov-h

xPaw avatar Jul 24 '23 08:07 xPaw

Probably for consistency/reliability, particularly for the unit tests.

In .NET 6 and earlier versions, the GetProperties method does not return properties in a particular order, such as alphabetical or declaration order. Your code must not depend on the order in which properties are returned, because that order varies. However, starting with .NET 7, the ordering is deterministic based upon the metadata ordering in the assembly.

-- https://learn.microsoft.com/en-us/dotnet/api/system.type.getproperties?view=net-7.0#system-type-getproperties

yaakov-h avatar Jul 25 '23 04:07 yaakov-h

Right, that makes sense.

Perhaps we can order by MetadataToken, is that what .NET7 does?

xPaw avatar Jul 25 '23 12:07 xPaw

I'm not sure what .NET 7 runtime does, its very hard to dig through this bit of the BCL/CLR codebase.

yaakov-h avatar Jul 25 '23 12:07 yaakov-h