Is it possible to define the usage of bits in the DataContract of a WCF service?

64 views Asked by At

I have a C++ program with a typical mode int that uses a bitmask. This bitmask is defined in an enum like this:

enum EModeEntryPoint
{
    // Mode
    entryPointNone              =0x00,
    entryPointNormal            =0x01,
    entryPointExistingAddress   =0x02,
    entryPointNewAddress        =0x04,
    entryPointNewAndExisting    =0x06,  
    entryPointOnlyNewTickets    =0x08,  
};

When I use an enum in a C# WCF service like this

[DataContractAttribute]
public enum ModeEntryPoint
{
    // Mode
    [EnumMember] None              =0x00,
    [EnumMember] Normal            =0x01,
    [EnumMember] ExistingAddress   =0x02,
    [EnumMember] NewAddress        =0x04,
    [EnumMember] NewAndExisting    =0x06,  
    [EnumMember] OnlyNewTickets    =0x08,  
};

I can see that the values I define here, are not used. I can see that such enums are always "renumbered".

Is it possible to define such bit-usage in the contract anywhere?

Creating a bool field for each bit isn't what I like. Also it bloats the data block.

1

There are 1 answers

0
xMRi On

The [Flags] keyword is the solution.

[DataContractAttribute][Flags]
public enum ModeEntryPoint
{
    // Mode
    [EnumMember] None = 0x00,
    [EnumMember] Normal = 0x01,
    [EnumMember] ExistingAddress = 0x02,
    [EnumMember] NewAddress = 0x04,
    [EnumMember] 
    NewAndExisting = ExistingAddress| NewAddress,  
    [EnumMember] OnlyNewTickets = 0x08,  
};

This allows the WCF client to deocde the specific fields of this type.