Skip to content

Commit

Permalink
Use generic Enum methods in more places. (dotnet#67147)
Browse files Browse the repository at this point in the history
  • Loading branch information
teo-tsirpanis committed Mar 26, 2022
1 parent a2448b0 commit a56b106
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 17 deletions.
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);
#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

0 comments on commit a56b106

Please sign in to comment.