C#中的大型标志枚举

时间:2010-05-07 22:34:50

标签: c# enumeration endianness flags

嘿大家,得到一个我似乎无法找到任何关于......的快速问题。

我正在开发一个项目,需要带有大量标志(最多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
}

此外,我也希望有一种简单的方法可以控制不同端机器上的实际比特排列,因为这些值最终将通过网络序列化:

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
}

只是想知道是否有人有任何很酷的想法,谢谢!

4 个答案:

答案 0 :(得分:13)

为什么不这样做:

public enum MyEnumeration : ulong 
{ 
    Flag1 = 1, 
    Flag2 = 1 << 1, 
    Flag3 = 1 << 2, 
    Flag4 = 1 << 3, 
    .
    .
    .
    Flag30 = 1 << 29, 
    Flag31 = 1 << 30, 
    Flag32 = 1 << 31
}

答案 1 :(得分:11)

您可以编写T4模板来生成枚举:

模板(MyEnumeration.tt)

<#@ template language="C#" #>
<#@ output extension=".cs" #>
using System;

namespace MyNamespace
{
    [Flags]
    public enum MyEnumeration : ulong
    {
<#
    ulong value = 1;
    for(int i = 1; i <= 64; i++)
    {
#>
        Flag<#= i #> = <#= string.Format("0x{0:X8}", value) #>,
<#
        value = value << 1;
    }
#>
    }
}

生成的C#代码(MyEnumeration.cs)

using System;

namespace MyNamespace
{
    [Flags]
    public enum MyEnumeration : ulong
    {
        Flag1 = 0x00000001,
        Flag2 = 0x00000002,
        Flag3 = 0x00000004,
        Flag4 = 0x00000008,
        Flag5 = 0x00000010,
        Flag6 = 0x00000020,
        Flag7 = 0x00000040,
        Flag8 = 0x00000080,
        Flag9 = 0x00000100,
        Flag10 = 0x00000200,
        Flag11 = 0x00000400,
        Flag12 = 0x00000800,
        Flag13 = 0x00001000,
        Flag14 = 0x00002000,
        Flag15 = 0x00004000,
        Flag16 = 0x00008000,
        Flag17 = 0x00010000,
        Flag18 = 0x00020000,
        Flag19 = 0x00040000,
        Flag20 = 0x00080000,
        Flag21 = 0x00100000,
        Flag22 = 0x00200000,
        Flag23 = 0x00400000,
        Flag24 = 0x00800000,
        Flag25 = 0x01000000,
        Flag26 = 0x02000000,
        Flag27 = 0x04000000,
        Flag28 = 0x08000000,
        Flag29 = 0x10000000,
        Flag30 = 0x20000000,
        Flag31 = 0x40000000,
        Flag32 = 0x80000000,
        Flag33 = 0x100000000,
        Flag34 = 0x200000000,
        Flag35 = 0x400000000,
        Flag36 = 0x800000000,
        Flag37 = 0x1000000000,
        Flag38 = 0x2000000000,
        Flag39 = 0x4000000000,
        Flag40 = 0x8000000000,
        Flag41 = 0x10000000000,
        Flag42 = 0x20000000000,
        Flag43 = 0x40000000000,
        Flag44 = 0x80000000000,
        Flag45 = 0x100000000000,
        Flag46 = 0x200000000000,
        Flag47 = 0x400000000000,
        Flag48 = 0x800000000000,
        Flag49 = 0x1000000000000,
        Flag50 = 0x2000000000000,
        Flag51 = 0x4000000000000,
        Flag52 = 0x8000000000000,
        Flag53 = 0x10000000000000,
        Flag54 = 0x20000000000000,
        Flag55 = 0x40000000000000,
        Flag56 = 0x80000000000000,
        Flag57 = 0x100000000000000,
        Flag58 = 0x200000000000000,
        Flag59 = 0x400000000000000,
        Flag60 = 0x800000000000000,
        Flag61 = 0x1000000000000000,
        Flag62 = 0x2000000000000000,
        Flag63 = 0x4000000000000000,
        Flag64 = 0x8000000000000000,
    }
}

为了编辑T4模板,我建议您使用像this one这样的T4编辑器插件(这会为您提供语法高亮和智能感知)

答案 2 :(得分:0)

为了解决这个问题,你有两个选择,我可以从脑海中想到

1-处理自己的序列化并使用System.Net.IPAddress.HostToNetworkOrder以确保线路上的字节顺序一致,当然在反序列化时与System.Net.IPAddress.NetworkToHostOrder相反。

我有关于二进制序列化主题的两篇旧帖子,他们可以使用更新,但这是一个起点。

http://taylorza.blogspot.com/2010/04/archive-binary-data-from-structure.html
http://taylorza.blogspot.com/2010/04/archive-structure-from-binary-data.html

2- Serialize to XML,在这种情况下,endianes不是问题,但当然还有其他缺点,如有效负载大小和一般性能。

答案 3 :(得分:0)

像这样使用1UL

[Flags]    
    public enum EventType : ulong
    {
        f1= 1,
        f2= 1 << 1,
        f3= 1 << 2,
        ......

        f... = 1 << 30,
        f... = 1UL << 31,
        f...  = 1UL << 32        
    }