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 ad31d402523d6a49d5b811a21ae152a927d5226f..8229e642dcb11ce6425c4d1b370c0e91eec39a85 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 6f52e630660ec8b26eef10c975a2588c88caca03..c6082e43775c9e8c3051e1a1ea995e9d7654d98e 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 42af1aeaca9af52f5c9972a0c227c2727bcd8d4c..3e5c58141d3ec8d2536e0b2c4e54a7efe0124783 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 df1e1d8f6e08191ffe70b43b40d43f320d167d14..9721ca0558d0f4283a9cfce10a71968b9eab16a9 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 fd9c2ce4fa6baffd768a2ad0c0a43d410433904d..81cd48054e0f385dc684f27061b104ee1667d032 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 f1f2481893c582838ab003d9265bda9aaddc01c0..a8913aae336ec6f43a91bf88ede76753bf2dd91a 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 NET6_0_OR_GREATER + 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 3f9805b535a36838573735088159353770d586f2..c422b9a7528cb2205a4690abe761a3e494644d40 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 d53342bc8dda04041f0898f364cb8145fd2b391a..f04971a80de45ffd332ffb7eccb6fda5caa1a3f5 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 0fa5561f3f83d5212d3bdce64a48980c1a5da7f5..c29e0c76b7bed219863a037ecac7d4ac9100c11f 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 NET6_0_OR_GREATER + 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 NET6_0_OR_GREATER + T value = values[i]; +#else T value = (T)values.GetValue(i)!; +#endif ulong key = ConvertToUInt64(value); string name = names[i];