0

我写了一个带有可选参数的网络方法。

[WebMethod]
  public void EmailSend(string from, string to, string cc = null, string bcc = null, string replyToList = null, string subject = null, string body = null, bool isBodyHtml = false , string[] attachmentNames = null, byte[][] attachmentContents = null)
    {
     .....
    }

我在客户端应用程序中调用此方法

 EmailServiceManagement.EmailService es = new EmailServiceManagement.EmailService();
 es.EmailSend(from, to,null,null,null,subject,body,true,attName,att); //this works

es.EmailSend(from,to); // this isn't working. According to c# optional parameter syntax it must work.

我究竟做错了什么?

4

1 回答 1

2

WebMethods 上不能有可选参数。你可以做的是有这样的重载方法:

[WebMethod(MessageName="Test")]
public string GenerateMessage(string firstName)
{
   return string.Concat("Hi ", firstName);
}

[WebMethod(MessageName="AnotherTest")]
public string GenerateMessage(string firstName, string lastName)
{
   return string.Format("Hi {0} {1}", firstName, lastName);
}

不确定您是如何与此 WebMethod 交互的,但有这么多参数可能表明您可以将它们分组到一个对象中,例如:

[WebMethod]
public void EmailSend(MessageParameters messageParams)
{
     .....
}
于 2013-05-21T07:29:31.480 回答