为什么 BindingList 添加成员时会抛出 System.ArgumentException
我有一个绑定列表,在某些难以重现的条件下,当向其中添加值时会抛出以下异常:
System.ArgumentException:Complex DataBinding 接受 IList 或 IListSource 作为数据源。 在 System.ComponentModel.ReflectPropertyDescriptor.SetValue(对象组件,对象值)
在 System.Windows.Forms.Binding.SetPropValue(对象值)
在 System.Windows.Forms.Binding.PushData(布尔力)
在 System.Windows.Forms.BindingManagerBase.PushData(Boolean& success)
在 System.Windows.Forms.BindingManagerBase.PushData()
在 System.Windows.Forms.CurrencyManager.CurrencyManager_PushData()
在 System.Windows.Forms.CurrencyManager.OnItemChanged(ItemChangedEventArgs e)
在 System.Windows.Forms.CurrencyManager.List_ListChanged(对象发送者,ListChangedEventArgs e)
在 System.ComponentModel.ListChangedEventHandler.Invoke(对象发送者,ListChangedEventArgs e)
在 System.ComponentModel.BindingList1.OnListChanged(ListChangedEventArgs e)
1.InsertItem(Int32索引,T项)
在System.ComponentModel.BindingList
在 System.Collections.ObjectModel.Collection`1.Add(T 项)
...
我可以理解它是否在创建列表或更改其中一个属性时发生,但只有在向列表添加某些值时才会发生这种情况。 它的声明如下:
private BindingList<IBusinessObject> _bindingList = new BindingList<IBusinessObject>();
然后像这样稍后使用(这是抛出异常的地方):
_bindingList.Add(myBusinessObject);
更糟糕的是,这种情况发生在以前工作的生产代码中,其中最近没有编辑过此处涉及的代码。 我怀疑这是某种微妙的数据交互问题,这也是它很少发生的原因,但坦率地说,我很困惑,因为异常的起源位于 .Net 内部深处。
更新: 它实际上不是单个表单,而是用于支持多个表单/对话框的对象模型的一部分。 这是非常复杂的代码,不是我编写的代码,只是维护/增强的代码。 深入研究代码后,看起来 BindingList 被添加到更多域对象中,并最终用于使用对象的字符串属性填充 ImageListBox。 有问题的字符串永远不应该为空或空白,但由于代码所在的位置以及难以重现附加调试器进行验证的错误将出现问题。 null 值是否会导致 BindingList 在距离问题实际所在位置如此遥远的位置引发异常? 该对象实际上被插入到 3 或 4 个其他对象/集合中,然后最终被分配为相关 ImageListBox 列表的数据源(即使这样,它也取决于其他几个选择,以确定最终哪个 BindingList 对象是用于填充列表)。
I've got a binding list, that under certain hard to reproduce conditions is throwing the following exception when a value is added to it:
System.ArgumentException: Complex DataBinding accepts as a data source either an IList or an IListSource.
at System.ComponentModel.ReflectPropertyDescriptor.SetValue(Object component, Object value)
at System.Windows.Forms.Binding.SetPropValue(Object value)
at System.Windows.Forms.Binding.PushData(Boolean force)
at System.Windows.Forms.BindingManagerBase.PushData(Boolean& success)
at System.Windows.Forms.BindingManagerBase.PushData()
at System.Windows.Forms.CurrencyManager.CurrencyManager_PushData()
at System.Windows.Forms.CurrencyManager.OnItemChanged(ItemChangedEventArgs e)
at System.Windows.Forms.CurrencyManager.List_ListChanged(Object sender, ListChangedEventArgs e)
at System.ComponentModel.ListChangedEventHandler.Invoke(Object sender, ListChangedEventArgs e)
at System.ComponentModel.BindingList1.OnListChanged(ListChangedEventArgs e)
1.InsertItem(Int32 index, T item)
at System.ComponentModel.BindingList
at System.Collections.ObjectModel.Collection`1.Add(T item)
...
I could understand if it was happening when creating the list, or changing one of the properties, but this is only happening when adding certain values to it. It's declared like follows:
private BindingList<IBusinessObject> _bindingList = new BindingList<IBusinessObject>();
And then used later like so (which is where the exception is being thrown):
_bindingList.Add(myBusinessObject);
To make matters worse this is occuring in previously working production code where none of the code involved here has been edited recently. I have a suspicion this is some sort of subtle data interaction issue which is also why it's happening infrequently, but frankly I'm at a loss as the origin point for the exception is deep inside the .Net internals.
Update:
It's not actually a single form, but rather a piece of the object model used to back several forms/dialogs. It's very complex code, and not code that I wrote but am merely maintaining/enhancing. After digging through the code, it looks like the BindingList gets added to some more domain objects and eventually used to populate a ImageListBox using a string property of the object. The string in question should never be null or blank, but because of where the code is located at and the difficultly in reproducing the error attaching a debugger to verify will be problematic. Could a null value cause the BindingList to throw an exception at such a distantly removed location from where the issue is actually located at? This object is literally inserted into 3 or 4 other objects/collections prior to eventually being assigned as the data source to the ImageListBox list in question (and even then it depends on a couple other selections as to exactly which one of the BindingList objects is eventually used to populate the list).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来您有某种类型的列表控件的 DataSource 属性绑定到 IBusinessObject 的属性,并且该属性返回的值作为 DataSource 无效。 您能否更具体地说明您在此表单上使用的所有绑定?
It looks like you have the DataSource property of a list control of some kind bound to a property of your IBusinessObject, and that property is returning a value that is not valid as a DataSource. Can you be more specific about all of the bindings you are using on this form?