某些 Windows 窗体方法现在将针对 null 参数引发 ArgumentNullException,而之前它们会引发 NullReferenceException。
更改描述
以前,如果传递 null 参数,某些 Windows 窗体方法将引发 NullReferenceException。 从 .NET 5 开始,这些方法现将针对 null 参数引发 ArgumentNullException。
引发 ArgumentNullException 符合 .NET 运行时的行为。 它还通过清楚地指示参数为 null 和具体参数来改进调试体验。
引入的版本
.NET 5.0
建议操作
如果调用这些方法中的任何一种,你的代码当前都会针对 null 参数捕获 NullReferenceException,请改为捕获 ArgumentNullException。 此外,请考虑更新代码,以避免将 null 参数传递到列出的方法。
受影响的 API
下表列出了受影响的方法和参数:
方法 |
参数名称 |
新增的版本 |
Control.ControlCollection(Control) |
owner |
预览版 1 |
TabControl.GetToolTipText(Object) |
item |
预览版 1 |
TableLayoutControlCollection(TableLayoutPanel) |
container |
预览版 1 |
ToolStripRenderer.OnRenderArrow(ToolStripArrowRenderEventArgs) |
e |
预览版 1 |
ToolStripRenderer.OnRenderItemCheck(ToolStripItemImageRenderEventArgs) |
e |
预览版 1 |
ToolStripRenderer.OnRenderItemImage(ToolStripItemImageRenderEventArgs) |
e |
预览版 1 |
ToolStripRenderer.OnRenderItemText(ToolStripItemTextRenderEventArgs) |
e |
预览版 1 |
ToolStripRenderer.OnRenderStatusStripSizingGrip(ToolStripRenderEventArgs) > |
e |
预览版 1 |
DataGridViewComboBoxEditingControl.ApplyCellStyleToEditingControl(DataGridViewCellStyle) |
dataGridViewCellStyle |
预览版 2 |
RichTextBox.LoadFile(Stream, RichTextBoxStreamType) |
data |
预览版 2 |
ListBox.IntegerCollection(ListBox) |
owner |
预览版 5 |
ListBox.IntegerCollection.CopyTo(Array, Int32) |
destination |
预览版 5 |
ListViewGroup.ISerializable.GetObjectData(SerializationInfo, StreamingContext) |
info |
预览版 5 |
VisualStyleRenderer(String, Int32, Int32) |
className |
预览版 5 |
ListBox.ObjectCollection(ListBox) |
owner |
预览版 6 |
ListBox.ObjectCollection(ListBox, Object[]) |
owner ,value |
预览版 6 |
ListBox.ObjectCollection(ListBox, ListBox+ObjectCollection) |
owner ,value |
预览版 6 |
ListBox.ObjectCollection.AddRange(Object[]) |
items |
预览版 6 |
ListBox.ObjectCollection.AddRange(ListBox+ObjectCollection) |
value |
预览版 6 |
ListBox.ObjectCollection.CopyTo(Object[], Int32) |
destination |
预览版 6 |
ListBox.ObjectCollection.ICollection.CopyTo(Array, Int32) |
destination |
预览版 6 |
ListView.SelectedIndexCollection(ListView) |
owner |
预览版 7 |
TreeNodeCollection.Find(String, Boolean) |
key 为 null 或空 |
预览版 8 |
ListView.ListViewItemCollection.Find(String, Boolean) |
key 为 null 或空 |
RC1 |
ScrollableControl.OnPaintBackground(PaintEventArgs) |
e |
RC1 |