summary refs log tree commit diff
path: root/crypto/src/util
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2022-10-05 23:03:16 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2022-10-05 23:03:16 +0700
commit6d442f9319a857fe483af22d67bbd1677b0991a1 (patch)
treef3356e473c23680d6675defe770532fbb5b42cdc /crypto/src/util
parentDelete experimental code (diff)
downloadBouncyCastle.NET-ed25519-6d442f9319a857fe483af22d67bbd1677b0991a1.tar.xz
Use generics in Enums
Diffstat (limited to 'crypto/src/util')
-rw-r--r--crypto/src/util/Enums.cs33
1 files changed, 19 insertions, 14 deletions
diff --git a/crypto/src/util/Enums.cs b/crypto/src/util/Enums.cs
index fb685e2a7..1034b5b7e 100644
--- a/crypto/src/util/Enums.cs
+++ b/crypto/src/util/Enums.cs
@@ -4,38 +4,43 @@ using Org.BouncyCastle.Utilities.Date;
 
 namespace Org.BouncyCastle.Utilities
 {
-    internal abstract class Enums
+    internal static class Enums
     {
-        internal static Enum GetEnumValue(Type enumType, string s)
+        internal static TEnum GetEnumValue<TEnum>(string s)
+            where TEnum : struct, Enum
         {
-            if (!enumType.IsEnum)
-                throw new ArgumentException("Not an enumeration type", nameof(enumType));
-
             // 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)Enum.Parse(enumType, s, false);
+#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 Array GetEnumValues(Type enumType)
+        internal static TEnum[] GetEnumValues<TEnum>()
+            where TEnum : struct, Enum
         {
-            if (!enumType.IsEnum)
-                throw new ArgumentException("Not an enumeration type", nameof(enumType));
-
-            return Enum.GetValues(enumType);
+#if NET5_0_OR_GREATER
+            return Enum.GetValues<TEnum>();
+#else
+            return (TEnum[])Enum.GetValues(typeof(TEnum));
+#endif
         }
 
-        internal static Enum GetArbitraryValue(Type enumType)
+        internal static TEnum GetArbitraryValue<TEnum>()
+            where TEnum : struct, Enum
         {
-            Array values = GetEnumValues(enumType);
+            TEnum[] values = GetEnumValues<TEnum>();
             int pos = (int)(DateTimeUtilities.CurrentUnixMs() & int.MaxValue) % values.Length;
-            return (Enum)values.GetValue(pos);
+            return values[pos];
         }
     }
 }