.NET 可序列化实体
我需要使所有实体可序列化。所以我在考虑使用具有备份和恢复方法的 BaseEntity。但在恢复中,我无法用保存的对象覆盖该对象,因为 this
是只读的。
有什么解决方案或其他方法来获取可序列化的实体吗?
我的代码:
internal class BaseEntity
{
private MemoryStream ms = new MemoryStream();
private BinaryFormatter bf = new BinaryFormatter();
public void Backup()
{
bf.Serialize(ms, this);
}
public void Restore()
{
this = (BaseEntity)bf.Deserialize(ms);
}
}
I need to make all my entities serializable. So I was thinking in a BaseEntity with a Backup and a Restore method. But in the restore I can't override the object with the saved one because this
is read-only.
Any solution or some other way to get the serializable entities?
My code:
internal class BaseEntity
{
private MemoryStream ms = new MemoryStream();
private BinaryFormatter bf = new BinaryFormatter();
public void Backup()
{
bf.Serialize(ms, this);
}
public void Restore()
{
this = (BaseEntity)bf.Deserialize(ms);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
更常见的模式是不让对象负责序列化/反序列化自身;相反,使用外部序列化器:
The more common pattern is to not make it the responsibility of your objects to serialize/deserialize themselves; rather, use an external serializer:
编辑:序列化的一种工作方式是使用 System.Runtime.Serialization.Formatters.Binary.BinaryFormatter (或 IFormatter 的其他实现)。要序列化对象,您需要传递对象和流。要反序列化对象,您传递一个流(位于序列化数据的开头),它返回序列化对象及其所有依赖项。
格式化程序不做的一件事(尽管 FormatterServices 类使之成为可能)是修改现有对象。因此,您可能不希望有一个名为 Deserialize 的实例方法。您实际上不能这样做:
new LionEntity().Deserialize ()
,它会替换现有实例的字段。注意:您需要将Serialized< /a> 覆盖所有类型。任何无法序列化的字段(因为它不是结构体,或者未标记为 [Serializable])都需要用 非序列化。
编辑:
我提到的方式是一种相当标准且被接受的方式。如果您想冒险进入 hackdom,您可以按照我提到的方式反序列化对象,然后使用反射将现有对象上的每个字段设置为反序列化对象的值。
Edit: One way serialization can work is to use the System.Runtime.Serialization.Formatters.Binary.BinaryFormatter (or other implementation of IFormatter). To serialize an object you pass the object and a stream. To Deserialize the object, you pass a stream (positioned at the begining of your serialized data), and it returns the serialized object and all its depenedencies.
One thing a formatter don't do (though the FormatterServices class makes it possible) is modify existing objects. So you probably don't want to have an instance method called Deserialize. You can't really do this:
new LionEntity().Deserialize ()
where it replaces the fields of an existing instance.Note: You'll need to put Serializable over all your types. Any fields that can't be serialized (because it's either not a struct, or it's not marked as [Serializable] will need to be marked with NonSerialized.
Edit:
The way I've mentioned is a rather standard and accepted way. If you want to venture into hackdom, you can deserialize the object the way I've mentioned, then use reflection to set each field on your existing object to the value of the deserialized object.
@jacklondon 你会如何做 EntitySerializer 方法?
您可以使用 http://www.servicestack.net/ StackService.Text 模块对干净实体进行序列化过程。您不需要以 ms 方式使用任何属性(可序列化/数据契约)。
@jacklondon how would you do EntitySerializer methods?
You can do serialization process with http://www.servicestack.net/ StackService.Text module for clean entities. You don't need any attribute (serializable/datacontract) in ms way.
我不一定推荐这样做,但这是一种对象模式,可以使用创建新实例的序列化来持久保存和恢复其自身状态:
请注意,只有当您的序列化程序支持非公共类时,这才有效。最坏的情况是,您必须将嵌套类设为公共,这很丑陋,但不会损害封装(因为实例是私有的)。
I don't necessarily recommend this, but here is one pattern for an object that can persist and restore its own state using serialization that creates new instances:
Note that this only works if your serializer supports non-public classes. Worst-case, you have to make the nested class public, which is ugly, but doesn't hurt encapsulation (since the instance is private).