Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use generic Enum methods in more places. #67147

Merged
merged 2 commits into from
Mar 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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<UnixFileSystemTypes>())
{
System.Diagnostics.Debug.Assert(GetDriveType(name) != DriveType.Unknown,
$"Expected {nameof(UnixFileSystemTypes)}.{name} to have an entry in {nameof(GetDriveType)}.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<XmlNodeType>();
for (int i = 0; i < enumValues.Length; i++)
{
tempVal = (int)enumValues.GetValue(i)!;
tempVal = (int)enumValues[i];
if (tempVal > max)
max = tempVal;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<OleDbServiceValues>(v, true);
}
return (int)convertedValue;
return convertedValue;
}
else
{
return (int)(OleDbServiceValues)Enum.Parse(typeof(OleDbServiceValues), svalue, true);
return (int)Enum.Parse<OleDbServiceValues>(svalue, true);
}
}
}
Expand All @@ -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);
}
Expand All @@ -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<OleDbServiceValues>();
Array.Sort(objValues, 0, objValues.Length);
standardValues = new StandardValuesCollection(objValues);
_standardValues = standardValues;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ internal static class PerfProviderCollection
private static object s_hiddenInternalSyncObject;
private static readonly List<PerfProvider> s_providerList = new List<PerfProvider>();
private static readonly Dictionary<object, int> s_counterSetList = new Dictionary<object, int>();
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<CounterType>();
private static readonly CounterSetInstanceType[] s_counterSetInstanceTypes = Enum.GetValues<CounterSetInstanceType>();

private static object s_lockObject
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<NotifyFilters>())
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.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,11 @@ private void ProcessRelationshipAttributes(XmlCompatibilityReader reader)
{
try
{
#if NET6_0_OR_GREATER
relationshipTargetMode = Enum.Parse<TargetMode>(targetModeAttributeValue, ignoreCase: false);
#else
relationshipTargetMode = (TargetMode)(Enum.Parse(typeof(TargetMode), targetModeAttributeValue, ignoreCase: false));
#endif
}
catch (ArgumentNullException argNullEx)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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>())
{
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})");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<XmlTypeCode>();
Debug.Assert(arrEnum[arrEnum.Length - 1] == XmlTypeCode.DayTimeDuration,
"DayTimeDuration is no longer the last item in XmlTypeCode. This code expects it to be.");
#endif

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,13 @@ public EnumConverter(EnumConverterOptions converterOptions, JsonNamingPolicy? na
_namingPolicy = namingPolicy;
_nameCache = new ConcurrentDictionary<ulong, JsonEncodedText>();

#if NET6_0_OR_GREATER
string[] names = Enum.GetNames<T>();
T[] values = Enum.GetValues<T>();
#else
string[] names = Enum.GetNames(TypeToConvert);
Array values = Enum.GetValues(TypeToConvert);
teo-tsirpanis marked this conversation as resolved.
Show resolved Hide resolved
#endif
Debug.Assert(names.Length == values.Length);

JavaScriptEncoder? encoder = serializerOptions.Encoder;
Expand All @@ -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];

Expand Down