From ab0b3b2e2b3eba290da38a8a62283865d0e4365b Mon Sep 17 00:00:00 2001 From: Bellamy Date: Wed, 3 May 2023 10:32:26 +0100 Subject: [PATCH 1/5] Remove dead code from DateTimeConverter2 Update DateTimeConverter2.cs --- .../Xaml/Replacements/DateTimeConverter2.cs | 27 +++----- .../DateTimeValueSerializerContext.cs | 60 ----------------- .../Windows/Markup/DateTimeConverter2.cs | 25 ++------ .../Markup/DateTimeValueSerializerContext.cs | 64 ------------------- .../src/WindowsBase/WindowsBase.csproj | 1 - 5 files changed, 16 insertions(+), 161 deletions(-) delete mode 100644 src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/Replacements/DateTimeValueSerializerContext.cs delete mode 100644 src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/Markup/DateTimeValueSerializerContext.cs diff --git a/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/Replacements/DateTimeConverter2.cs b/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/Replacements/DateTimeConverter2.cs index a61f85d827a..349af986ed1 100644 --- a/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/Replacements/DateTimeConverter2.cs +++ b/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/Replacements/DateTimeConverter2.cs @@ -8,16 +8,10 @@ namespace System.Xaml.Replacements { - - //+-------------------------------------------------------------------------------------- - // - // DateTimeConverter2 - // - // This internal class simply wraps the DateTimeValueSerializer, to make it compatible with - // internal code that expects a type converter. - // - //+-------------------------------------------------------------------------------------- - + /// + /// This internal class simply wraps the DateTimeValueSerializer, to make it compatible with + /// internal code that expects a type converter. + /// internal class DateTimeConverter2 : TypeConverter { public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) @@ -32,30 +26,27 @@ public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceT public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) { - if (destinationType == typeof(string)) + if (destinationType == typeof(string)) { return true; } - + return base.CanConvertTo(context, destinationType); } public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) - { - return _dateTimeValueSerializer.ConvertFromString( value as string, _valueSerializerContext ); - } + => _dateTimeValueSerializer.ConvertFromString(value as string, null); public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) { if (destinationType == typeof(string) && value is DateTime) { - return _dateTimeValueSerializer.ConvertToString(value, _valueSerializerContext); + return _dateTimeValueSerializer.ConvertToString(value, null); } return base.ConvertTo(context, culture, value, destinationType); } - private DateTimeValueSerializer _dateTimeValueSerializer = new DateTimeValueSerializer(); - private IValueSerializerContext _valueSerializerContext = new DateTimeValueSerializerContext(); + private readonly DateTimeValueSerializer _dateTimeValueSerializer = new DateTimeValueSerializer(); } } diff --git a/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/Replacements/DateTimeValueSerializerContext.cs b/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/Replacements/DateTimeValueSerializerContext.cs deleted file mode 100644 index 2ddbb0b1996..00000000000 --- a/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/Replacements/DateTimeValueSerializerContext.cs +++ /dev/null @@ -1,60 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// 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.ComponentModel; -using System.Windows.Markup; - -namespace System.Xaml.Replacements -{ - - // This is a helper class used by the DateTimeConverter2 to call the DateTimeValueSerializer. - // It provides no functionality. - - internal class DateTimeValueSerializerContext : IValueSerializerContext - { - public ValueSerializer GetValueSerializerFor(PropertyDescriptor descriptor) - { - return null; - } - - public ValueSerializer GetValueSerializerFor(Type type) - { - return null; - } - - - public IContainer Container - { - get { return null; } - } - - public object Instance - { - get { return null; } - } - - public void OnComponentChanged() - { - } - - public bool OnComponentChanging() - { - return false; - } - - public PropertyDescriptor PropertyDescriptor - { - get { return null; } - } - - public object GetService(Type serviceType) - { - return null; - } - - } - - -} - diff --git a/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/Markup/DateTimeConverter2.cs b/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/Markup/DateTimeConverter2.cs index a02aa9c5a22..b711dbec069 100644 --- a/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/Markup/DateTimeConverter2.cs +++ b/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/Markup/DateTimeConverter2.cs @@ -13,15 +13,10 @@ namespace MS.Internal.Markup namespace System.Windows.Markup #endif { - //+-------------------------------------------------------------------------------------- - // - // DateTimeConverter2 - // - // This internal class simply wraps the DateTimeValueSerializer, to make it compatible with - // internal code that expects a type converter. - // - //+-------------------------------------------------------------------------------------- - + /// + /// This internal class simply wraps the DateTimeValueSerializer, to make it compatible with + /// internal code that expects a type converter. + /// internal class DateTimeConverter2 : TypeConverter { #if !PBTCOMPILER @@ -45,27 +40,21 @@ public override bool CanConvertTo(ITypeDescriptorContext context, Type destinati return base.CanConvertTo(context, destinationType); } - public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) - { - return _dateTimeValueSerializer.ConvertFromString( value as string, _valueSerializerContext ); - } - - + => _dateTimeValueSerializer.ConvertFromString( value as string, null ); public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) { if (destinationType != null && value is DateTime) { - _dateTimeValueSerializer.ConvertToString( value as string, _valueSerializerContext ); + return _dateTimeValueSerializer.ConvertToString(value as string, null); } return base.ConvertTo(context, culture, value, destinationType); } - private DateTimeValueSerializer _dateTimeValueSerializer = new DateTimeValueSerializer(); - private IValueSerializerContext _valueSerializerContext = new DateTimeValueSerializerContext(); + private readonly DateTimeValueSerializer _dateTimeValueSerializer = new DateTimeValueSerializer(); #endif } } diff --git a/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/Markup/DateTimeValueSerializerContext.cs b/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/Markup/DateTimeValueSerializerContext.cs deleted file mode 100644 index ddb6abb5abf..00000000000 --- a/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/Markup/DateTimeValueSerializerContext.cs +++ /dev/null @@ -1,64 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// 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.Globalization; -using System.Text; -using System.ComponentModel; - -namespace System.Windows.Markup -{ - // This is a helper class used by the DateTimeConverter2 to call the DateTimeValueSerializer. - // It provides no functionality. - - internal class DateTimeValueSerializerContext : IValueSerializerContext - { - public ValueSerializer GetValueSerializerFor(PropertyDescriptor descriptor) - { - return null; - } - - public ValueSerializer GetValueSerializerFor(Type type) - { - return null; - } - - - public IContainer Container - { - get { return null; } - } - - public object Instance - { - get { return null; } - } - - public void OnComponentChanged() - { - } - - public bool OnComponentChanging() - { - return false; - } - - public PropertyDescriptor PropertyDescriptor - { - get { return null; } - } - - public object GetService(Type serviceType) - { - return null; - } -} -} - diff --git a/src/Microsoft.DotNet.Wpf/src/WindowsBase/WindowsBase.csproj b/src/Microsoft.DotNet.Wpf/src/WindowsBase/WindowsBase.csproj index e6155312cbd..e9080daa106 100644 --- a/src/Microsoft.DotNet.Wpf/src/WindowsBase/WindowsBase.csproj +++ b/src/Microsoft.DotNet.Wpf/src/WindowsBase/WindowsBase.csproj @@ -249,7 +249,6 @@ - From a3c0fbaf683714bb8b7e06f6231732763909d32a Mon Sep 17 00:00:00 2001 From: Bellamy Date: Wed, 3 May 2023 10:34:10 +0100 Subject: [PATCH 2/5] Cleanup DateTimeOffsetConverter2 - Remove dead `if (constructor != null)` check - this ctor will always be there. - Cleanup code --- .../Replacements/DateTimeOffsetConverter2.cs | 62 +++++++------------ 1 file changed, 23 insertions(+), 39 deletions(-) diff --git a/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/Replacements/DateTimeOffsetConverter2.cs b/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/Replacements/DateTimeOffsetConverter2.cs index e2db25cbb93..31a45b08c32 100644 --- a/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/Replacements/DateTimeOffsetConverter2.cs +++ b/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/Replacements/DateTimeOffsetConverter2.cs @@ -8,9 +8,9 @@ using System.Reflection; using System.Security; -namespace System.Windows.Markup +namespace System.Xaml.Replacements { - class DateTimeOffsetConverter2 : TypeConverter + internal class DateTimeOffsetConverter2 : TypeConverter { public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) { @@ -18,6 +18,7 @@ public override bool CanConvertTo(ITypeDescriptorContext context, Type destinati { return true; } + return base.CanConvertTo(context, destinationType); } @@ -27,42 +28,32 @@ public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceT { return true; } + return base.CanConvertFrom(context, sourceType); } public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) { - if ((destinationType == typeof(string)) && (value is DateTimeOffset)) + if (value is DateTimeOffset dtOffset) { - if (culture == null) + if (destinationType == typeof(string)) { - culture = CultureInfo.CurrentCulture; + return dtOffset.ToString("O", culture ?? CultureInfo.CurrentCulture); } - - return ((DateTimeOffset)value).ToString("O", culture); - } - if ((destinationType == typeof(InstanceDescriptor)) && (value is DateTimeOffset)) - { - // Use the year, month, day, hour, minute, second, millisecond, offset constructor - // Should there be a branch to use the calendar constructor? - DateTimeOffset dtOffset = (DateTimeOffset)value; - - Type intType = typeof(int); - ConstructorInfo constructor = typeof(DateTimeOffset).GetConstructor( - new Type[] { - intType, - intType, - intType, - intType, - intType, - intType, - intType, - typeof(TimeSpan) - } - ); - - if (constructor != null) + else if (destinationType == typeof(InstanceDescriptor)) { + // Use the year, month, day, hour, minute, second, millisecond, offset constructor + ConstructorInfo constructor = typeof(DateTimeOffset).GetConstructor(new Type[] + { + typeof(int), + typeof(int), + typeof(int), + typeof(int), + typeof(int), + typeof(int), + typeof(int), + typeof(TimeSpan) + }); return new InstanceDescriptor( constructor, new object[] { @@ -77,23 +68,16 @@ public override object ConvertTo(ITypeDescriptorContext context, CultureInfo cul }, true); } - - return null; } + return base.ConvertTo(context, culture, value, destinationType); } public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) { - if (value is string) + if (value is string s) { - string s = ((string)value).Trim(); - - if (culture == null) - { - culture = CultureInfo.CurrentCulture; - } - return DateTimeOffset.Parse(s, culture, DateTimeStyles.None); + return DateTimeOffset.Parse(s.Trim(), culture ?? CultureInfo.CurrentCulture, DateTimeStyles.None); } return base.ConvertFrom(context, culture, value); From f5ed55837144bccc613c7feac52a93f77ea25a78 Mon Sep 17 00:00:00 2001 From: Bellamy Date: Wed, 3 May 2023 10:36:10 +0100 Subject: [PATCH 3/5] Cleanup TypeUriConverter - Code cleanup --- .../Markup/Replacements/TypeUriConverter.cs | 56 ++++++------------- 1 file changed, 16 insertions(+), 40 deletions(-) diff --git a/src/Microsoft.DotNet.Wpf/src/Shared/System/Windows/Markup/Replacements/TypeUriConverter.cs b/src/Microsoft.DotNet.Wpf/src/Shared/System/Windows/Markup/Replacements/TypeUriConverter.cs index 55e55421405..3090cea60c9 100644 --- a/src/Microsoft.DotNet.Wpf/src/Shared/System/Windows/Markup/Replacements/TypeUriConverter.cs +++ b/src/Microsoft.DotNet.Wpf/src/Shared/System/Windows/Markup/Replacements/TypeUriConverter.cs @@ -2,23 +2,18 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -// Contents: Limited converter for string <--> System.Uri +using System.ComponentModel; +using System.ComponentModel.Design.Serialization; +using System.Globalization; +using System.Reflection; namespace System.Xaml.Replacements { - using System; - using System.ComponentModel; - using System.ComponentModel.Design.Serialization; - using System.Globalization; - using System.Reflection; - + /// + /// Limited converter for string <--> System.Uri + /// internal class TypeUriConverter : TypeConverter { - public TypeUriConverter() - { - } - - /// public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) { ArgumentNullException.ThrowIfNull(sourceType); @@ -26,7 +21,6 @@ public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceT return sourceType == typeof(string) || sourceType == typeof(Uri); } - /// public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) { return @@ -35,13 +29,11 @@ public override bool CanConvertTo(ITypeDescriptorContext context, Type destinati destinationType == typeof(Uri); } - /// public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) { - var uri = value as Uri; - if (uri != null) + if (value is Uri uri) { - var uriKind = UriKind.RelativeOrAbsolute; + UriKind uriKind = UriKind.RelativeOrAbsolute; if (uri.IsWellFormedOriginalString()) { uriKind = uri.IsAbsoluteUri ? UriKind.Absolute : UriKind.Relative; @@ -49,25 +41,14 @@ public override object ConvertTo(ITypeDescriptorContext context, CultureInfo cul if (destinationType == typeof(InstanceDescriptor)) { - var ci = - typeof(Uri) - .GetConstructor( - BindingFlags.Public | BindingFlags.Instance, - null, - new Type[] { typeof(string), typeof(UriKind) }, - null); - return - new InstanceDescriptor( - ci, - new object[] { uri.OriginalString, uriKind}); + ConstructorInfo constructor = typeof(Uri).GetConstructor(new Type[] { typeof(string), typeof(UriKind) }); + return new InstanceDescriptor(constructor, new object[] { uri.OriginalString, uriKind }); } - - if (destinationType == typeof(string)) + else if (destinationType == typeof(string)) { return uri.OriginalString; } - - if (destinationType == typeof(Uri)) + else if (destinationType == typeof(Uri)) { return new Uri(uri.OriginalString, uriKind); } @@ -76,11 +57,9 @@ public override object ConvertTo(ITypeDescriptorContext context, CultureInfo cul return base.ConvertTo(context, culture, value, destinationType); } - /// public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) { - var uriString = value as string; - if (uriString != null) + if (value is string uriString) { if (Uri.IsWellFormedUriString(uriString, UriKind.Absolute)) { @@ -95,8 +74,7 @@ public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo c return new Uri(uriString, UriKind.RelativeOrAbsolute); } - var uri = value as Uri; - if (uri != null) + if (value is Uri uri) { if (uri.IsWellFormedOriginalString()) { @@ -109,11 +87,9 @@ public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo c return base.ConvertFrom(context, culture, value); } - /// public override bool IsValid(ITypeDescriptorContext context, object value) { - var uriString = value as string; - if (uriString != null) + if (value is string uriString) { return Uri.TryCreate(uriString, UriKind.RelativeOrAbsolute, out _); } From 6902413efe01cddbfeb6d07277ad73ecb7adbd8c Mon Sep 17 00:00:00 2001 From: Hugh Bellamy Date: Wed, 20 Sep 2023 09:58:30 +0100 Subject: [PATCH 4/5] Cleanup TypeListConverter --- .../Xaml/Replacements/TypeListConverter.cs | 28 ++++++------------- 1 file changed, 9 insertions(+), 19 deletions(-) diff --git a/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/Replacements/TypeListConverter.cs b/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/Replacements/TypeListConverter.cs index ed6efa8bac2..3c818595014 100644 --- a/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/Replacements/TypeListConverter.cs +++ b/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/Replacements/TypeListConverter.cs @@ -4,50 +4,40 @@ using System.ComponentModel; using System.Globalization; -using XAML3 = System.Windows.Markup; +using System.Windows.Markup; namespace System.Xaml.Replacements { - // Not sure if this type converter is used at all. - // we need to either make this a useful type converter or remove the code. - /// /// TypeConverter for System.Type[] /// internal class TypeListConverter : TypeConverter { - private static readonly TypeTypeConverter typeTypeConverter = new TypeTypeConverter(); + private static readonly TypeTypeConverter s_typeTypeConverter = new TypeTypeConverter(); + public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) - { - return sourceType == typeof(string); - } + => sourceType == typeof(string); public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) { - string typeList = (string)value; - if (null != context) + if (context != null && value is string typeList) { - // Consider HashMap(int, int), HashMap(int, int) string[] tl = StringHelpers.SplitTypeList(typeList); Type[] types = new Type[tl.Length]; for (int i = 0; i < tl.Length; i++) { - types[i] = (Type)typeTypeConverter.ConvertFrom(context, TypeConverterHelper.InvariantEnglishUS, tl[i]); + types[i] = (Type)s_typeTypeConverter.ConvertFrom(context, TypeConverterHelper.InvariantEnglishUS, tl[i]); } + return types; } + return base.ConvertFrom(context, culture, value); } - - } internal static class StringHelpers { - // split top level types and strip out whitespace - public static string[] SplitTypeList(string typeList) - { - return null; - } + public static string[] SplitTypeList(string typeList) => Array.Empty(); } } From ced5cddb1c9d9547cfbc43f1e847f367c2d9860f Mon Sep 17 00:00:00 2001 From: Hugh Bellamy Date: Wed, 20 Sep 2023 09:58:40 +0100 Subject: [PATCH 5/5] Cleanup other Xaml Replacement converters --- .../Xaml/Replacements/TypeTypeConverter.cs | 35 ++-- .../Xaml/Schema/BuiltInValueConverter.cs | 179 +++--------------- .../System/Xaml/Schema/XamlValueConverter.cs | 30 ++- .../Windows/Markup/TypeTypeConverter.cs | 11 +- 4 files changed, 60 insertions(+), 195 deletions(-) diff --git a/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/Replacements/TypeTypeConverter.cs b/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/Replacements/TypeTypeConverter.cs index 456a511c1e2..91adae87973 100644 --- a/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/Replacements/TypeTypeConverter.cs +++ b/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/Replacements/TypeTypeConverter.cs @@ -4,8 +4,8 @@ using System.ComponentModel; using System.Globalization; +using System.Windows.Markup; using System.Xaml.Schema; -using XAML3 = System.Windows.Markup; namespace System.Xaml.Replacements { @@ -15,22 +15,16 @@ namespace System.Xaml.Replacements internal class TypeTypeConverter : TypeConverter { public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) - { - return sourceType == typeof(string); - } + => sourceType == typeof(string); public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) { - string typeName = value as string; - - if (null != context && null != typeName) + if (context != null && value is string typeName) { - var typeResolver = GetService(context); - - if (null != typeResolver) + IXamlTypeResolver typeResolver = GetService(context); + if (typeResolver != null) { - Type type = typeResolver.Resolve(typeName); - return type; + return typeResolver.Resolve(typeName); } } @@ -38,15 +32,11 @@ public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo c } public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) - { - return destinationType == typeof(string); - } + => destinationType == typeof(string); public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) { - Type type = value as Type; - - if (context != null && type != null && destinationType == typeof(string)) + if (context != null && value is Type type && destinationType == typeof(string)) { string result = ConvertTypeToString(context, type); if (result != null) @@ -54,12 +44,13 @@ public override object ConvertTo(ITypeDescriptorContext context, CultureInfo cul return result; } } + return base.ConvertTo(context, culture, value, destinationType); } private static string ConvertTypeToString(ITypeDescriptorContext context, Type type) { - var schemaContextProvider = GetService(context); + IXamlSchemaContextProvider schemaContextProvider = GetService(context); if (schemaContextProvider == null) { return null; @@ -68,17 +59,17 @@ private static string ConvertTypeToString(ITypeDescriptorContext context, Type t { return null; } + XamlType xamlType = schemaContextProvider.SchemaContext.GetXamlType(type); if (xamlType == null) { return null; } + return XamlTypeTypeConverter.ConvertXamlTypeToString(context, xamlType); } private static TService GetService(ITypeDescriptorContext context) where TService : class - { - return context.GetService(typeof(TService)) as TService; - } + => context.GetService(typeof(TService)) as TService; } } diff --git a/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/Schema/BuiltInValueConverter.cs b/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/Schema/BuiltInValueConverter.cs index 57c205a9804..8a75881459f 100644 --- a/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/Schema/BuiltInValueConverter.cs +++ b/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/Schema/BuiltInValueConverter.cs @@ -7,7 +7,6 @@ using System.Globalization; using System.Windows.Markup; using System.Xaml.Replacements; -using TypeListConverter = System.Xaml.Replacements.TypeListConverter; namespace System.Xaml.Schema { @@ -22,15 +21,9 @@ internal BuiltInValueConverter(Type converterType, Func factory) _factory = factory; } - internal override bool IsPublic - { - get { return true; } - } + internal override bool IsPublic => true; - protected override TConverterBase CreateInstance() - { - return _factory.Invoke(); - } + protected override TConverterBase CreateInstance() => _factory.Invoke(); } internal static class BuiltInValueConverter @@ -62,52 +55,16 @@ internal static class BuiltInValueConverter private static XamlValueConverter s_Uri; internal static XamlValueConverter Int32 - { - get - { - if (s_Int32 is null) - { - s_Int32 = new BuiltInValueConverter(typeof(Int32Converter), () => new Int32Converter()); - } - return s_Int32; - } - } + => s_Int32 ??= new BuiltInValueConverter(typeof(Int32Converter), () => new Int32Converter()); internal static XamlValueConverter String - { - get - { - if (s_String is null) - { - s_String = new BuiltInValueConverter(typeof(StringConverter), () => new StringConverter()); - } - return s_String; - } - } + => s_String ??= new BuiltInValueConverter(typeof(StringConverter), () => new StringConverter()); internal static XamlValueConverter Object - { - get - { - if (s_Object is null) - { - s_Object = new XamlValueConverter(null, XamlLanguage.Object); - } - return s_Object; - } - } + => s_Object ??= new XamlValueConverter(null, XamlLanguage.Object); internal static XamlValueConverter Event - { - get - { - if (s_Delegate is null) - { - s_Delegate = new BuiltInValueConverter(typeof(EventConverter), () => new EventConverter()); - } - return s_Delegate; - } - } + => s_Delegate ??= new BuiltInValueConverter(typeof(EventConverter), () => new EventConverter()); internal static XamlValueConverter GetTypeConverter(Type targetType) { @@ -125,163 +82,83 @@ internal static XamlValueConverter GetTypeConverter(Type targetTy } if (typeof(Int16) == targetType) { - if (s_Int16 is null) - { - s_Int16 = new BuiltInValueConverter(typeof(Int16Converter), () => new Int16Converter()); - } - return s_Int16; + return s_Int16 ??= new BuiltInValueConverter(typeof(Int16Converter), () => new Int16Converter()); } if (typeof(Int64) == targetType) { - if (s_Int64 is null) - { - s_Int64 = new BuiltInValueConverter(typeof(Int64Converter), () => new Int64Converter()); - } - return s_Int64; + return s_Int64 ??= new BuiltInValueConverter(typeof(Int64Converter), () => new Int64Converter()); } if (typeof(UInt32) == targetType) { - if (s_UInt32 is null) - { - s_UInt32 = new BuiltInValueConverter(typeof(UInt32Converter), () => new UInt32Converter()); - } - return s_UInt32; + return s_UInt32 ??= new BuiltInValueConverter(typeof(UInt32Converter), () => new UInt32Converter()); } if (typeof(UInt16) == targetType) { - if (s_UInt16 is null) - { - s_UInt16 = new BuiltInValueConverter(typeof(UInt16Converter), () => new UInt16Converter()); - } - return s_UInt16; + return s_UInt16 ??= new BuiltInValueConverter(typeof(UInt16Converter), () => new UInt16Converter()); } if (typeof(UInt64) == targetType) { - if (s_UInt64 is null) - { - s_UInt64 = new BuiltInValueConverter(typeof(UInt64Converter), () => new UInt64Converter()); - } - return s_UInt64; + return s_UInt64 ??= new BuiltInValueConverter(typeof(UInt64Converter), () => new UInt64Converter()); } if (typeof(Boolean) == targetType) { - if (s_Boolean is null) - { - s_Boolean = new BuiltInValueConverter(typeof(BooleanConverter), () => new BooleanConverter()); - } - return s_Boolean; + return s_Boolean ??= new BuiltInValueConverter(typeof(BooleanConverter), () => new BooleanConverter()); } if (typeof(Double) == targetType) { - if (s_Double is null) - { - s_Double = new BuiltInValueConverter(typeof(DoubleConverter), () => new DoubleConverter()); - } - return s_Double; + return s_Double ??= new BuiltInValueConverter(typeof(DoubleConverter), () => new DoubleConverter()); } if (typeof(Single) == targetType) { - if (s_Single is null) - { - s_Single = new BuiltInValueConverter(typeof(SingleConverter), () => new SingleConverter()); - } - return s_Single; + return s_Single ??= new BuiltInValueConverter(typeof(SingleConverter), () => new SingleConverter()); } if (typeof(Byte) == targetType) { - if (s_Byte is null) - { - s_Byte = new BuiltInValueConverter(typeof(ByteConverter), () => new ByteConverter()); - } - return s_Byte; + return s_Byte ??= new BuiltInValueConverter(typeof(ByteConverter), () => new ByteConverter()); } if (typeof(SByte) == targetType) { - if (s_SByte is null) - { - s_SByte = new BuiltInValueConverter(typeof(SByteConverter), () => new SByteConverter()); - } - return s_SByte; + return s_SByte ??= new BuiltInValueConverter(typeof(SByteConverter), () => new SByteConverter()); } if (typeof(Char) == targetType) { - if (s_Char is null) - { - s_Char = new BuiltInValueConverter(typeof(CharConverter), () => new CharConverter()); - } - return s_Char; + return s_Char ??= new BuiltInValueConverter(typeof(CharConverter), () => new CharConverter()); } if (typeof(Decimal) == targetType) { - if (s_Decimal is null) - { - s_Decimal = new BuiltInValueConverter(typeof(DecimalConverter), () => new DecimalConverter()); - } - return s_Decimal; + return s_Decimal ??= new BuiltInValueConverter(typeof(DecimalConverter), () => new DecimalConverter()); } if (typeof(TimeSpan) == targetType) { - if (s_TimeSpan is null) - { - s_TimeSpan = new BuiltInValueConverter(typeof(TimeSpanConverter), () => new TimeSpanConverter()); - } - return s_TimeSpan; + return s_TimeSpan ??= new BuiltInValueConverter(typeof(TimeSpanConverter), () => new TimeSpanConverter()); } if (typeof(Guid) == targetType) { - if (s_Guid is null) - { - s_Guid = new BuiltInValueConverter(typeof(GuidConverter), () => new GuidConverter()); - } - return s_Guid; + return s_Guid ??= new BuiltInValueConverter(typeof(GuidConverter), () => new GuidConverter()); } if (typeof(Type).IsAssignableFrom(targetType)) { - if (s_Type is null) - { - s_Type = new BuiltInValueConverter(typeof(TypeTypeConverter), () => new TypeTypeConverter()); - } - return s_Type; + return s_Type ??= new BuiltInValueConverter(typeof(System.Xaml.Replacements.TypeTypeConverter), () => new System.Xaml.Replacements.TypeTypeConverter()); } if (typeof(Type[]).IsAssignableFrom(targetType)) { - if (s_TypeList is null) - { - s_TypeList = new BuiltInValueConverter(typeof(TypeListConverter), () => new TypeListConverter()); - } - return s_TypeList; + return s_TypeList ??= new BuiltInValueConverter(typeof(System.Xaml.Replacements.TypeListConverter), () => new System.Xaml.Replacements.TypeListConverter()); } if (typeof(DateTime) == targetType) { - if (s_DateTime is null) - { - s_DateTime = new BuiltInValueConverter(typeof(DateTimeConverter2), () => new DateTimeConverter2()); - } - return s_DateTime; + return s_DateTime ??= new BuiltInValueConverter(typeof(System.Xaml.Replacements.DateTimeConverter2), () => new System.Xaml.Replacements.DateTimeConverter2()); } if (typeof(DateTimeOffset) == targetType) { - if (s_DateTimeOffset is null) - { - s_DateTimeOffset = new BuiltInValueConverter(typeof(DateTimeOffsetConverter2), () => new DateTimeOffsetConverter2()); - } - return s_DateTimeOffset; + return s_DateTimeOffset ??= new BuiltInValueConverter(typeof(System.Xaml.Replacements.DateTimeOffsetConverter2), () => new System.Xaml.Replacements.DateTimeOffsetConverter2()); } if (typeof(CultureInfo).IsAssignableFrom(targetType)) { - if (s_CultureInfo is null) - { - s_CultureInfo = new BuiltInValueConverter(typeof(CultureInfoConverter), () => new CultureInfoConverter()); - } - return s_CultureInfo; + return s_CultureInfo ??= new BuiltInValueConverter(typeof(CultureInfoConverter), () => new CultureInfoConverter()); } if (typeof(Delegate).IsAssignableFrom(targetType)) { - if (s_Delegate is null) - { - s_Delegate = new BuiltInValueConverter(typeof(EventConverter), () => new EventConverter()); - } - return s_Delegate; + return s_Delegate ??= new BuiltInValueConverter(typeof(EventConverter), () => new EventConverter()); } if (typeof(Uri).IsAssignableFrom(targetType)) { @@ -317,8 +194,10 @@ internal static XamlValueConverter GetTypeConverter(Type targetTy s_Uri = new BuiltInValueConverter(stdConverter.GetType(), () => TypeDescriptor.GetConverter(typeof(Uri))); } } + return s_Uri; } + return null; } @@ -332,8 +211,10 @@ internal static XamlValueConverter GetValueSerializer(Type targ ValueSerializer stringSerializer = ValueSerializer.GetSerializerFor(typeof(string)); s_StringSerializer = new BuiltInValueConverter(stringSerializer.GetType(), () => stringSerializer); } + return s_StringSerializer; } + return null; } } diff --git a/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/Schema/XamlValueConverter.cs b/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/Schema/XamlValueConverter.cs index ffad632db97..68e812331e0 100644 --- a/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/Schema/XamlValueConverter.cs +++ b/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/Schema/XamlValueConverter.cs @@ -29,8 +29,9 @@ public XamlValueConverter(Type converterType, XamlType targetType, string name) { if (converterType == null && targetType == null && name == null) { - throw new ArgumentException(SR.Format(SR.ArgumentRequired, "converterType, targetType, name")); + throw new ArgumentException(SR.Format(SR.ArgumentRequired, $"{nameof(converterType)}, {nameof(targetType)}, {nameof(name)}")); } + ConverterType = converterType; TargetType = targetType; Name = name ?? GetDefaultName(); @@ -45,14 +46,12 @@ public TConverterBase ConverterInstance Interlocked.CompareExchange(ref _instance, CreateInstance(), null); _instanceIsSet = true; } + return _instance; } } - public override string ToString() - { - return Name; - } + public override string ToString() => Name; internal virtual bool IsPublic { @@ -62,6 +61,7 @@ internal virtual bool IsPublic { _isPublic = (ConverterType == null || ConverterType.IsVisible) ? ThreeValuedBool.True : ThreeValuedBool.False; } + return _isPublic == ThreeValuedBool.True; } } @@ -80,8 +80,10 @@ protected virtual TConverterBase CreateInstance() throw new XamlSchemaException(SR.Format(SR.ConverterMustDeriveFromBase, ConverterType, typeof(TConverterBase))); } + return (TConverterBase)Activator.CreateInstance(ConverterType, null); } + return null; } @@ -93,13 +95,13 @@ private string GetDefaultName() { return ConverterType.Name + "(" + TargetType.Name + ")"; } + return ConverterType.Name; } + return TargetType.Name; } - #region IEquatable> Members - public override bool Equals(object obj) { XamlValueConverter other = obj as XamlValueConverter; @@ -107,6 +109,7 @@ public override bool Equals(object obj) { return false; } + return this == other; } @@ -121,13 +124,11 @@ public override int GetHashCode() { result ^= TargetType.GetHashCode(); } + return result; } - public bool Equals(XamlValueConverter other) - { - return this == other; - } + public bool Equals(XamlValueConverter other) => this == other; public static bool operator ==(XamlValueConverter converter1, XamlValueConverter converter2) { @@ -139,16 +140,13 @@ public bool Equals(XamlValueConverter other) { return false; } + return converter1.ConverterType == converter2.ConverterType && converter1.TargetType == converter2.TargetType && converter1.Name == converter2.Name; } public static bool operator !=(XamlValueConverter converter1, XamlValueConverter converter2) - { - return !(converter1 == converter2); - } - - #endregion + => !(converter1 == converter2); } } diff --git a/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/Markup/TypeTypeConverter.cs b/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/Markup/TypeTypeConverter.cs index d0c9ff560bb..afa84668952 100644 --- a/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/Markup/TypeTypeConverter.cs +++ b/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/Markup/TypeTypeConverter.cs @@ -18,19 +18,14 @@ internal class TypeTypeConverter : TypeConverter { #if !PBTCOMPILER public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) - { - return sourceType == typeof(string); - } + => sourceType == typeof(string); public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) { - string typeName = value as string; - - if (null != context && typeName != null) + if (context != null && value is string typeName) { IXamlTypeResolver xamlTypeResolver = (IXamlTypeResolver)context.GetService(typeof(IXamlTypeResolver)); - - if (null != xamlTypeResolver) + if (xamlTypeResolver != null) { return xamlTypeResolver.Resolve(typeName); }