Skip to content

Commit

Permalink
Use generic Enum methods when possible. (#64850)
Browse files Browse the repository at this point in the history
* Use generic `Enum` methods when possible.

* Update src/libraries/System.ComponentModel.TypeConverter/src/System/ComponentModel/ToolboxItemFilterAttribute.cs

Co-authored-by: Günther Foidl <gue@korporal.at>

Co-authored-by: Günther Foidl <gue@korporal.at>
  • Loading branch information
teo-tsirpanis and gfoidl committed Mar 2, 2022
1 parent b61687b commit d579527
Show file tree
Hide file tree
Showing 10 changed files with 11 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ public ReadyToRunInstructionSetSupportSignature(string instructionSetsSupport)
_instructionSetsSupport = instructionSetsSupport;
}

private ReadyToRunInstructionSet? InstructionSetFromString(string instructionSetString)
private ReadyToRunInstructionSet InstructionSetFromString(string instructionSetString)
{
return (ReadyToRunInstructionSet)Enum.Parse(typeof(ReadyToRunInstructionSet), instructionSetString);
return Enum.Parse<ReadyToRunInstructionSet>(instructionSetString);
}

public override ObjectData GetData(NodeFactory factory, bool relocsOnly = false)
Expand Down
2 changes: 1 addition & 1 deletion src/coreclr/tools/dotnet-pgo/CommandLineOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ private Verbosity VerbosityConverter(string s)
{
try
{
return (Verbosity)Enum.Parse(typeof(Verbosity), s);
return Enum.Parse<Verbosity>(s);
}
catch
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace System.ComponentModel.DataAnnotations
AllowMultiple = false)]
public class DataTypeAttribute : ValidationAttribute
{
private static readonly string[] _dataTypeStrings = Enum.GetNames(typeof(DataType));
private static readonly string[] _dataTypeStrings = Enum.GetNames<DataType>();

/// <summary>
/// Constructor that accepts a data type enumeration
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,6 @@ public override bool Match([NotNullWhen(true)] object? obj)
&& other.FilterString.Equals(FilterString);
}

public override string ToString() => FilterString + "," + Enum.GetName(typeof(ToolboxItemFilterType), FilterType);
public override string ToString() => FilterString + "," + Enum.GetName<ToolboxItemFilterType>(FilterType);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public string DataViewSettingCollectionString
}
if (r.MoveToAttribute("RowStateFilter"))
{
_dataViewSettingsCollection[table]!.RowStateFilter = (DataViewRowState)Enum.Parse(typeof(DataViewRowState), r.Value);
_dataViewSettingsCollection[table]!.RowStateFilter = Enum.Parse<DataViewRowState>(r.Value);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public bool ShouldTrace(TraceEventType eventType)

protected override void OnValueChanged()
{
SwitchSetting = (int)Enum.Parse(typeof(SourceLevels), Value, true);
SwitchSetting = (int)Enum.Parse<SourceLevels>(Value, true);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ protected override void OnSwitchSettingChanged()

protected override void OnValueChanged()
{
SwitchSetting = (int)Enum.Parse(typeof(TraceLevel), Value, true);
SwitchSetting = (int)Enum.Parse<TraceLevel>(Value, true);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ public override bool CanConvertTo(ITypeDescriptorContext? context, [NotNullWhen(
string styleText = styleTokens[tokenCount];
styleText = styleText.Trim();

fontStyle |= (FontStyle)Enum.Parse(typeof(FontStyle), styleText, true);
fontStyle |= Enum.Parse<FontStyle>(styleText, true);

// Enum.IsDefined doesn't do what we want on flags enums...
FontStyle validBits = FontStyle.Regular | FontStyle.Bold | FontStyle.Italic | FontStyle.Underline | FontStyle.Strikeout;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public string? Name
return name;

// Create ilasm style name from the enum value name.
name = Enum.GetName(typeof(OpCodeValues), opCodeValue)!.ToLowerInvariant().Replace('_', '.');
name = Enum.GetName<OpCodeValues>(opCodeValue)!.ToLowerInvariant().Replace('_', '.');
Volatile.Write(ref nameCache[idx], name);
return name;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ internal enum OptimizerPatternArgument
/// </summary>
internal sealed class OptimizerPatterns : IQilAnnotation
{
private static readonly int s_patternCount = Enum.GetValues(typeof(OptimizerPatternName)).Length;
private static readonly int s_patternCount = Enum.GetValues<OptimizerPatternName>().Length;

private int _patterns; // Set of patterns that the annotated Qil node and its subtree matches
private bool _isReadOnly; // True if setters are disabled in the case of singleton OptimizerPatterns
Expand Down

0 comments on commit d579527

Please sign in to comment.