Silverlight序列化/反序列化问题
我正在寻找一种方法将 Silverlight 对象保留到用户的 PC,然后重新水合它们,以便用户可以完成它们的编辑。
使用 DataContractSerializer
进行序列化并保存到 IsolatedStorageFile
效果很好。但是,反序列化会导致问题。以下是导致失败的代码:
private string _FirstNames = string.Empty;
public string FirstNames
{
get { return _FirstNames; }
set
{
new PersonNameValidator().Validate(value); //<-- BOOM 8(
Set(ref _FirstNames, value, () => this.FirstNames);
}
}
反序列化器调用属性设置器,该属性设置器又抛出异常并中止反序列化。
我尝试显式应用 DataContract
/DataMember
/IgnoreDataMember
属性,但它不能很好地处理私有字段:
系统.Security.SecurityException 发生消息=“数据合同 类型 'Trident.Model.Journey.JourneyApplication' 无法序列化,因为 成员“_TravellerSavingsAmount”是 不公开。公开成员 将修复此错误。或者, 您可以将其设置为内部,然后使用 InternalsVisibleToAttribute 属性 在您的程序集上以便启用 内部成员的序列化 - 请参阅文档了解更多详细信息。是 意识到这样做有一定的 安全隐患。”
如何在反序列化过程中绕过属性设置器?
我想让我的课程专注于域,而不是受到基础设施问题的过多污染。
I'm looking for a way to persist Silverlight objects to a user's PC, then re-hydrate them so the user can finish editing them.
Serialising with DataContractSerializer
and persisting to IsolatedStorageFile
works fine. However, deserialising causes a problem. Here's the code that causes the failure:
private string _FirstNames = string.Empty;
public string FirstNames
{
get { return _FirstNames; }
set
{
new PersonNameValidator().Validate(value); //<-- BOOM 8(
Set(ref _FirstNames, value, () => this.FirstNames);
}
}
The deserialiser calls the property setter, which in turn throws an exception and aborts the deserialisation.
I've tried explicitly applying DataContract
/DataMember
/IgnoreDataMember
attributes, but then it doesn't play nicely with private fields:
System.Security.SecurityException
occurred Message="The data contract
type
'Trident.Model.Journey.JourneyApplication'
cannot be serialized because the
member '_TravellerSavingsAmount' is
not public. Making the member public
will fix this error. Alternatively,
you can make it internal, and use the
InternalsVisibleToAttribute attribute
on your assembly in order to enable
serialization of internal members -
see documentation for more details. Be
aware that doing so has certain
security implications."
How can I bypass the property setters during deserialisation?
I'd like to keep my classes focused on the domain, and not too polluted with infrastructure concerns.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一些想法:
A couple of ideas: