From 0787e1a602f0b00e5e2422be47ab064dcd7d93f5 Mon Sep 17 00:00:00 2001 From: Theodore Tsirpanis Date: Wed, 23 Mar 2022 13:01:40 +0200 Subject: [PATCH 1/2] Use generic `Enum` methods in more places. --- .../System.Native/Interop.MountPoints.FormatInfo.cs | 2 +- .../src/System/Xml/XPathNodePointer.cs | 4 ++-- .../src/OleDbConnectionStringBuilder.cs | 12 ++++++------ .../PerformanceData/PerfProviderCollection.cs | 4 ++-- .../src/System/IO/FileSystemWatcher.cs | 2 +- .../IO/Packaging/InternalRelationshipCollection.cs | 4 ++++ .../src/System/Net/Security/TlsCipherSuiteData.cs | 5 ++--- .../src/System/Xml/Xsl/XmlQueryTypeFactory.cs | 4 ++-- .../Serialization/Converters/Value/EnumConverter.cs | 9 +++++++++ 9 files changed, 29 insertions(+), 17 deletions(-) diff --git a/src/libraries/Common/src/Interop/Unix/System.Native/Interop.MountPoints.FormatInfo.cs b/src/libraries/Common/src/Interop/Unix/System.Native/Interop.MountPoints.FormatInfo.cs index ad31d402523d6..8229e642dcb11 100644 --- a/src/libraries/Common/src/Interop/Unix/System.Native/Interop.MountPoints.FormatInfo.cs +++ b/src/libraries/Common/src/Interop/Unix/System.Native/Interop.MountPoints.FormatInfo.cs @@ -12,7 +12,7 @@ internal static partial class Sys #if DEBUG static Sys() { - foreach (string name in Enum.GetNames(typeof(UnixFileSystemTypes))) + foreach (string name in Enum.GetNames()) { System.Diagnostics.Debug.Assert(GetDriveType(name) != DriveType.Unknown, $"Expected {nameof(UnixFileSystemTypes)}.{name} to have an entry in {nameof(GetDriveType)}."); diff --git a/src/libraries/System.Data.Common/src/System/Xml/XPathNodePointer.cs b/src/libraries/System.Data.Common/src/System/Xml/XPathNodePointer.cs index 6f52e630660ec..c6082e43775c9 100644 --- a/src/libraries/System.Data.Common/src/System/Xml/XPathNodePointer.cs +++ b/src/libraries/System.Data.Common/src/System/Xml/XPathNodePointer.cs @@ -28,10 +28,10 @@ private static int[] CreateXmlNodeTypeToXpathNodeTypeMap() { #if DEBUG int max = 0, tempVal = 0; - Array enumValues = Enum.GetValues(typeof(XmlNodeType)); + XmlNodeType[] enumValues = Enum.GetValues(); for (int i = 0; i < enumValues.Length; i++) { - tempVal = (int)enumValues.GetValue(i)!; + tempVal = (int)enumValues[i]; if (tempVal > max) max = tempVal; } diff --git a/src/libraries/System.Data.OleDb/src/OleDbConnectionStringBuilder.cs b/src/libraries/System.Data.OleDb/src/OleDbConnectionStringBuilder.cs index 42af1aeaca9af..3e5c58141d3ec 100644 --- a/src/libraries/System.Data.OleDb/src/OleDbConnectionStringBuilder.cs +++ b/src/libraries/System.Data.OleDb/src/OleDbConnectionStringBuilder.cs @@ -632,13 +632,13 @@ public override bool CanConvertFrom(ITypeDescriptorContext? context, Type source string[] values = svalue.Split(new char[] { ',' }); foreach (string v in values) { - convertedValue |= (int)(OleDbServiceValues)Enum.Parse(typeof(OleDbServiceValues), v, true); + convertedValue |= (int)Enum.Parse(v, true); } - return (int)convertedValue; + return convertedValue; } else { - return (int)(OleDbServiceValues)Enum.Parse(typeof(OleDbServiceValues), svalue, true); + return (int)Enum.Parse(svalue, true); } } } @@ -651,11 +651,11 @@ public override bool CanConvertTo(ITypeDescriptorContext? context, [NotNullWhen( return ((typeof(string) == destinationType) || base.CanConvertTo(context, destinationType)); } - public override object? ConvertTo(ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object? value, Type destinationType) + public override object? ConvertTo(ITypeDescriptorContext? context, CultureInfo? culture, object? value, Type destinationType) { if ((typeof(string) == destinationType) && (null != value) && (typeof(int) == value.GetType())) { - return Enum.Format(typeof(OleDbServiceValues), ((OleDbServiceValues)(int)value), "G"); + return ((OleDbServiceValues)(int)(value)).ToString("G"); } return base.ConvertTo(context, culture, value, destinationType); } @@ -675,7 +675,7 @@ public override StandardValuesCollection GetStandardValues(ITypeDescriptorContex StandardValuesCollection? standardValues = _standardValues; if (null == standardValues) { - Array objValues = Enum.GetValues(typeof(OleDbServiceValues)); + OleDbServiceValues[] objValues = Enum.GetValues(); Array.Sort(objValues, 0, objValues.Length); standardValues = new StandardValuesCollection(objValues); _standardValues = standardValues; diff --git a/src/libraries/System.Diagnostics.PerformanceCounter/src/System/Diagnostics/PerformanceData/PerfProviderCollection.cs b/src/libraries/System.Diagnostics.PerformanceCounter/src/System/Diagnostics/PerformanceData/PerfProviderCollection.cs index df1e1d8f6e081..9721ca0558d0f 100644 --- a/src/libraries/System.Diagnostics.PerformanceCounter/src/System/Diagnostics/PerformanceData/PerfProviderCollection.cs +++ b/src/libraries/System.Diagnostics.PerformanceCounter/src/System/Diagnostics/PerformanceData/PerfProviderCollection.cs @@ -33,8 +33,8 @@ internal static class PerfProviderCollection private static object s_hiddenInternalSyncObject; private static readonly List s_providerList = new List(); private static readonly Dictionary s_counterSetList = new Dictionary(); - private static readonly CounterType[] s_counterTypes = (CounterType[])Enum.GetValues(typeof(CounterType)); - private static readonly CounterSetInstanceType[] s_counterSetInstanceTypes = (CounterSetInstanceType[])Enum.GetValues(typeof(CounterSetInstanceType)); + private static readonly CounterType[] s_counterTypes = Enum.GetValues(); + private static readonly CounterSetInstanceType[] s_counterSetInstanceTypes = Enum.GetValues(); private static object s_lockObject { diff --git a/src/libraries/System.IO.FileSystem.Watcher/src/System/IO/FileSystemWatcher.cs b/src/libraries/System.IO.FileSystem.Watcher/src/System/IO/FileSystemWatcher.cs index fd9c2ce4fa6ba..81cd48054e0f3 100644 --- a/src/libraries/System.IO.FileSystem.Watcher/src/System/IO/FileSystemWatcher.cs +++ b/src/libraries/System.IO.FileSystem.Watcher/src/System/IO/FileSystemWatcher.cs @@ -64,7 +64,7 @@ public partial class FileSystemWatcher : Component, ISupportInitialize static FileSystemWatcher() { int s_notifyFiltersValidMask = 0; - foreach (int enumValue in Enum.GetValues(typeof(NotifyFilters))) + foreach (int enumValue in Enum.GetValues()) s_notifyFiltersValidMask |= enumValue; Debug.Assert(c_notifyFiltersValidMask == s_notifyFiltersValidMask, "The NotifyFilters enum has changed. The c_notifyFiltersValidMask must be updated to reflect the values of the NotifyFilters enum."); } diff --git a/src/libraries/System.IO.Packaging/src/System/IO/Packaging/InternalRelationshipCollection.cs b/src/libraries/System.IO.Packaging/src/System/IO/Packaging/InternalRelationshipCollection.cs index f1f2481893c58..f04369b6c1f26 100644 --- a/src/libraries/System.IO.Packaging/src/System/IO/Packaging/InternalRelationshipCollection.cs +++ b/src/libraries/System.IO.Packaging/src/System/IO/Packaging/InternalRelationshipCollection.cs @@ -320,7 +320,11 @@ private void ProcessRelationshipAttributes(XmlCompatibilityReader reader) { try { +#if NET + relationshipTargetMode = Enum.Parse(targetModeAttributeValue, ignoreCase: false); +#else relationshipTargetMode = (TargetMode)(Enum.Parse(typeof(TargetMode), targetModeAttributeValue, ignoreCase: false)); +#endif } catch (ArgumentNullException argNullEx) { diff --git a/src/libraries/System.Net.Security/src/System/Net/Security/TlsCipherSuiteData.cs b/src/libraries/System.Net.Security/src/System/Net/Security/TlsCipherSuiteData.cs index 3f9805b535a36..c422b9a7528cb 100644 --- a/src/libraries/System.Net.Security/src/System/Net/Security/TlsCipherSuiteData.cs +++ b/src/libraries/System.Net.Security/src/System/Net/Security/TlsCipherSuiteData.cs @@ -23,10 +23,9 @@ static TlsCipherSuiteData() s_tlsLookup.Count == LookupCount, $"Lookup dictionary was of size {s_tlsLookup.Count} instead of {LookupCount}"); - foreach (object? value in Enum.GetValues(typeof(TlsCipherSuite))) + foreach (TlsCipherSuite value in Enum.GetValues()) { - TlsCipherSuite val = (TlsCipherSuite)value!; - Debug.Assert(s_tlsLookup.ContainsKey(val), $"No mapping found for {val} ({(int)val})"); + Debug.Assert(s_tlsLookup.ContainsKey(value), $"No mapping found for {value} ({value:X})"); } } diff --git a/src/libraries/System.Private.Xml/src/System/Xml/Xsl/XmlQueryTypeFactory.cs b/src/libraries/System.Private.Xml/src/System/Xml/Xsl/XmlQueryTypeFactory.cs index d53342bc8dda0..f04971a80de45 100644 --- a/src/libraries/System.Private.Xml/src/System/Xml/Xsl/XmlQueryTypeFactory.cs +++ b/src/libraries/System.Private.Xml/src/System/Xml/Xsl/XmlQueryTypeFactory.cs @@ -347,8 +347,8 @@ private sealed class ItemType : XmlQueryType static ItemType() { #if DEBUG - Array arrEnum = Enum.GetValues(typeof(XmlTypeCode)); - Debug.Assert((XmlTypeCode)arrEnum.GetValue(arrEnum.Length - 1)! == XmlTypeCode.DayTimeDuration, + XmlTypeCode[] arrEnum = Enum.GetValues(); + Debug.Assert(arrEnum[arrEnum.Length - 1] == XmlTypeCode.DayTimeDuration, "DayTimeDuration is no longer the last item in XmlTypeCode. This code expects it to be."); #endif diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/EnumConverter.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/EnumConverter.cs index 0fa5561f3f83d..ff6145e7a27ab 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/EnumConverter.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/EnumConverter.cs @@ -47,8 +47,13 @@ public EnumConverter(EnumConverterOptions converterOptions, JsonNamingPolicy? na _namingPolicy = namingPolicy; _nameCache = new ConcurrentDictionary(); +#if NET + string[] names = Enum.GetNames(); + T[] values = Enum.GetValues(); +#else string[] names = Enum.GetNames(TypeToConvert); Array values = Enum.GetValues(TypeToConvert); +#endif Debug.Assert(names.Length == values.Length); JavaScriptEncoder? encoder = serializerOptions.Encoder; @@ -60,7 +65,11 @@ public EnumConverter(EnumConverterOptions converterOptions, JsonNamingPolicy? na break; } +#if NET + T value = values[i]; +#else T value = (T)values.GetValue(i)!; +#endif ulong key = ConvertToUInt64(value); string name = names[i]; From 0f766952f3e96c31c6eccb31e0f54495f438a4c0 Mon Sep 17 00:00:00 2001 From: Theodore Tsirpanis Date: Fri, 25 Mar 2022 21:32:13 +0200 Subject: [PATCH 2/2] Use `#if NET6_0_OR_GREATER` instead of `#if NET`. --- .../src/System/IO/Packaging/InternalRelationshipCollection.cs | 2 +- .../Text/Json/Serialization/Converters/Value/EnumConverter.cs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/libraries/System.IO.Packaging/src/System/IO/Packaging/InternalRelationshipCollection.cs b/src/libraries/System.IO.Packaging/src/System/IO/Packaging/InternalRelationshipCollection.cs index f04369b6c1f26..a8913aae336ec 100644 --- a/src/libraries/System.IO.Packaging/src/System/IO/Packaging/InternalRelationshipCollection.cs +++ b/src/libraries/System.IO.Packaging/src/System/IO/Packaging/InternalRelationshipCollection.cs @@ -320,7 +320,7 @@ private void ProcessRelationshipAttributes(XmlCompatibilityReader reader) { try { -#if NET +#if NET6_0_OR_GREATER relationshipTargetMode = Enum.Parse(targetModeAttributeValue, ignoreCase: false); #else relationshipTargetMode = (TargetMode)(Enum.Parse(typeof(TargetMode), targetModeAttributeValue, ignoreCase: false)); diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/EnumConverter.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/EnumConverter.cs index ff6145e7a27ab..c29e0c76b7bed 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/EnumConverter.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/EnumConverter.cs @@ -47,7 +47,7 @@ public EnumConverter(EnumConverterOptions converterOptions, JsonNamingPolicy? na _namingPolicy = namingPolicy; _nameCache = new ConcurrentDictionary(); -#if NET +#if NET6_0_OR_GREATER string[] names = Enum.GetNames(); T[] values = Enum.GetValues(); #else @@ -65,7 +65,7 @@ public EnumConverter(EnumConverterOptions converterOptions, JsonNamingPolicy? na break; } -#if NET +#if NET6_0_OR_GREATER T value = values[i]; #else T value = (T)values.GetValue(i)!;