为什么 listbox1.Items.Add 使用我的对象的 Equals 方法?
我正在仔细研究 Windows 窗体 ListBoxCollection 添加方法, 在分析过程中我发现方法“add”调用了基础对象中的Equals方法 为什么这个方法要做这个动作?
我使用过“调用堆栈”,我找到了这个答案:
我们调用:System.Windows.Forms.ListBox.ObjectCollection.Add(object item)
,它将调用:System.Windows.Forms.ListBox.ObjectCollection.AddInternal(object item)
,它将调用: System.Windows.Forms.ListBox.NativeAdd(object item)
,它将调用: System.Windows.Forms.ListControl.GetItemText(object item)
,它将调用:System.Windows.Forms.Formatter.FormatObject(对象值,System.Type targetType,System.ComponentModel.TypeConverter sourceConverter,System.ComponentModel.TypeConverter targetConverter,字符串formatString,System.IFormatProvider formatInfo,对象formattedNullValue,对象dataSourceNullValue)
,它将调用:System.Windows.Forms.Formatter.IsNullData(object value, object dataSourceNullValue)
最后它将调用: object.Equals(object objA, object objB)
该方法使我的 Equal 方法也运行:
WindowsFormsApplication1.Student.Equals(object obj)
并且将发送到我的 Equals 方法(作为 obj)的最后一个参数是 DBNULL 对象! 什么是 DBNULL ?
i'm scrutinizing Windows Forms ListBoxCollection Add Method,
during the analyzing i found that the method "add" calls Equals method in base Object
why this method do this action ?
I Have used "Call Stack" and I have Find This Answer :
We Call :System.Windows.Forms.ListBox.ObjectCollection.Add(object item)
and it will Call : System.Windows.Forms.ListBox.ObjectCollection.AddInternal(object item)
and it will Call : System.Windows.Forms.ListBox.NativeAdd(object item)
and it will call : System.Windows.Forms.ListControl.GetItemText(object item)
and it will call : System.Windows.Forms.Formatter.FormatObject(object value, System.Type targetType, System.ComponentModel.TypeConverter sourceConverter, System.ComponentModel.TypeConverter targetConverter, string formatString, System.IFormatProvider formatInfo, object formattedNullValue, object dataSourceNullValue)
and it will call :System.Windows.Forms.Formatter.IsNullData(object value, object dataSourceNullValue)
and Finally it will call : object.Equals(object objA, object objB)
which This Method Make My Equal Method too Run :
WindowsFormsApplication1.Student.Equals(object obj)
and The Last Parameter which Will be Send to My Equals Method (as obj) is a DBNULL Object !
what is a DBNULL ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因为它需要能够知道哪个项目正在触发事件。如果这些对象是完全相同的对象,则选择、编辑、删除变得困难且混乱。
Because it needs to be able to know what item is firing events. If the objects are the exact same object, selection, editing, deletion becomes difficult and confusing.