嘿大家,有一个快速的问题,我似乎无法找到任何东西...大标志枚举
我工作的一个项目,需要有大量的标志旗枚举(高达40-ISH),我真的不觉得自己的确切面具每个枚举值输入:
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
}
此外,我也希望有一个简单的(IER)的方式对我来说以控制不同端机上位的实际排列,因为这些值最终将通过网络串行化:
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
}
刚想知道如果有人有任何很酷的想法,谢谢!
您输入几百行文字在这里。你为什么不咬一口就打出原来的40行? (你可能已经使用了1 << 1,1 << 2,...而不是乘法,但是无论如何...) – 2010-05-07 22:45:24
嗯......我确实想提出我曾试过的解决方案,“<<" and ">>”会有工作,但当他们只被评估一次时,我不会完全看到差异。它不需要花费我半个小时才能输出,几百行文字并不多...我宁愿提供太多的信息,也不能太少... – LorenVS 2010-05-07 22:51:42
您可以使用全变量运算符的ulong,您只需向编译器指出您正在转换的是一个ulong。 1ul << 63,注意'ul'的1. – 2010-05-07 23:09:19