我实现了一个将两个Enum值作为参数的方法:
void HandleEnumValue( System.Enum v, System.Enum bits )
两个参数的实际枚举类型不一定相同..
在这个方法中,我假设枚举的基础类型是int。 现在这是第一次用枚举值调用此方法 基础类型,方法现在抛出异常。
我喜欢增强该方法,以便它可以使用任何枚举值,无论如何 基础类型。我有一个实现,但我不喜欢 非常。
我想知道是否有更通用/可维护的解决方案?
唉,我不能使用泛型类型参数,因为这会引入一个 打破了库中的变化,以及对我的方法的数以万计的调用 必须更换。
以下是代码,以及我不太好的解决方案:
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 ) ;
// will throw InvalidCastException: HandleEnumValueOriginal( ByteEnum.SomeByte, ByteEnum.One ) ;
// will throw InvalidCastException: HandleEnumValueOriginal( ShortEnum.SomeShort, ShortEnum.One ) ;
HandleEnumValueOriginal( IntEnum.SomeInt, IntEnum.One ) ;
// will throw InvalidCastException: HandleEnumValueOriginal( LongEnum.SomeLong, LongEnum.One ) ;
}
// new implementation, that I dislike.
static void HandleEnumValue( System.Enum v, System.Enum bits )
{
// assert underlying types of v and bits are equal
if ( v.GetTypeCode() != bits.GetTypeCode() ) throw new Exception( "enum type code mismatch" ) ;
bool hasBitsSet = false ; // won't compile: bool hasBitsSet = ( ( v & bits ) == bits ) ;
long lvalue = 0L ; // will throw : lvalue = (long)(object)v
switch ( v.GetTypeCode() )
{
case TypeCode.Byte :
hasBitsSet = ( ( (byte)(object)v & (byte)(object)bits ) == (byte)(object)bits ) ;
lvalue = (long)(byte)(object)v ;
break ;
case TypeCode.Int16 :
hasBitsSet = ( ( (short)(object)v & (short)(object)bits ) == (short)(object)bits ) ;
lvalue = (long)(short)(object)v ;
break ;
case TypeCode.Int32 :
hasBitsSet = ( ( (int)(object)v & (int)(object)bits ) == (int)(object)bits ) ;
lvalue = (long)(int)(object)v ;
break ;
case TypeCode.Int64 :
hasBitsSet = ( ( (long)(object)v & (long)(object)bits ) == (long)(object)bits ) ;
lvalue = (long)(object)v ;
break ;
}
Console.WriteLine( "(new) enum value = " + v.ToString() ) ;
Console.WriteLine( "(new) number value = " + lvalue.ToString() ) ;
Console.WriteLine( "(new) has bits set = " + hasBitsSet.ToString() ) ;
// further processing ...
}
// original implementation, that doesn't work anymore
static void HandleEnumValueOriginal( System.Enum v, System.Enum bits )
{
int lvalue = (int)(object)v ;
bool hasBitsSet = ( ( (int)(object)v & (int)(object)bits ) == (int)(object)bits ) ;
Console.WriteLine( "(original) enum value = " + v.ToString() ) ;
Console.WriteLine( "(original) number value = " + lvalue.ToString() ) ;
Console.WriteLine( "(original) has bits set = " + hasBitsSet.ToString() ) ;
// further processing ...
}
}
答案 0 :(得分:4)
在.NET 4中,添加的方法几乎可以满足您的需求:Enum.HasFlag。
不幸的是,如果实际的枚举类型不同,则抛出此方法。它的实现非常简单,无需枚举类型检查即可复制,或者如果您使用的是旧版本的框架:
static void HandleEnumValue(Enum v, Enum bits) {
ulong enumValue = ToUInt64(v);
ulong bitsValue = ToUInt64(bits);
bool hasBitsSet = (enumValue & bitsValue) == bitsValue;
Console.WriteLine("enum value = " + v);
Console.WriteLine("number value = " + bits);
Console.WriteLine("has bits set = " + hasBitsSet);
}
internal static ulong ToUInt64(Enum value) {
switch (Convert.GetTypeCode(value)) {
case TypeCode.SByte:
case TypeCode.Int16:
case TypeCode.Int32:
case TypeCode.Int64:
return (ulong) Convert.ToInt64(value);
case TypeCode.Byte:
case TypeCode.UInt16:
case TypeCode.UInt32:
case TypeCode.UInt64:
return Convert.ToUInt64(value);
default:
throw new ArgumentOutOfRangeException("value");
}
}
此方法将处理任何类型的枚举和任何基础类型,即使它们不同。