【发布时间】:2012-01-02 17:09:28
【问题描述】:
我正在检查 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(object value, System.Type targetType, System.ComponentModel.TypeConverter sourceConverter, System.ComponentModel.TypeConverter targetConverter, string formatString, System.IFormatProvider formatInfo, object formattedNullValue , 对象 dataSourceNullValue)
它会调用 :System.Windows.Forms.Formatter.IsNullData(object value, object dataSourceNullValue)
最后它会调用:object.Equals(object objA, object objB)
which This Method Make My Equal Method too run :
WindowsFormsApplication1.Student.Equals(object obj)
并且将发送到我的 Equals 方法(作为 obj)的最后一个参数是 DBNULL 对象! 什么是 DBNULL?
【问题讨论】:
-
很难猜,Winforms 没有 ListBoxCollection 类。 ListBox.ObjectCollection.Add() 将是下一个可能的候选者,但它的 Add() 方法不会调用 Equals() 除非列表已排序。查看调用堆栈以了解它是如何到达那里的。