From 6adc61332e6288e797a309a5c21858ee5e65dd1d Mon Sep 17 00:00:00 2001 From: Lachlan Ennis <2433737+elachlan@users.noreply.github.com> Date: Thu, 24 Nov 2022 10:41:03 +1000 Subject: [PATCH 1/4] Refactor GetStandardValues implementations to remove ArrayList --- ...CodeDomDesignerLoader.ModifierConverter.cs | 2 +- .../System/Windows/Forms/CursorConverter.cs | 11 +++++--- .../Forms/MdiWindowListItemConverter.cs | 12 +++------ .../Forms/SpecialFolderEnumConverter.cs | 26 ++++++++++--------- .../TextBoxAutoCompleteSourceConverter.cs | 22 ++++++++-------- 5 files changed, 37 insertions(+), 36 deletions(-) diff --git a/src/System.Windows.Forms.Design/src/System/ComponentModel/Design/Serialization/CodeDomDesignerLoader.ModifierConverter.cs b/src/System.Windows.Forms.Design/src/System/ComponentModel/Design/Serialization/CodeDomDesignerLoader.ModifierConverter.cs index 36cc7569530..95ab2075c7b 100644 --- a/src/System.Windows.Forms.Design/src/System/ComponentModel/Design/Serialization/CodeDomDesignerLoader.ModifierConverter.cs +++ b/src/System.Windows.Forms.Design/src/System/ComponentModel/Design/Serialization/CodeDomDesignerLoader.ModifierConverter.cs @@ -119,7 +119,7 @@ public override StandardValuesCollection GetStandardValues(ITypeDescriptorContex if (needMassage) { - ArrayList list = new ArrayList(values.Count); + List list = new(values.Count); foreach (MemberAttributes value in values) { diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/CursorConverter.cs b/src/System.Windows.Forms/src/System/Windows/Forms/CursorConverter.cs index f141cae9aab..3fb3f285ddc 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/CursorConverter.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/CursorConverter.cs @@ -164,17 +164,20 @@ public override StandardValuesCollection GetStandardValues(ITypeDescriptorContex { if (_values is null) { - ArrayList list = new ArrayList(); + List list = new(); PropertyInfo[] props = GetProperties(); for (int i = 0; i < props.Length; i++) { PropertyInfo prop = props[i]; object[]? tempIndex = null; - Debug.Assert(prop.GetValue(null, tempIndex) is not null, "Property " + prop.Name + " returned NULL"); - list.Add(prop.GetValue(null, tempIndex)); + Debug.Assert(prop.GetValue(null, tempIndex) is not null, $"Property {prop.Name} returned NULL"); + if (prop.GetValue(null, tempIndex) is object item) + { + list.Add(item); + } } - _values = new StandardValuesCollection(list.ToArray()); + _values = new StandardValuesCollection(list); } return _values; diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/MdiWindowListItemConverter.cs b/src/System.Windows.Forms/src/System/Windows/Forms/MdiWindowListItemConverter.cs index 5ffcea409be..32f99d66e6e 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/MdiWindowListItemConverter.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/MdiWindowListItemConverter.cs @@ -2,9 +2,6 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -#nullable disable - -using System.Collections; using System.ComponentModel; namespace System.Windows.Forms @@ -19,14 +16,13 @@ public MdiWindowListItemConverter(Type type) : base(type) /// Gets a collection of standard values for the data type this validator is /// designed for. /// - public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context) + public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext? context) { - if (context.Instance is MenuStrip menu) + if (context?.Instance is MenuStrip menu) { StandardValuesCollection values = base.GetStandardValues(context); - ArrayList list = new ArrayList(); - int count = values.Count; - for (int i = 0; i < count; i++) + List list = new(); + for (int i = 0; i < values.Count; i++) { if (values[i] is ToolStripItem currentItem && currentItem.Owner == menu) { diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/SpecialFolderEnumConverter.cs b/src/System.Windows.Forms/src/System/Windows/Forms/SpecialFolderEnumConverter.cs index 00d2b99af85..4cf855feed8 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/SpecialFolderEnumConverter.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/SpecialFolderEnumConverter.cs @@ -23,23 +23,25 @@ public SpecialFolderEnumConverter( public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext? context) { StandardValuesCollection values = base.GetStandardValues(context); - var list = new ArrayList(); - int count = values.Count; + List list = new(); bool personalSeen = false; - for (int i = 0; i < count; i++) + for (int i = 0; i < values.Count; i++) { - if (values[i] is Environment.SpecialFolder specialFolder && - specialFolder.Equals(Environment.SpecialFolder.Personal)) + if (values[i] is object currentItem) { - if (!personalSeen) + if (currentItem is Environment.SpecialFolder specialFolder && + specialFolder.Equals(Environment.SpecialFolder.Personal)) { - personalSeen = true; - list.Add(values[i]); + if (!personalSeen) + { + personalSeen = true; + list.Add(currentItem); + } + } + else + { + list.Add(currentItem); } - } - else - { - list.Add(values[i]); } } diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/TextBoxAutoCompleteSourceConverter.cs b/src/System.Windows.Forms/src/System/Windows/Forms/TextBoxAutoCompleteSourceConverter.cs index 1f010a0cc13..a804056ed69 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/TextBoxAutoCompleteSourceConverter.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/TextBoxAutoCompleteSourceConverter.cs @@ -2,11 +2,8 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -#nullable disable - -using System.Collections; -using System.Diagnostics.CodeAnalysis; using System.ComponentModel; +using System.Diagnostics.CodeAnalysis; namespace System.Windows.Forms { @@ -22,17 +19,20 @@ public TextBoxAutoCompleteSourceConverter( /// Gets a collection of standard values for the data type this validator is /// designed for. /// - public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context) + public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext? context) { + const string ListItems = "ListItems"; StandardValuesCollection values = base.GetStandardValues(context); - ArrayList list = new ArrayList(); - int count = values.Count; - for (int i = 0; i < count; i++) + List list = new(); + for (int i = 0; i < values.Count; i++) { - string currentItemText = values[i].ToString(); - if (!currentItemText.Equals("ListItems")) + if (values[i] is object currentItem) { - list.Add(values[i]); + string? currentItemText = currentItem.ToString(); + if (!string.IsNullOrEmpty(currentItemText) && !currentItemText.Equals(ListItems)) + { + list.Add(currentItem); + } } } From fa8480ac586da3badad8d3e55c5c12c032551d37 Mon Sep 17 00:00:00 2001 From: Lachlan Ennis <2433737+elachlan@users.noreply.github.com> Date: Thu, 24 Nov 2022 11:21:33 +1000 Subject: [PATCH 2/4] fix usings --- .../src/System/Windows/Forms/CursorConverter.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/CursorConverter.cs b/src/System.Windows.Forms/src/System/Windows/Forms/CursorConverter.cs index 3fb3f285ddc..247e97b64cc 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/CursorConverter.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/CursorConverter.cs @@ -2,7 +2,6 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -using System.Collections; using System.ComponentModel; using System.ComponentModel.Design.Serialization; using System.Diagnostics; From 82dd58dc789985f555d38401237e1e571ef9b2bc Mon Sep 17 00:00:00 2001 From: Lachlan Ennis <2433737+elachlan@users.noreply.github.com> Date: Wed, 30 Nov 2022 08:04:05 +1000 Subject: [PATCH 3/4] changes from review --- .../Forms/SpecialFolderEnumConverter.cs | 22 ++++++++++--------- .../TextBoxAutoCompleteSourceConverter.cs | 4 +--- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/SpecialFolderEnumConverter.cs b/src/System.Windows.Forms/src/System/Windows/Forms/SpecialFolderEnumConverter.cs index 4cf855feed8..76d6d00d047 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/SpecialFolderEnumConverter.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/SpecialFolderEnumConverter.cs @@ -27,22 +27,24 @@ public override StandardValuesCollection GetStandardValues(ITypeDescriptorContex bool personalSeen = false; for (int i = 0; i < values.Count; i++) { - if (values[i] is object currentItem) + if (values[i] is not object currentItem) { - if (currentItem is Environment.SpecialFolder specialFolder && + continue; + } + + if (currentItem is Environment.SpecialFolder specialFolder && specialFolder.Equals(Environment.SpecialFolder.Personal)) + { + if (!personalSeen) { - if (!personalSeen) - { - personalSeen = true; - list.Add(currentItem); - } - } - else - { + personalSeen = true; list.Add(currentItem); } } + else + { + list.Add(currentItem); + } } return new StandardValuesCollection(list); diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/TextBoxAutoCompleteSourceConverter.cs b/src/System.Windows.Forms/src/System/Windows/Forms/TextBoxAutoCompleteSourceConverter.cs index a804056ed69..54e76e5e24a 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/TextBoxAutoCompleteSourceConverter.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/TextBoxAutoCompleteSourceConverter.cs @@ -21,15 +21,13 @@ public TextBoxAutoCompleteSourceConverter( /// public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext? context) { - const string ListItems = "ListItems"; StandardValuesCollection values = base.GetStandardValues(context); List list = new(); for (int i = 0; i < values.Count; i++) { if (values[i] is object currentItem) { - string? currentItemText = currentItem.ToString(); - if (!string.IsNullOrEmpty(currentItemText) && !currentItemText.Equals(ListItems)) + if (string.Equals(currentItem.ToString(), "ListItems")) { list.Add(currentItem); } From 812255d2db404d44735eb62c8960742dc3fd43b7 Mon Sep 17 00:00:00 2001 From: Lachlan Ennis <2433737+elachlan@users.noreply.github.com> Date: Thu, 1 Dec 2022 09:46:14 +1000 Subject: [PATCH 4/4] changes from review --- .../Windows/Forms/SpecialFolderEnumConverter.cs | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/SpecialFolderEnumConverter.cs b/src/System.Windows.Forms/src/System/Windows/Forms/SpecialFolderEnumConverter.cs index 76d6d00d047..197b737483e 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/SpecialFolderEnumConverter.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/SpecialFolderEnumConverter.cs @@ -5,6 +5,7 @@ using System.Collections; using System.Diagnostics.CodeAnalysis; using System.ComponentModel; +using System.Diagnostics; namespace System.Windows.Forms { @@ -27,11 +28,7 @@ public override StandardValuesCollection GetStandardValues(ITypeDescriptorContex bool personalSeen = false; for (int i = 0; i < values.Count; i++) { - if (values[i] is not object currentItem) - { - continue; - } - + object? currentItem = values[i]; if (currentItem is Environment.SpecialFolder specialFolder && specialFolder.Equals(Environment.SpecialFolder.Personal)) { @@ -43,7 +40,11 @@ public override StandardValuesCollection GetStandardValues(ITypeDescriptorContex } else { - list.Add(currentItem); + Debug.Assert(currentItem is not null); + if (currentItem is not null) + { + list.Add(currentItem); + } } }