为什么我们不能序列化这些对象?
为什么我们不能将对象序列化为随机访问文件?另一方面,我们可以将对象序列化为顺序访问文件?
""C# 不提供在运行时获取对象大小的方法。这意味着, 如果我们序列化该类,我们无法保证固定长度的记录大小“”(来自我读到的书)。
所以我们无法读取随机访问文件,因为我们不知道文件中的每个对象大小,所以我们如何才能进行查找??????
why we can't Serialize objects into Random Access file ? and on the other hand we can serialize objects into sequential access file ?
""C# does not provide a means to obtain an object’s size at runtime. This means that,
if we serialize the class, we cannot guarantee a fixed-length record size "" (from the book that i read in).
so we cannot read the the random access file because we don't know every object size in the file so how we could do seeking ??????
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
任何标有 SerializedAttribute 属性的对象都可以序列化(在大多数情况下)。序列化的结果始终定向到流,该流很可能是文件输出流。
您是否在问为什么对象图不能部分反序列化? .NET 序列化仅[反]序列化完整的对象图。否则,您将不得不转向其他序列化格式化程序,或编写自己的序列化格式化程序。
要直接随机访问文件,必须使用支持查找的流打开文件。
编辑:
从序列化中寻找结果流没有实际目的 - 只有序列化格式化程序知道其中有什么,并且应该始终在流的开头提供。
用于将数据持久保存到其他结构中;分两个阶段进行:首先,将序列化字节定位到一个[即内存支持]流,您可以随后从中读取大小,然后使用所述大小知识将数据写入实际的后备存储。
您无法预测序列化对象的大小,因为序列化表示可能与运行时表示有很大不同。
如果您仅使用原始类型,并且使用 BinaryWriter 进行编写,那么仍然可以实现对输出大小的精确控制 - 但这本身并不是序列化。
Any object marked with the
SerializableAttribute
attribute can be serialized (in most scenarios). The result from serialization is always directed to a stream, which may very well be a file output stream.Are you asking why an object graph cannot be deserialized partially? .NET serialization only [de]serializes complete object graphs. Otherwise you'll have to turn to other serialization formatters, or write your own.
For direct random access to a file, you must open the file with a stream that supports seeking.
EDIT:
Seeking in the resulting stream from a serialization has no practical purpose - only the serialiation formatter knows what's in there anyway and should always be fed the very start of the stream.
For persisting the data into other structures; do it in a two-stage process: First, target the serialization bytes to a [i.e. memory-backed] stream that you can read the size from afterwards, then write the data to the actual backing store, using said knowledge of size.
You can't predict the size of a serialized object, because the serialized representation might differ a lot from the runtime representation.
It it still possible to achieve exact control over output size, if you use only primitive types, and you write using a BinaryWriter - but that is not serialization per-se.
.NET 中默认的二进制序列化会序列化整个对象图,由于其图的本质,它没有恒定的大小,这意味着每个序列化对象(记录)不会有恒定的大小,从而防止随机访问。
为了能够随机访问文件中的任何记录,请编写您自己的类的二进制序列化实现,或使用数据库。如果您需要一个简单的、免安装的单线程数据库引擎,请查看 SQL Server Compact。
The default binary serialization in .NET serializes a whole object graph, which, by its nature of being a graph, doesn't have a constant size, which means each serialization object (record) won't have a constant size, preventing random access.
To be able to randomly access any record in a file, write your own implementation of the binary serialization of your class, or use a database. If you need a simple, no-install single-threaded database engine, have a look at SQL Server Compact.