- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有一个自定义 BindingList,我想为其创建自定义 AddRange 方法。
public class MyBindingList<I> : BindingList<I>
{
...
public void AddRange(IEnumerable<I> vals)
{
foreach (I v in vals)
Add(v);
}
}
我的问题是大型集合的性能很糟糕。我现在正在调试的案例是尝试添加大约 30,000 条记录,并且花费了无法接受的时间。
在线查看此问题后,问题似乎在于使用 Add
会在每次添加时调整数组的大小。 This answer我认为总结如下:
If you are using Add, it is resizing the inner array gradually as needed (doubling)
我可以在我的自定义 AddRange
实现中做什么来指定 BindingList 需要根据项目计数调整大小,而不是让它在添加每个项目时不断地重新分配数组?
最佳答案
CSharpie 在他的 answer 中解释道性能不佳是由于 ListChanged
-事件在每个 Add
之后触发,并展示了一种实现方式 AddRange
为您定制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
-子类。
关于c# - 如何提高自定义 BindingList 上 AddRange 方法的性能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43331145/
我在家里的电脑上玩 Entity Framework 6,决定尝试插入相当多的行,大约 430k。 我的第一次尝试是这样的,是的,我知道它可以做得更好,但无论如何都是为了研究: var watch =
这个问题在这里已经有了答案: Nested object initializer syntax (1 个回答) 关闭 5 年前。 有一个 C# 功能,我不知道它的术语,因此无法找到关于它的文档。 允
我有一些遗留的 C# 代码,其中包含以下部分: private List mList = new List(); public List getList() { List list = new
通用列表中是否有内置函数可以从特定索引中的另一个列表中添加范围,还是我必须自己编写? 例如: List list1 = new List(); List list2 = new List(); lis
在下面的代码中: var cats = new List() {"cat1", "cat2"}; var dogs = new List() {"dog1", "dog2"}; var animals
具有讽刺意味的是,在将一堆旧 Arraylist 转换为使用通用 List(Of foo) 集合时,为了提高类型安全性,我在 List.AddRange() 中遇到了意外行为方法。 鉴于以下代码,我预
项目:我有一个包含 ComboBox 和 FlowLayoutPanel 的父面板。 FlowLayoutPanel 包含数量可变的子面板(继承自 UserControl 的自定义控件)。每个子面板包
我正在为 ObservableCollection 编写一个扩展方法,并且已了解到 .Add 函数每次调用都会引发 3 个属性更改事件, 所以这样的事情是一个坏主意: public static vo
这个问题在这里已经有了答案: Selection.addRange() is deprecated and will be removed from Chrome (1 个回答) 关闭 5 年前。
基本上我正在寻找这样的东西...... cbo_Genre.Items.AddRange({"Horror", "Comedy"}); 最佳答案 看看这个。 ComboBox1.Items.AddRa
如果我有这两个类: class A {} class B : A {} 我制作了一个 List 但我想通过调用 List.AddRange(List) 添加一个 List 但编译器拒绝: Argume
比如说,我有 3 个列表 List l1 List l1,l2,l3 所有 3 个列表都有很多项目我想将它们全部添加到一个列表中 List finalList finalList.AddRange(l
我坚信 ToolStripItemCollection.AddRange 的实现是一个错误: 我制作了带有两个菜单的 Windows 窗体应用程序,每个菜单包含 2 个项目。 first menu s
此问题中的代码生成以下 JSON。 代码应该根据 .Where(a => a.Values != null) 行排除空的“子”键,但它不起作用。 我可以在哪里放置 where 子句,以便 JSON 不
在HttpWebRequest 的AddRange 方法中,我们可以指定要下载的字节范围。我想知道的是如何指定从某个偏移量到文件末尾的范围。 比如如果我们不知道文件的长度,我们可以在Request H
我已经安装了 Connector/NET 6.5.4。在 VisualStutio 中,IntelliSense 建议使用一种名为 MySqlParameterCollection.AddRange
这似乎是一个简单的问题,但对我来说不是,而且搜索也没有结果。到目前为止,我唯一完成的 .net 编程是使用 Delphi Prism。使用 Prism,我可以执行以下操作: var l := new
“源数组不够长。检查 srcIndex 和长度,以及数组的下限。” 我在创建目标列表(在我添加对象的地方)和源列表(从我在目标列表中添加对象的地方)时没有提到任何大小 这是我正在做的类似代码。 Lis
namespace SubscriptionWebsite.PlaceHolders { public class TreeData { public int ID {
我想做的是 FreezableCollection.AddRange(collectionToAdd) 每次我添加到 FreezableCollection 时都会引发一个事件并发生一些事情。现在我有
我是一名优秀的程序员,十分优秀!