可序列化类上的 WP7 应用程序上的 InvalidDataContractException
当尝试将对象保存到独立存储中时,我遇到了一个奇怪的错误。我有一个具有一些属性的类,这里是代码:
[DataContract]
public class ExerciseStatistic
{
[XmlIgnore]
public int CorrectAnswers
{
get
{
return Attempts.Where(a => a.AttemptAnswerIsCorrect).Count();
}
}
[XmlIgnore]
public int IncorrectAnswers
{
get
{
return Attempts.Where(a => !a.AttemptAnswerIsCorrect).Count();
}
}
[XmlIgnore]
public int AnswerAttempts
{
get { return Attempts.Count; }
}
public List<AnswerAttempt> Attempts { get; set; }
public ExerciseStatistic()
{
Attempts = new List<AnswerAttempt>();
}
}
public class AnswerAttempt
{
public DateTime AttemptDate { get; set; }
public string AttemptTargetName { get; set; }
public string AttemptName { get; set; }
public bool AttemptAnswerIsCorrect { get; set; }
}
但是,当尝试用这句话保存它时:
IsolatedStorageSettings.ApplicationSettings["a"] = new ExerciseStatistic()
{
Attempts = new List<AnswerAttempt>()
{
new AnswerAttempt()
{
AttemptAnswerIsCorrect = true,
AttemptDate = DateTime.Now,
AttemptName = "lala",
AttemptTargetName = "lala2"
},
new AnswerAttempt()
{
AttemptAnswerIsCorrect = false,
AttemptDate = DateTime.Now,
AttemptName = "lalab",
AttemptTargetName = "lalab2"
}
}
};
我收到一个像这样的异常(我用假名称更改了代码的签名,但是对于例如它达到了它的目的):
类型“XX.Model.FirstClass.SecondClass”无法序列化。考虑 使用 DataContractAttribute 属性对其进行标记,并标记所有 您希望使用 DataMemberAttribute 序列化其成员的数量 属性。
我不明白为什么序列化器试图序列化我的模型的对象(不可序列化),而我提供的类没有对该类型的任何引用......我缺少什么? ->不,我不想将数据契约属性添加到我不需要且不打算序列化的类中,所以请不要回答这个:)
I'm having an strange error when trying to save an object into isolated storage. I have a class that has some properties, here's the code :
[DataContract]
public class ExerciseStatistic
{
[XmlIgnore]
public int CorrectAnswers
{
get
{
return Attempts.Where(a => a.AttemptAnswerIsCorrect).Count();
}
}
[XmlIgnore]
public int IncorrectAnswers
{
get
{
return Attempts.Where(a => !a.AttemptAnswerIsCorrect).Count();
}
}
[XmlIgnore]
public int AnswerAttempts
{
get { return Attempts.Count; }
}
public List<AnswerAttempt> Attempts { get; set; }
public ExerciseStatistic()
{
Attempts = new List<AnswerAttempt>();
}
}
public class AnswerAttempt
{
public DateTime AttemptDate { get; set; }
public string AttemptTargetName { get; set; }
public string AttemptName { get; set; }
public bool AttemptAnswerIsCorrect { get; set; }
}
However, when trying to save it with this sentence :
IsolatedStorageSettings.ApplicationSettings["a"] = new ExerciseStatistic()
{
Attempts = new List<AnswerAttempt>()
{
new AnswerAttempt()
{
AttemptAnswerIsCorrect = true,
AttemptDate = DateTime.Now,
AttemptName = "lala",
AttemptTargetName = "lala2"
},
new AnswerAttempt()
{
AttemptAnswerIsCorrect = false,
AttemptDate = DateTime.Now,
AttemptName = "lalab",
AttemptTargetName = "lalab2"
}
}
};
I'm getting an exception like this one (i changed a bit the signature of the code with fake names, but for the example it serves its purpose) :
Type 'XX.Model.FirstClass.SecondClass' cannot be serialized. Consider
marking it with the DataContractAttribute attribute, and marking all
of its members you want serialized with the DataMemberAttribute
attribute.
I don't understand why the serializer is trying to serialize an object of my model (which is not serializable) when the class that I'm giving it doesn't have any references to that kind of type... what am i missing? -> nope, i don't want to add datacontract attributes to classes that i don't need and am not planning to serialize, so please don't answer with this :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您执行“演练:使用适用于 Windows Phone 的 MVVM 的 OData”中的参考过程,您可能会遇到此问题,网址为 http://msdn.microsoft.com/en-us/library/hh394007(v=VS.92).aspx
当您到达拨打电话的位置时:
您可能会收到InvalidDataContractException 并显示消息:
无法序列化类型“DataBoundApp1.Northwind.NorthwindEntities”。考虑使用 DataContractAttribute 属性标记它,并使用 DataMemberAttribute 属性标记您想要序列化的所有成员。
感谢 Daniel Perez 的回答,我能够解决此问题,并且我正在记录我的步骤以澄清问题其他人的解决方案:
Reference.datasvcmap)
属性,添加如下所示:
。
添加 DataContract 属性后,问题就消失了!
You might experience this problem if you work through the reference procedure in "Walkthrough: Consuming OData with MVVM for Windows Phone" at http://msdn.microsoft.com/en-us/library/hh394007(v=VS.92).aspx
When you get to the point where you call :
You might get an InvalidDataContractException with the message:
Type 'DataBoundApp1.Northwind.NorthwindEntities' cannot be serialized. Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute.
Thanks to the answer by Daniel Perez, I was able to resolve this problem and I am documenting my steps to clarify the solution for others:
Reference.datasvcmap)
attribute, add it as shown here:
.
Once I added the DataContract attribute, the problem went away!
在我看来,您尝试使用
XmlIgnore
从序列化中排除属性。来自文档:
因此,请尝试使用
IgnoreDataMemberAttribute
而不是XmlIgnore
来选择从序列化中排除成员。在与您完全相同的情况下,我也遇到了
DataContract
的问题,因此我恢复到普通旧式 XML 序列化为字符串,然后将其存储在独立存储中。这也简化了调试。It seems to me you try to exclude properties from serialization by using
XmlIgnore
.From the documentation:
so try using
IgnoreDataMemberAttribute
instead ofXmlIgnore
to opt out members from serialization.I also had some troubles with
DataContract
in the very same situation as you, therefore I reverted to plain old XML serialization to strings, which i then stored in isolated storage. This also eases debugging.我认为这不是一个正确的答案,但这是我必须做的才能解决它。
在更改更多代码后,我意识到即使我没有将任何内容保存到独立存储中,这也会失败。仅在类型上声明 DataContract 属性就会出现错误。我必须认为 WP7 的框架在某些时候正在解析具有此属性的所有类,并且出于一些奇怪和模糊的原因(我找不到)它也在其他类中寻找它们。我在框架正在抱怨的类中添加了 DataContract 属性,还有一些 KnownType 属性,一切都开始顺利运行......很奇怪......如果有人能对此有所了解,我会的快乐(我讨厌当我解决问题但不知道确切原因时)
I don't think that this is a proper answer, but it's what i had to do in order to fix it.
After changing some more the code, i realised that this was failing EVEN if I wasn't saving anything to the isolated storage. Just declaring a DataContract attribute on the type made the error arise. I must think that WP7's framework at some point is parsing all classes that have this attribute, and for some strange and obscure reason (which i can't find) it's looking for them in other classes as well. I added the DataContract attributes in the classes that the framework is complaning about, and also some KnownType attributes as well, and everything started to run smoothly... weird weird... if someone can shed some light into this, i'd be happy (i hate it when i solve a problem but without knowing the exact cause)