Ejemplo n.º 1
0
        /// <summary>
        /// Sort the <code>MBBindingList</code> using a stable sort, instead of the standard unstable sort (which screws up hero orderings once in a while).
        /// Uses 2 pieces of reflection to first, get the internal <code>List</code> of the MBBindingList,
        /// and afterward get the internal <code>_items</code> array of the List.
        ///
        /// This is not optimal and VERY fragile, Enumerable would be preferable, but for some reason it seems to freeze the game
        /// whenever we try to .. enumerate the enumeration.
        /// </summary>
        /// <param name="list"></param>
        /// <param name="comparer"></param>
        public static void StableSort <T>(this MBBindingList <T> list, IComparer <T> comparer)
        {
            if (list.IsOrdered(comparer))
            {
                return;
            }

            var traverse     = Traverse.Create(list);
            var internalList = traverse.Field <List <T> >("_list").Value;

            internalList.StableSort(comparer);
            traverse.Method("FireListChanged", new[] { typeof(ListChangedType), typeof(int) },
                            new object[] { ListChangedType.Sorted, -1 }).GetValue();
        }