public object Deserialize(Stream serializationStream, HeaderHandler handler);
时间: 2024-03-06 18:50:22 浏览: 21
`Deserialize`方法是`System.Runtime.Serialization.Formatters.Binary.BinaryFormatter`类中的一个方法,用于从指定的序列化流(`serializationStream`)中反序列化对象图,并可选地指定一个头部处理程序(`handler`)来处理头部信息。
具体参数说明如下:
- `serializationStream`:序列化流,即从中读取对象图进行反序列化的源流。
- `handler`:可选的头部处理程序,用于处理对象图中包含的头部信息。
方法返回一个反序列化的对象图。
下面是一个示例代码:
```csharp
using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
[Serializable]
class Person
{
public string Name { get; set; }
public int Age { get; set; }
}
class Program
{
static void Main(string[] args)
{
// 创建一个包含头部信息的序列化流
BinaryFormatter formatter = new BinaryFormatter();
using (MemoryStream stream = new MemoryStream())
{
formatter.Serialize(stream, new Person() { Name = "Tom", Age = 20 }, new[] { new Header("CustomHeader", "CustomHeaderValue") });
// 从序列化流中反序列化对象图,并处理头部信息
stream.Seek(0, SeekOrigin.Begin);
object obj = formatter.Deserialize(stream, (header, stream1) =>
{
Console.WriteLine($"Header name: {header.Name}, Value: {header.Value}");
return null;
});
// 输出反序列化的对象
Console.WriteLine(obj);
}
}
}
```
上面的示例创建了一个包含头部信息的序列化流,并通过`Deserialize`方法从流中反序列化对象图。在反序列化过程中,使用了一个头部处理程序,该程序会在处理每个头部信息时输出头部的名称和值。最后,示例输出了反序列化的对象。注意,在使用头部处理程序时,需要将流的位置设置为0,即`stream.Seek(0, SeekOrigin.Begin)`,以便从流的开头开始读取数据。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)