【问题标题】:To be XML serializable, types which inherit from IEnumerable must have an implementation of Add(System.Object)要成为 XML 可序列化的,从 IEnumerable 继承的类型必须具有 Add(System.Object) 的实现
【发布时间】:2016-10-24 07:57:07
【问题描述】:

我正在尝试构建一个 Web 服务以从外部获取邮件服务器的附件并从那里发送,但出现以下错误:

要成为 XML 可序列化的,从 IEnumerable 继承的类型必须具有 Add(System.Object) 在其所有级别的实现 继承层次结构。 System.Collections.Specialized.StringDictionary 不实现 Add(System.Object)。 [无效操作异常: 反映类型“System.Net.Mail.Attachment”时出现错误。]

我的代码如下:

[WebMethod]
    public String SendMailWithAttachment(string mail_sender, string[] mail_receiver, string mail_subject, string mail_text, Attachment att)
...

【问题讨论】:

    标签: c# web-services serialization soap


    【解决方案1】:

    System.Net.Mail.Attachment 并非设计为序列化为 xml。您需要发送构建附件所需的数据,并在您的 Web 方法的正文中使用它。例如,发送带有数据的字节数组和带有附件名称的字符串:

    [WebMethod]
    public String SendMailWithAttachment(string mail_sender, string[] mail_receiver, string mail_subject, string mail_text, byte[] attachment_data, string attachment_name){
       using(var ms = new MemoryStream(attachment_data)){
           var attachment = new System.Net.Mail.Attachment(ms,attachment_name);
           ...
       }
    }
    

    【讨论】:

      猜你喜欢
      • 2013-05-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-04
      • 2010-09-07
      • 1970-01-01
      • 1970-01-01
      • 2016-06-18
      相关资源
      最近更新 更多