我将枚举用作标志,并希望“或”它,但是总是遇到错误:

枚举值“ 3”对于类型“ Common.EventType”无效,并且无法序列化。如果类型具有DataContractAttribute属性,请确保存在必要的枚举值并用EnumMemberAttribute属性标记

码:

[DataContract]
public enum EventType
{
    [EnumMember]
    Event1 = 1,
    [EnumMember]
    Event2 = 2,
    [EnumMember]
    Event3 = 4,
    [EnumMember]
    AllEvents = Event1 | Event2 | Event3
}


MyContractClient代理=新的MyContractClient(上下文);

proxy.Subscribe(EventType.Event1 | EventType.Event2);

先感谢您

最佳答案

您需要添加[Flags]属性以通过WCF发送标志。

Enumeration Types in Data Contracts

[DataContract][Flags]
public enum EventType
{
  [EnumMember]
  Event1 = 1,
  [EnumMember]
  Event2 = 2,
  [EnumMember]
  Event3 = 4,
  [EnumMember]
  AllEvents = Event1 | Event2 | Event3
}

09-12 18:21