某些 Windows 窗体方法现在将针对 null 参数引发 ArgumentNullException,而之前它们会引发 NullReferenceException。
更改描述
以前,某些 Windows 窗体方法在传递为 null 的参数时抛出一个 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 |