I implemented a method that takes two Enum values as parameters:
void HandleEnumValue( System.Enum v, System.Enum bits )
The actual type of enumeration is not necessarily the same for the two parameters.
In this method, I assumed that the base enumeration type is int. Now for the first time this method is called with enumeration values that are long as the base type, and the method now throws an exception.
I like to improve the method so that it can accept any enum value, regardless of the underlying type. I have an implementation for this, but I don't like it very much.
I wonder if there is a more general / supported solution?
Alas, I can’t use the type parameter, because this would lead to a violation of changes in the library and the number of calls to my method would have to be replaced.
Here's the code and my not-so-good solution:
using System; [Flags()] public enum ByteEnum : byte { One = 1, SomeByte = 0x42 } [Flags()] public enum ShortEnum: short{ One = 1, SomeShort = 0x4223 } [Flags()] public enum IntEnum : int { One = 1, SomeInt = 0x42230815 } [Flags()] public enum LongEnum : long { One = 1, SomeLong = 0x4223081547112012L } public class Program { public static void Main() { HandleEnumValue( ByteEnum.SomeByte, ByteEnum.One ) ; HandleEnumValue( ShortEnum.SomeShort, ShortEnum.One ) ; HandleEnumValue( IntEnum.SomeInt, IntEnum.One ) ; HandleEnumValue( LongEnum.SomeLong, LongEnum.One ) ;
source share