-
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add String.Equals with StringComparison (#93)
- Loading branch information
Showing
6 changed files
with
568 additions
and
446 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
using System.Diagnostics.CodeAnalysis; | ||
|
||
namespace HandlebarsDotNet.Helpers.Utils; | ||
|
||
public static class EnumUtils | ||
{ | ||
public static bool TryParse<TEnum>(object? value, [NotNullWhen(true)] out TEnum? comparisonType) where TEnum : struct, Enum | ||
{ | ||
switch (value) | ||
{ | ||
case TEnum valueAsEnum: | ||
comparisonType = valueAsEnum; | ||
return true; | ||
|
||
case int valueAsInt when Enum.IsDefined(typeof(TEnum), valueAsInt): | ||
comparisonType = IntToEnum<TEnum>(valueAsInt); | ||
return true; | ||
|
||
case string stringValue when Enum.TryParse<TEnum>(stringValue, out var parsedAsEnum): | ||
comparisonType = parsedAsEnum; | ||
return true; | ||
|
||
case string stringValue when int.TryParse(stringValue, out var parsedAsInt) && Enum.IsDefined(typeof(TEnum), parsedAsInt): | ||
comparisonType = IntToEnum<TEnum>(parsedAsInt); | ||
return true; | ||
|
||
default: | ||
comparisonType = default; | ||
return false; | ||
} | ||
} | ||
|
||
public static TEnum IntToEnum<TEnum>(int value) where TEnum : struct, Enum | ||
=> (TEnum)Enum.ToObject(typeof(TEnum), value); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.