c# - C#反序列化分页
问题描述
我有大量需要在 HTML 表格中显示的对象。我使用 BinaryFormatter 检索列表,如下所示:
IFormatter formatter = new BinaryFormatter();
Stream stream = new FileStream("MyFile.bin", FileMode.Open, FileAccess.Read, FileShare.Read);
MyObject obj = (MyObject) formatter.Deserialize(fromStream);
stream.Close();
Deserialize() 方法可以返回前 100 条记录,然后返回下 100 条,依此类推吗?
解决方案
Deserialize() 方法可以返回前 100 条记录,然后返回下 100 条,依此类推吗?
最简洁的答案是不。
1) 但是,您可以单步执行反序列化对象,例如以下地址:
using System;
using System.IO;
using System.Collections;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;
public class App
{
[STAThread]
static void Main()
{
Serialize();
Deserialize();
}
static void Serialize()
{
// Create a hashtable of values that will eventually be serialized.
Hashtable addresses = new Hashtable();
addresses.Add("Jeff", "123 Main Street, Redmond, WA 98052");
addresses.Add("Fred", "987 Pine Road, Phila., PA 19116");
addresses.Add("Mary", "PO Box 112233, Palo Alto, CA 94301");
// To serialize the hashtable and its key/value pairs,
// you must first open a stream for writing.
// In this case, use a file stream.
FileStream fs = new FileStream("DataFile.dat", FileMode.Create);
// Construct a BinaryFormatter and use it to serialize the data to the stream.
BinaryFormatter formatter = new BinaryFormatter();
try
{
formatter.Serialize(fs, addresses);
}
catch (SerializationException e)
{
Console.WriteLine("Failed to serialize. Reason: " + e.Message);
throw;
}
finally
{
fs.Close();
}
}
static void Deserialize()
{
// Declare the hashtable reference.
Hashtable addresses = null;
// Open the file containing the data that you want to deserialize.
FileStream fs = new FileStream("DataFile.dat", FileMode.Open);
try
{
BinaryFormatter formatter = new BinaryFormatter();
// Deserialize the hashtable from the file and
// assign the reference to the local variable.
addresses = (Hashtable) formatter.Deserialize(fs);
}
catch (SerializationException e)
{
Console.WriteLine("Failed to deserialize. Reason: " + e.Message);
throw;
}
finally
{
fs.Close();
}
// To prove that the table deserialized correctly,
// display the key/value pairs.
foreach (DictionaryEntry de in addresses)
{
Console.WriteLine("{0} lives at {1}.", de.Key, de.Value);
}
}
}
2)首先选择一种不同的方法来编码你的二进制数据,这样你就可以根据需要顺序读取它。因此,例如,您可以编写一个自定义序列化程序,该序列化程序首先根据您正在流式传输的对象的大小将要读取的字节数作为字节序列输出,一些基本代码将是:
int read = stream.Read(data, offset, remaining);
事实上,编写自己的序列化程序以便了解数据是如何序列化的,并且可以根据需要逐步反序列化它,如果这是您的要求,那么这可能是值得的。
3) 依靠 3rd 方包,它可以有效地序列化您的数据并允许您按顺序读取内容。那里有堆只是做一些谷歌搜索会找到堆,但你可以看看谷歌协议缓冲区,它是大小有效且快速的。请参阅https://developers.google.com/protocol-buffers/docs/csharptutorial,其中包含一些我讨论过的内容的示例。
推荐阅读
- curl - 使用 curl 从 sharepoint 下载文件
- c - 在 c 中打印语句
- ios - GetStreamActivityFeed 的 pod 中的 JSON 解码错误
- parsing - Haskell 解析查询 (Parsec)
- umbraco - Umbraco 7.6.1 不会升级
- laravel - 带有单个参数的 laravel 策略方法如何工作?
- javascript - 如何简化以下脚本以更轻松地编写 HTML?
- javascript - 这种命名变量函数的方式是否可以接受?
- r - 是否有一种计算速度更快的方法来进行这种数据转换?
- angular - 在 ANGULAR 订阅中的数据数组更改后,组件(AngularMatDataTable)不会重新渲染?