0

I Have a email class as

public class EmailInfo
{
    public MailAddress SenderEmailAddress { get; set; }
    public MailAddressCollection ReceiverEmailAddress { get; set; }
    public MailAddressCollection CCEmailAddress { get; set; }
    public MailAddressCollection BCCEmailAddress { get; set; }
    public AttachmentCollection Attachment { get; set; }
    public string Subject { get; set; }
    public DateTime EmailDate { get; set; }
}

and When I Try to serialize list of Type EmailInfo. I get the the Following error

Type 'System.Collections.Generic.List`1[[Darena.EmailParser.EmailInfo, Darena.EmailParser, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]' with data contract name 'ArrayOfEmailInfo:http://schemas.datacontract.org/2004/07/Darena.EmailParser' is not expected. Consider using a DataContractResolver or add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer.

I am Serializing using

 DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(EmailInfo));
        MemoryStream ms = new MemoryStream();
        ser.WriteObject(ms, emailInfos);
        string jsonString = Encoding.UTF8.GetString(ms.ToArray());
        ms.Close();
        return jsonString;

Any help

4

2 回答 2

0

您要序列化的类型必须是可序列化的。

因为DataContractJsonSerializer这意味着你必须EmailInfo用属性来装饰你的类()DataContract及其成员(只是那些你想包含在序列化字符串中的)DataMember

但是,在您的情况下,您引用的是 3rd 方类型,例如MailAddress和其他不可序列化的类型,因此您必须提供某种类型的自定义序列化,并且对于DataContractJsonSerializer.

看看Json.NET库,它为 JSON 序列化提供了更多的灵活性,例如。JsonConverter. 你会在这个网站上找到很好的例子。

于 2014-05-12T07:08:41.090 回答
0

List 不支持序列化。您可以使用 object[] 而不是 List。

于 2014-05-12T07:14:49.310 回答