gpt4 book ai didi

c# - 在 c# develop windows phone 中出现异常

转载 作者:行者123 更新时间:2023-11-30 22:20:03 24 4
gpt4 key购买 nike

System.Runtime.Serialization.InvalidDataContractException was unhandled by user code
HResult=-2146233088
Message=Type 'xxx.Cost+RootObject' cannot be serialized. Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute. Alternatively, you can ensure that the type is public and has a parameterless constructor - all public members of the type will then be serialized, and no attributes will be required.
Source=System.Runtime.Serialization
InnerException:

我看到另一个帖子,有人建议添加数据契约和数据成员。你能帮我把数据成员和数据契约(Contract)放在哪里吗?我还是不明白。

这是我的类(class)代码:

namespace xxx
{
class Cost
{
public class Title
{
public string from { get; set; }
public string to { get; set; }
public string from_zip { get; set; }
public string to_zip { get; set; }
public string from_suburb { get; set; }
public string to_suburb { get; set; }
}

public class Content
{
public string company { get; set; }
public string package { get; set; }
public string rate { get; set; }
public string rate_second { get; set; }
public string est_time { get; set; }
public string inclusion { get; set; }
public string exclusion { get; set; }
public string last_update { get; set; }
}

public class RootObject
{
public Title title { get; set; }
public List<Content> content { get; set; }
}
}
}

最佳答案

您可以在 http://msdn.microsoft.com/en-us/library/ms731073.aspx 阅读有关序列化契约(Contract)的文档。

在您的情况下,您需要使用 DataContract 属性注释您的类,以及您希望使用 DataMember 序列化的成员,如下所示。

using System.Runtime.Serialization;
class Cost
{
[DataContract]
public class Title
{
[DataMember]
public string from { get; set; }
[DataMember]
public string to { get; set; }
[DataMember]
public string from_zip { get; set; }
[DataMember]
public string to_zip { get; set; }
[DataMember]
public string from_suburb { get; set; }
[DataMember]
public string to_suburb { get; set; }
}

[DataContract]
public class Content
{
[DataMember]
public string company { get; set; }
[DataMember]
public string package { get; set; }
[DataMember]
public string rate { get; set; }
[DataMember]
public string rate_second { get; set; }
[DataMember]
public string est_time { get; set; }
[DataMember]
public string inclusion { get; set; }
[DataMember]
public string exclusion { get; set; }
[DataMember]
public string last_update { get; set; }
}

[DataContract]
public class RootObject
{
[DataMember]
public Title title { get; set; }

[DataMember]
public List<Content> content { get; set; }
}
}

关于c# - 在 c# develop windows phone 中出现异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15180194/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com