8

我有一个自定义的 BindingList,我想为其创建一个自定义的 AddRange 方法。

public class MyBindingList<I> : BindingList<I>
{
    ...

    public void AddRange(IEnumerable<I> vals)
    {
        foreach (I v in vals)
            Add(v);
    }
}

我的问题是大型集合的性能很糟糕。我现在正在调试的案例是尝试添加大约 30,000 条记录,并且花费了不可接受的时间。

在线查看此问题后,似乎问题在于Add每次添加时都会调整数组的大小。我认为这个答案总结为:

如果您使用的是 Add,它会根据需要逐渐调整内部数组的大小(加倍)

我可以在我的自定义AddRange实现中做些什么来指定 BindingList 需要根据项目计数调整大小的大小,而不是让它在添加每个项目时不断地重新分配数组?

4

2 回答 2

8

CSharpie 在他的回答中解释说,性能不佳是由于ListChanged每次触发 -event造成的Add,并展示了一种AddRange为您的 custom实现的方法BindingList

另一种方法是将AddRange功能实现为BindingList<T>. 基于 CSharpies 实现:

/// <summary>
/// Extension methods for <see cref="System.ComponentModel.BindingList{T}"/>.
/// </summary>
public static class BindingListExtensions
{
  /// <summary>
  /// Adds the elements of the specified collection to the end of the <see cref="System.ComponentModel.BindingList{T}"/>,
  /// while only firing the <see cref="System.ComponentModel.BindingList{T}.ListChanged"/>-event once.
  /// </summary>
  /// <typeparam name="T">
  /// The type T of the values of the <see cref="System.ComponentModel.BindingList{T}"/>.
  /// </typeparam>
  /// <param name="bindingList">
  /// The <see cref="System.ComponentModel.BindingList{T}"/> to which the values shall be added.
  /// </param>
  /// <param name="collection">
  /// The collection whose elements should be added to the end of the <see cref="System.ComponentModel.BindingList{T}"/>.
  /// The collection itself cannot be null, but it can contain elements that are null,
  /// if type T is a reference type.
  /// </param>
  /// <exception cref="ArgumentNullException">values is null.</exception>
  public static void AddRange<T>(this System.ComponentModel.BindingList<T> bindingList, IEnumerable<T> collection)
  {
    // The given collection may not be null.
    if (collection == null)
      throw new ArgumentNullException(nameof(collection));

    // Remember the current setting for RaiseListChangedEvents
    // (if it was already deactivated, we shouldn't activate it after adding!).
    var oldRaiseEventsValue = bindingList.RaiseListChangedEvents;

    // Try adding all of the elements to the binding list.
    try
    {
      bindingList.RaiseListChangedEvents = false;

      foreach (var value in collection)
        bindingList.Add(value);
    }

    // Restore the old setting for RaiseListChangedEvents (even if there was an exception),
    // and fire the ListChanged-event once (if RaiseListChangedEvents is activated).
    finally
    {
      bindingList.RaiseListChangedEvents = oldRaiseEventsValue;

      if (bindingList.RaiseListChangedEvents)
        bindingList.ResetBindings();
    }
  }
}

这样,根据您的需要,您甚至可能不需要编写自己的BindingList-subclass。

于 2017-11-29T10:04:28.220 回答
7

您可以在构造函数中传入 List 并使用List<T>.Capacity.

但我敢打赌,最显着的加速将来自添加范围时的暂停事件。所以我在我的示例代码中包含了这两件事。

可能需要一些微调来处理一些最坏的情况,而不是什么。

public class MyBindingList<I> : BindingList<I>
{
    private readonly List<I> _baseList;

    public MyBindingList() : this(new List<I>())
    {

    }

    public MyBindingList(List<I> baseList) : base(baseList)
    {
        if(baseList == null)
            throw new ArgumentNullException();            
        _baseList = baseList;
    }

    public void AddRange(IEnumerable<I> vals)
    {
        ICollection<I> collection = vals as ICollection<I>;
        if (collection != null)
        {
            int requiredCapacity = Count + collection.Count;
            if (requiredCapacity > _baseList.Capacity)
                _baseList.Capacity = requiredCapacity;
        }

        bool restore = RaiseListChangedEvents;
        try
        {
            RaiseListChangedEvents = false;
            foreach (I v in vals)
                Add(v); // We cant call _baseList.Add, otherwise Events wont get hooked.
        }
        finally
        {
            RaiseListChangedEvents = restore;
            if (RaiseListChangedEvents)
                ResetBindings();
        }
    }
}

你不能使用_baseList.AddRange因为BindingList<T>不会挂钩 PropertyChanged 事件。您可以通过在 AddRange之后为每个项目调用私有方法HookPropertyChanged来绕过此问题。vals然而,这只有在(您的方法参数)是一个集合时才有意义。否则,您将冒着枚举可枚举两次的风险。

这是您无需编写自己的 BindingList 即可获得的最接近“最佳”的方法。这不应该太难,因为您可以从 BindingList 复制源代码并根据需要更改部分。

于 2017-04-10T19:31:10.263 回答