From fa3e0a2f06b5712da4611ff0e4a78a408e80af44 Mon Sep 17 00:00:00 2001 From: Lakshan Fernando Date: Thu, 11 Apr 2024 14:55:21 -0700 Subject: [PATCH 01/14] Type registration experiment --- .../src/System/ComponentModel/TypeConverterHelper.cs | 2 +- .../Windows/Forms/Controls/ListControl/ListControl.cs | 2 +- .../Windows/Forms/Controls/ToolStrips/OpacityConverter.cs | 2 +- .../Forms/Controls/ToolStrips/ToolStripMenuItem.cs | 2 +- .../src/System/Windows/Forms/Internal/Formatter.cs | 6 ++---- .../src/System/Windows/Forms/MDI/MDIWindowDialog.cs | 8 ++++---- 6 files changed, 10 insertions(+), 12 deletions(-) diff --git a/src/System.Windows.Forms.Primitives/src/System/ComponentModel/TypeConverterHelper.cs b/src/System.Windows.Forms.Primitives/src/System/ComponentModel/TypeConverterHelper.cs index 5ea877d26b..e2f473e412 100644 --- a/src/System.Windows.Forms.Primitives/src/System/ComponentModel/TypeConverterHelper.cs +++ b/src/System.Windows.Forms.Primitives/src/System/ComponentModel/TypeConverterHelper.cs @@ -28,7 +28,7 @@ internal static class TypeConverterHelper return false; } - TypeConverter converter = TypeDescriptor.GetConverter(typeof(T)); + TypeConverter converter = TypeDescriptor.GetConverterFromKnownType(typeof(T)); for (int i = 0; i < output.Length; i++) { // Note: ConvertFromString will raise exception if value cannot be converted. diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/Controls/ListControl/ListControl.cs b/src/System.Windows.Forms/src/System/Windows/Forms/Controls/ListControl/ListControl.cs index cbc24978dd..eae848dd37 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/Controls/ListControl/ListControl.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/Controls/ListControl/ListControl.cs @@ -527,7 +527,7 @@ public abstract class ListControl : Control } // Try Formatter.FormatObject - s_stringTypeConverter ??= TypeDescriptor.GetConverter(typeof(string)); + s_stringTypeConverter ??= TypeDescriptor.GetConverterFromKnownType(typeof(string)); try { diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/Controls/ToolStrips/OpacityConverter.cs b/src/System.Windows.Forms/src/System/Windows/Forms/Controls/ToolStrips/OpacityConverter.cs index a8b00bf80d..84a7514b39 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/Controls/ToolStrips/OpacityConverter.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/Controls/ToolStrips/OpacityConverter.cs @@ -45,7 +45,7 @@ public class OpacityConverter : TypeConverter double percent; try { - percent = (double)TypeDescriptor.GetConverter(typeof(double)).ConvertFrom(context, culture, text)!; + percent = (double)TypeDescriptor.GetConverterFromKnownType(typeof(double)).ConvertFrom(context, culture, text)!; // Assume they meant a percentage if it is > 1.0, else they actually // typed the correct double. diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/Controls/ToolStrips/ToolStripMenuItem.cs b/src/System.Windows.Forms/src/System/Windows/Forms/Controls/ToolStrips/ToolStripMenuItem.cs index ad48f03ebc..62c1fd7303 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/Controls/ToolStrips/ToolStripMenuItem.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/Controls/ToolStrips/ToolStripMenuItem.cs @@ -1108,7 +1108,7 @@ public partial class ToolStripMenuItem : ToolStripDropDownItem return string.Empty; } - return TypeDescriptor.GetConverter(typeof(Keys)).ConvertToString(context: null, CultureInfo.CurrentUICulture, shortcutKeys); + return TypeDescriptor.GetConverterFromKnownType(typeof(Keys)).ConvertToString(context: null, CultureInfo.CurrentUICulture, shortcutKeys); } internal override bool IsBeingTabbedTo() diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/Internal/Formatter.cs b/src/System.Windows.Forms/src/System/Windows/Forms/Internal/Formatter.cs index 6174cbafaf..410758b3d9 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/Internal/Formatter.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/Internal/Formatter.cs @@ -26,7 +26,6 @@ internal static class Formatter /// If the caller is expecting a nullable value back, we must also re-wrap the final result /// inside a nullable value before returning. /// - [RequiresUnreferencedCode(ComponentModelTrimIncompatibilityMessage)] public static object? FormatObject( object? value, Type targetType, @@ -84,7 +83,6 @@ internal static class Formatter /// - Uses TypeConverters or IConvertible where appropriate /// - Throws a FormatException is no suitable conversion can be found /// - [RequiresUnreferencedCode(ComponentModelTrimIncompatibilityMessage)] private static object? FormatObjectInternal( object? value, Type targetType, @@ -137,13 +135,13 @@ internal static class Formatter // The converters for properties should take precedence. Unfortunately, we don't know whether we have one. Check vs. the // type's TypeConverter. We're punting the case where the property-provided converter is the same as the type's converter. Type sourceType = value.GetType(); - TypeConverter sourceTypeTypeConverter = TypeDescriptor.GetConverter(sourceType); + TypeConverter sourceTypeTypeConverter = TypeDescriptor.GetConverterFromKnownType(sourceType); if (sourceConverter is not null && sourceConverter != sourceTypeTypeConverter && sourceConverter.CanConvertTo(targetType)) { return sourceConverter.ConvertTo(context: null, GetFormatterCulture(formatInfo), value, targetType); } - TypeConverter targetTypeTypeConverter = TypeDescriptor.GetConverter(targetType); + TypeConverter targetTypeTypeConverter = TypeDescriptor.GetConverterFromKnownType(targetType); if (targetConverter is not null && targetConverter != targetTypeTypeConverter && targetConverter.CanConvertFrom(sourceType)) { return targetConverter.ConvertFrom(context: null, GetFormatterCulture(formatInfo), value); diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/MDI/MDIWindowDialog.cs b/src/System.Windows.Forms/src/System/Windows/Forms/MDI/MDIWindowDialog.cs index 2904f9e405..e35a7a9789 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/MDI/MDIWindowDialog.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/MDI/MDIWindowDialog.cs @@ -86,20 +86,20 @@ internal sealed partial class MdiWindowDialog : Form // // _itemList // - resources.ApplyResources(_itemList, "itemList"); + resources.ApplyResources(_itemList, "itemList", null); _itemList.FormattingEnabled = true; _itemList.Name = "itemList"; // // _okButton // - resources.ApplyResources(_okButton, "okButton"); + resources.ApplyResources