ILSpy
ILSpy copied to clipboard
Attributes sorting order
Usually if you do export to project serveral times, ILSpy saves types with accompanying attributes in different order e.g.:
Describe the solution you'd like It'd be very nice to happen if the order of attributes would be sorted in some order and will be constant from export to export
Such feature was implemented in dnSpy and could be turned ON/OFF in Options:
Overall benefit of this feature would be to have shorter time to find the diff between multiple exports thus making end-users even more happy :)
Was also going to report this issue, but not on the UI level but the nuget pkg/decompiler settings class. I don't know if IL is preserving attribute order and this is a bug or if the order of attributes is just not defined. In the latter case, I'd suggest adding a DecompilerSetting "sort attributes in alphabetical order" (or similar) and just sorting the set when rendering the code.
That would greatly help my tooling by reducing faulty diffs (that basically happen on every file that has more than two attributes on an element)
Edit: A simple hack would probably be to sort here: https://github.com/icsharpcode/ILSpy/blob/2ed9ad6b51110acfa5430a50f34b985c9c4eb1ac/ICSharpCode.Decompiler/CSharp/OutputVisitor/CSharpOutputVisitor.cs#L569-L572
I don't know the code base at all, but essentially AstNodeCollection seems to preserve the order, so the question is why the AstNode provides the attributes in such an order, so probably sorting makes sense in the parser that populates the AstNode? or have it sort said node afterwards?
ILSpy should alway maintain the order of the attributes from the CustomAttributes metadata table. I think it's the C# compiler that emits attributes in non-deterministic order. The C# compiler needs to sort the attribute table by the parent metadata token, so my guess is that they're using a non-stable sort for doing that.
Actually I'm not sure who's doing the sorting -- System.Reflection.Metadata also has logic to unstably sort the custom attribute table if they're not already sorted (CustomAttributeTableReader ctor).