Paste Int in Generic Enum in C #

Like Cast int for an enum in C # , but my enum is a Generic Type parameter. What is the best way to handle this?

Example:

private T ConvertEnum<T>(int i) where T : struct, IConvertible { return (T)i; } 

Generates Cannot convert type 'int' to 'T' compiler error Cannot convert type 'int' to 'T'

The full code is as follows, where the value may contain int or null.

 private int? TryParseInt(string value) { var i = 0; if (!int.TryParse(value, out i)) { return null; } return i; } private T? TryParseEnum<T>(string value) where T : struct, IConvertible { var i = TryParseInt(value); if (!i.HasValue) { return null; } return (T)i.Value; } 
+49
generics enums casting c #
Apr 30 2018-12-12T00:
source share
4 answers

The easiest way I've found is to force the compiler command to add a cast to object .

 return (T)(object)i.Value; 
+72
Apr 30 '12 at 16:30
source share

Here's a very quick fix that misuses the fact that the runtime creates multiple instances of static generic classes. Unleash your inner optimization daemons!

It really shines when you read Enums from a stream in a general way. Combine with an external class that also caches the base type enum and BitConverter to unleash the awesome.

 void Main() { Console.WriteLine("Cast (reference): {0}", (TestEnum)5); Console.WriteLine("EnumConverter: {0}", EnumConverter<TestEnum>.Convert(5)); Console.WriteLine("Enum.ToObject: {0}", Enum.ToObject(typeof(TestEnum), 5)); int iterations = 1000 * 1000 * 100; Measure(iterations, "Cast (reference)", () => { var t = (TestEnum)5; }); Measure(iterations, "EnumConverter", () => EnumConverter<TestEnum>.Convert(5)); Measure(iterations, "Enum.ToObject", () => Enum.ToObject(typeof(TestEnum), 5)); } static class EnumConverter<TEnum> where TEnum : struct, IConvertible { public static readonly Func<long, TEnum> Convert = GenerateConverter(); static Func<long, TEnum> GenerateConverter() { var parameter = Expression.Parameter(typeof(long)); var dynamicMethod = Expression.Lambda<Func<long, TEnum>>( Expression.Convert(parameter, typeof(TEnum)), parameter); return dynamicMethod.Compile(); } } enum TestEnum { Value = 5 } static void Measure(int repetitions, string what, Action action) { action(); var total = Stopwatch.StartNew(); for (int i = 0; i < repetitions; i++) { action(); } Console.WriteLine("{0}: {1}", what, total.Elapsed); } 

Core i7-3740QM results with optimizations enabled:

 Cast (reference): Value EnumConverter: Value Enum.ToObject: Value Cast (reference): 00:00:00.3175615 EnumConverter: 00:00:00.4335949 Enum.ToObject: 00:00:14.3396366 
+13
Oct 10 '14 at 0:02
source share

You can use Enum.Parse for this:

 return (T)Enum.Parse(typeof(T), i.Value.ToString(), true); 

This article talks about parsing common enumerations for extenstion methods:

+11
Apr 30 2018-12-12T00:
source share
 public static class Extensions { public static T ToEnum<T>(this int param) { var info = typeof(T); if (info.IsEnum) { T result = (T)Enum.Parse(typeof(T), param.ToString(), true); return result; } return default(T); } } 
0
Apr 7 '17 at 11:19 on
source share



All Articles