1

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.

xMRi
  • 14,982
  • 3
  • 26
  • 59
  • do you want to use `DataContract` or `DataContractAttribute` ? – Vivek Nuna Oct 07 '21 at 08:13
  • 1
    I think you should be specifying the enum values, e.g. `[EnumMember(Value="ExistingAddress")]` – Matthew Watson Oct 07 '21 at 08:14
  • @MatthewWatson Thanks. I will look at this! – xMRi Oct 07 '21 at 08:24
  • 2
    also: if those are bits: consider marking the enum with `[Flags]`; it may also be useful to consider `None = 0`, `Normal = 1 << 0`, `ExistingAddress = 1 << 1`, `NewAddress = 1 << 2`, `NewAndExisting = NewAddress | ExistingAddress` – Marc Gravell Oct 07 '21 at 08:36
  • Maybe you can take a look at [the docs](https://learn.microsoft.com/en-us/dotnet/framework/wcf/feature-details/enumeration-types-in-data-contracts?redirectedfrom=MSDN) and find some solution. – Jiayao Oct 07 '21 at 09:28
  • The [Flags] keyword is the solution. – xMRi Oct 08 '21 at 09:04

1 Answers1

1

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.

xMRi
  • 14,982
  • 3
  • 26
  • 59