summary refs log tree commit diff
path: root/crypto/src/util/Enums.cs
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2024-05-13 01:38:11 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2024-05-13 01:38:11 +0700
commite44ebc3ada821408d29c3bd52fd862db4067064d (patch)
treec4e9d24fbb596123d338aa0c5902a9b0724111c1 /crypto/src/util/Enums.cs
parentUpdate release notes (diff)
downloadBouncyCastle.NET-ed25519-e44ebc3ada821408d29c3bd52fd862db4067064d.tar.xz
Refactor to use Enum.TryParse
Diffstat (limited to 'crypto/src/util/Enums.cs')
-rw-r--r--crypto/src/util/Enums.cs35
1 files changed, 16 insertions, 19 deletions
diff --git a/crypto/src/util/Enums.cs b/crypto/src/util/Enums.cs
index 1034b5b7e..01adf7f0a 100644
--- a/crypto/src/util/Enums.cs
+++ b/crypto/src/util/Enums.cs
@@ -6,25 +6,6 @@ namespace Org.BouncyCastle.Utilities
 {
     internal static class Enums
     {
-        internal static TEnum GetEnumValue<TEnum>(string s)
-            where TEnum : struct, Enum
-        {
-            // We only want to parse single named constants
-            if (s.Length > 0 && char.IsLetter(s[0]) && s.IndexOf(',') < 0)
-            {
-                s = s.Replace('-', '_');
-                s = s.Replace('/', '_');
-
-#if NETCOREAPP2_0_OR_GREATER || NETSTANDARD2_1_OR_GREATER
-                return Enum.Parse<TEnum>(s, false);
-#else
-                return (TEnum)Enum.Parse(typeof(TEnum), s, false);
-#endif
-            }
-
-            throw new ArgumentException();
-        }
-
         internal static TEnum[] GetEnumValues<TEnum>()
             where TEnum : struct, Enum
         {
@@ -42,5 +23,21 @@ namespace Org.BouncyCastle.Utilities
             int pos = (int)(DateTimeUtilities.CurrentUnixMs() & int.MaxValue) % values.Length;
             return values[pos];
         }
+
+        internal static bool TryGetEnumValue<TEnum>(string s, out TEnum result)
+            where TEnum : struct, Enum
+        {
+            // We only want to parse single named constants
+            if (s.Length > 0 && char.IsLetter(s[0]) && s.IndexOf(',') < 0)
+            {
+                s = s.Replace('-', '_');
+                s = s.Replace('/', '_');
+
+                return Enum.TryParse<TEnum>(s, out result);
+            }
+
+            result = default(TEnum);
+            return false;
+        }
     }
 }