以二进制格式序列化和反序列化对象或连接对象的整个图形。
命名空间: System.Runtime.Serialization.Formatters.Binary
程序集: mscorlib(位于 mscorlib.dll)
继承层次结构
System.Object
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
语法:
[ComVisibleAttribute(true)] public sealed class BinaryFormatter : IRemotingFormatter, IFormatter
构造函数
名称 | 说明 | |
---|---|---|
![]() |
BinaryFormatter() | 使用默认值初始化 BinaryFormatter 类的新实例。 |
![]() |
BinaryFormatter(ISurrogateSelector,StreamingContext) | 新实例初始化 BinaryFormatter 使用给定的代理项选择器和流上下文的类。 |
属性
名称 | 说明 | |
---|---|---|
![]() |
AssemblyFormat | 获取或设置关于查找和加载程序集反序列化程序的行为。 |
![]() |
Binder | 获取或设置类型的对象 SerializationBinder 控制将序列化对象传递给一种类型的绑定。 |
![]() |
Context | 获取或设置 StreamingContext 此格式化程序。 |
![]() |
FilterLevel | 获取或设置 TypeFilterLevel 的自动反序列化 BinaryFormatter 执行。 |
![]() |
SurrogateSelector | 获取或设置 ISurrogateSelector 控制类型序列化和反序列化过程的替换。 |
![]() |
TypeFormat | 获取或设置类型说明中的序列化流中的布局的格式。 |
方法
名称 | 说明 | |
---|---|---|
![]() |
Deserialize(Stream) | 指定的流反序列化对象图。 |
![]() |
Deserialize(Stream,HeaderHandler) | 指定的流反序列化对象图。 提供 HeaderHandler 处理该流中的任何标头。 |
![]() |
DeserializeMethodResponse(Stream,HeaderHandler,IMethodCallMessage) | 反序列化对实现远程方法调用从所提供的响应 Stream。 |
![]() |
Equals(Object) | 确定指定的对象是否等于当前对象。(继承自 Object。) |
![]() |
GetHashCode() | 作为默认哈希函数。(继承自 Object。) |
![]() |
GetType() | 获取当前实例的 Type。(继承自 Object。) |
![]() |
Serialize(Stream,Object) | 将对象或具有指定顶级 (根)、 对象图序列化到给定的流。 |
![]() |
Serialize(Stream,Object,Header[]) | 给定的流将提供的标头附加到序列化对象或具有指定顶级 (根)、 对象图。 |
![]() |
ToString() | 返回表示当前对象的字符串。(继承自 Object。) |
![]() |
UnsafeDeserialize(Stream,HeaderHandler) | 指定的流反序列化对象图。 提供 HeaderHandler 处理该流中的任何标头。 |
![]() |
UnsafeDeserializeMethodResponse(Stream,HeaderHandler,IMethodCallMessage) | 反序列化对实现远程方法调用从所提供的响应 Stream。 |
备注
The T:System.Runtime.Serialization.Formatters.Soap.SoapFormatter and T:System.Runtime.Serialization.Formatters.Binary.BinaryFormatter classes implement the T:System.Runtime.Remoting.Messaging.IRemotingFormatter interface to support remote procedure calls (RPCs), and the T:System.Runtime.Serialization.IFormatter interface (inherited by the T:System.Runtime.Remoting.Messaging.IRemotingFormatter) to support serialization of a graph of objects. The T:System.Runtime.Serialization.Formatters.Soap.SoapFormatter class also supports RPCs with T:System.Runtime.Serialization.Formatters.ISoapMessage objects, without using the T:System.Runtime.Remoting.Messaging.IRemotingFormatter functionality.
During RPCs, the T:System.Runtime.Remoting.Messaging.IRemotingFormatter interface allows the specification of two separate object graphs: the graph of objects to serialize, and an additional graph that contains an array of header objects that convey information about the remote function call (for example, transaction ID or a method signature).
RPCs that use the T:System.Runtime.Serialization.Formatters.Binary.BinaryFormatter separate into two distinct parts: method calls, which are sent to the server with the remote object that contains the method called, and method responses, which are sent from the server to the client with the status and response information from the called method.
During serialization of a method call the first object of the object graph must support the T:System.Runtime.Remoting.Messaging.IMethodCallMessage interface. To deserialize a method call, use the M:System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(System.IO.Stream) method with the T:System.Runtime.Remoting.Messaging.HeaderHandler parameter. The remoting infrastructure uses the T:System.Runtime.Remoting.Messaging.HeaderHandler delegate to produce an object that supports the T:System.Runtime.Serialization.ISerializable interface. When the T:System.Runtime.Serialization.Formatters.Binary.BinaryFormatter invokes the T:System.Runtime.Remoting.Messaging.HeaderHandler delegate, it returns the URI of the remote object with the method that is being called. The first object in the graph returned supports the T:System.Runtime.Remoting.Messaging.IMethodCallMessage interface.
The serialization procedure for a method response is identical to that of a method call, except the first object of the object graph must support the T:System.Runtime.Remoting.Messaging.IMethodReturnMessage interface. To deserialize a method response, use the M:System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.DeserializeMethodResponse(System.IO.Stream,System.Runtime.Remoting.Messaging.HeaderHandler,System.Runtime.Remoting.Messaging.IMethodCallMessage) method. To save time, details about the caller object are not sent to the remote object during the method call. These details are instead obtained from the original method call, which is passed to the M:System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.DeserializeMethodResponse(System.IO.Stream,System.Runtime.Remoting.Messaging.HeaderHandler,System.Runtime.Remoting.Messaging.IMethodCallMessage) method in the T:System.Runtime.Remoting.Messaging.IMethodCallMessage parameter. The first object in the graph returned by the M:System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.DeserializeMethodResponse(System.IO.Stream,System.Runtime.Remoting.Messaging.HeaderHandler,System.Runtime.Remoting.Messaging.IMethodCallMessage) method supports the T:System.Runtime.Remoting.Messaging.IMethodReturnMessage interface.
安全性 说明 |
---|
Using binary serialization to deserialize untrusted data can lead to security risks. For more information, see Untrusted Data Security Riskshttp://go.microsoft.com/fwlink/?LinkId=330378. |
Unpaired Surrogates
Any unpaired surrogate characters are lost in binary serialization. For example, the following string contains a high surrogate Unicode character (\ud800) in between the two Test words:
Test\ud800Test
Before serialization, the byte array of the string is as follows:
Byte Array Value |
Character |
---|---|
84 |
T |
101 |
e |
115 |
s |
116 |
t |
55296 |
\ud800 |
84 |
T |
101 |
e |
115 |
s |
116 |
t |
After deserialization, the high surrogate Unicode character is lost:
Byte Array Value |
Character |
---|---|
84 |
T |
101 |
e |
115 |
s |
116 |
t |
84 |
T |
101 |
e |
115 |
s |
116 |
t |
示例:
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); } } }
线程安全
此类型的所有公共静态(Visual Basic 中的 已共享 在 Visual Basic 中)成员都是线程安全的。不保证所有实例成员都是线程安全的。
备注:转自https://msdn.microsoft.com/zh-cn/library/system.runtime.serialization.formatters.binary.binaryformatter(v=vs.110).aspx