我在服务中编写了以下代码。
[DataContract]
public class Email
{
private string senderEmail;
private MyEmailType emailType;
[DataMember]
public string SenderEmail { get { return senderEmail; } set { senderEmail = value; } }
[DataMember]
public MyEmailType EmailType { get { return emailType; } set { emailType = value; } }
}
[DataContract]
public enum MyEmailType
{
[EnumMember]
EmailType1 = 1,
[EnumMember]
EmailType2= 2
}
我将电子邮件对象发送到服务,如下所示:
Email email = new Email();
email.SenderEmail = "someemail@mail.com";
email.EmailType = MyEmailType.EmailType1;
bool isDispatchSuccessful = myClientObject.SendMassProcessEmail(email);
当我到达服务时,SenderEmail属性的值是正确的,但EmailType(枚举)得到0而不是1(EmailType1)。
为什么会这样?
修改:
我尝试将除Email对象之外的MyEmailType变量传递给SendMassProcessEmail,如下所示:
bool isDispatchSuccessful = myClientObject.SendMassProcessEmail(email, MyEmailType.EmailType1);
现在,独立的枚举变量接收到正确的值,但是作为Email类的公共成员的值仍然获得值0.
答案 0 :(得分:0)
要回答问题,请参阅此链接http://msdn.microsoft.com/en-us/library/aa347875(v=vs.110).aspx
如果你真的想传递实际值,我认为你应该这样做:
[DataContract]
public enum MyEmailType
{
[EnumMember(Value = 1)]
EmailType1 = 1,
[EnumMember(Value = 2)]
EmailType2= 2
}
答案 1 :(得分:-1)
您必须将[DataContract]添加到支持字段:
[DataContract]
public class Email
{
[DataMember]
private string senderEmail;
[DataMember]
private MyEmailType emailType;
public string SenderEmail { get { return senderEmail; } set { senderEmail = value; } }
public MyEmailType EmailType { get { return emailType; } set { emailType = value; } }
}
答案 2 :(得分:-1)
我遇到了同样的麻烦但在我的情况下,参数名称是错误的。
[ServiceContract]
public interface ISendMail
{
/// <summary />
/// <param name="emailType">This parameter should be 'myEmailType'</param>
[OperationContract]
void Send(string senderEmail, MyEmailType emailType);
}
在我的情况下,第二个参数是服务器端的myEmailType,但是,当我在客户端创建界面时,我忘记了&#39; my&#39;。一个愚蠢的错误可以为这个问题带来一些启示。