Fix multiple enumerations when ordering setting sources

This was not spotted previously, because the base `Attribute` overrides
`Equals()` to have semantics similar to structs (per-field equality) by
using reflection. That masked the issue when strings were used, and
migrating to `LocalisableString` revealed it, as that struct's
implementation of equality currently uses instance checks.

Whether `LocalisableString.Equals()` is the correct implementation may
still be up for discussion, but allowing multiple enumeration is wrong
anyway, since the underlying enumerables are live (one especially is a
yield iterator, causing new object instances to be allocated).
This commit is contained in:
Bartłomiej Dach 2021-02-27 14:47:09 +01:00
parent 87b73da73e
commit 528de5869e
1 changed files with 5 additions and 2 deletions

View File

@ -139,9 +139,12 @@ public static IEnumerable<Drawable> CreateSettingsControls(this object obj)
public static IEnumerable<(SettingSourceAttribute, PropertyInfo)> GetOrderedSettingsSourceProperties(this object obj)
{
var original = obj.GetSettingsSourceProperties();
var original = obj.GetSettingsSourceProperties().ToArray();
var orderedRelative = original.Where(attr => attr.Item1.OrderPosition != null).OrderBy(attr => attr.Item1.OrderPosition);
var orderedRelative = original
.Where(attr => attr.Item1.OrderPosition != null)
.OrderBy(attr => attr.Item1.OrderPosition)
.ToArray();
var unordered = original.Except(orderedRelative);
return orderedRelative.Concat(unordered);