[英]Passing enum value as parameter
I am having trouble in passing multiple enum
values from the WCF Client. 我在从WCF客户端传递多个
enum
值时遇到麻烦。
class Client
{
static void Main(string[] args)
{
ChannelFactory<IWCFService> channelFactor = new ChannelFactory<IWCFService>("HellowWorldServiceEndpoint");
channelFactor.Open();
IWCFService proxy = channelFactor.CreateChannel();
Console.WriteLine(proxy.GetEnumString(EnumComponents.enumVal1)); // -> SUCCEDED HERE (only one Enum value)
// Console.WriteLine(proxy.GetEnumString(EnumComponents.enumVal1| EnumComponents.enumVal2)); // -> **** FAILS HERE with invalid enum type. *****
Console.Read();
}
}
Exception is, There was an error while trying to serialize parameter http://tempuri.org/:components . 例外是,尝试序列化参数http://tempuri.org/:components时发生错误。 The InnerException message was 'Enum value 'enumVal1, enumVal2' is invalid for type 'Microsoft.Geospatial.Data.Gateway.ObjectModel.EnumComponents' and cannot be serialized.
InnerException消息为“枚举值'enumVal1,enumVal2'对于类型'Microsoft.Geospatial.Data.Gateway.ObjectModel.EnumComponents'无效,并且无法序列化。 Ensure that the necessary enum values are present and are marked with EnumMemberAttribute attribute if the type has DataContractAttribute attribute.'.
如果类型具有DataContractAttribute属性,请确保存在必要的枚举值并用EnumMemberAttribute属性标记。 Please see InnerException for more details
请参阅InnerException了解更多详细信息
FYI, the enum is from a common library that is added as a reference in WCFclient, WCFserver and WCFinterface. 仅供参考,该枚举来自一个公共库,该库在WCFclient,WCFserver和WCFinterface中作为引用添加。
The same woks if it is a normal function call. 如果是正常的函数调用,则相同的炒锅。 Is there a possible way to solve this issue?
有没有可能解决此问题的方法?
The only reason for you to send a bunch of |
您发送一堆邮件的唯一原因
|
-ed enum
values is in a case where the enum
symbolizes flags. -ed
enum
值是在enum
符号表示标志的情况下。
WCF
supports using enums
with DataContract
in few cases - one of them is a flags enum . WCF
在少数情况下支持将enums
与DataContract
一起使用-其中之一是标志枚举 。
In a case where your enum
has FlagsAttribute
decorating it, the serialization is done in the following way: 在您的
enum
具有FlagsAttribute
装饰的情况下,可以通过以下方式完成序列化:
- Attempt to find an enumeration member (with the EnumMemberAttribute attribute applied) that maps to the numeric value.
尝试找到一个映射到数值的枚举成员( 已应用EnumMemberAttribute属性)。 If found, send a list that contains just that member.
如果找到,请发送仅包含该成员的列表。
- Attempt to break the numeric value into a sum such that there are enumeration members (each with the EnumMemberAttribute attribute applied) that map to each part of the sum.
尝试将数值分解为一个总和,以使存在映射到总和各部分的枚举成员(每个枚举成员都应用了EnumMemberAttribute属性)。 Send the list of all these members.
发送所有这些成员的列表。 Note that the greedy algorithm is used to find such a sum, and thus there is no guarantee that such a sum is found even if it is present.
注意,贪心算法用于找到这样的和,因此即使存在,也不能保证找到这样的和。 To avoid this problem, make sure that the numeric values of the enumeration members are powers of two.
为避免此问题,请确保枚举成员的数值是2的幂。
- If the preceding two steps fail, and the numeric value is nonzero, throw a SerializationException.
如果前两个步骤失败,并且数值不为零,则引发SerializationException。 If the numeric value is zero, send the empty list.
如果数值为零,则发送空列表。
Therefore, you need your enum to look like the following: 因此,您需要您的枚举如下所示:
[DataContract]
[Flags]
enum EnumComponents
{
[EnumMember]
enumVal1,
[EnumMember]
enumVal2
}
If the enum
is from an external source and it doesn't have the FlagsAttribute
you will have to break the numeric value into a sum of enumeration members decorated with EnumMemberAttribute
yourself. 如果
enum
来自外部来源,并且没有FlagsAttribute
,则必须将数值自己分解为用EnumMemberAttribute
装饰的枚举成员的总和。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.