嘿大家,有一个快速的问题,我似乎找不到任何关于...
我正在开发一个需要带有大量标志(最多 40 个)的标志枚举的项目,我真的不想为每个枚举值输入确切的掩码:
public enum MyEnumeration : ulong
{
Flag1 = 1,
Flag2 = 2,
Flag3 = 4,
Flag4 = 8,
Flag5 = 16,
// ...
Flag16 = 65536,
Flag17 = 65536 * 2,
Flag18 = 65536 * 4,
Flag19 = 65536 * 8,
// ...
Flag32 = 65536 * 65536,
Flag33 = 65536 * 65536 * 2
// right about here I start to get really pissed off
}
此外,我还希望有一种简单(更)的方法来控制不同字节序机器上位的实际排列,因为这些值最终将通过网络进行序列化:
public enum MyEnumeration : uint
{
Flag1 = 1, // BIG: 0x00000001, LITTLE:0x01000000
Flag2 = 2, // BIG: 0x00000002, LITTLE:0x02000000
Flag3 = 4, // BIG: 0x00000004, LITTLE:0x03000000
// ...
Flag9 = 256, // BIG: 0x00000010, LITTLE:0x10000000
Flag10 = 512, // BIG: 0x00000011, LITTLE:0x11000000
Flag11 = 1024 // BIG: 0x00000012, LITTLE:0x12000000
}
所以,我有点想知道是否有一些很酷的方法可以设置我的枚举,例如:
public enum MyEnumeration : uint
{
Flag1 = flag(1), // BOTH: 0x80000000
Flag2 = flag(2), // BOTH: 0x40000000
Flag3 = flag(3), // BOTH: 0x20000000
// ...
Flag9 = flag(9), // BOTH: 0x00800000
}
我试过的:
// this won't work because Math.Pow returns double
// and because C# requires constants for enum values
public enum MyEnumeration : uint
{
Flag1 = Math.Pow(2, 0),
Flag2 = Math.Pow(2, 1)
}
// this won't work because C# requires constants for enum values
public enum MyEnumeration : uint
{
Flag1 = Masks.MyCustomerBitmaskGeneratingFunction(0)
}
// this is my best solution so far, but is definitely
// quite clunkie
public struct EnumWrapper<TEnum> where TEnum
{
private BitVector32 vector;
public bool this[TEnum index]
{
// returns whether the index-th bit is set in vector
}
// all sorts of overriding using TEnum as args
}
只是想知道是否有人有任何很酷的想法,谢谢!