SoapFormatter 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
以 SOAP 格式将对象或整个连接对象的图形序列化和反序列化。
public ref class SoapFormatter sealed : System::Runtime::Remoting::Messaging::IRemotingFormatter
public sealed class SoapFormatter : System.Runtime.Remoting.Messaging.IRemotingFormatter
type SoapFormatter = class
interface IRemotingFormatter
interface IFormatter
Public NotInheritable Class SoapFormatter
Implements IRemotingFormatter
- 继承
-
SoapFormatter
- 实现
示例
#using <system.dll>
#using <system.runtime.serialization.formatters.soap.dll>
using namespace System;
using namespace System::IO;
using namespace System::Collections;
using namespace System::Runtime::Serialization;
using namespace System::Runtime::Serialization::Formatters::Soap;
void Serialize()
{
// Create a hashtable of values that will eventually be serialized.
Hashtable^ addresses = gcnew 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 keys/values),
// you must first open a stream for writing.
// We will use a file stream here.
FileStream^ fs = gcnew FileStream( "DataFile.soap",FileMode::Create );
// Construct a SoapFormatter and use it
// to serialize the data to the stream.
SoapFormatter^ formatter = gcnew SoapFormatter;
try
{
formatter->Serialize( fs, addresses );
}
catch ( SerializationException^ e )
{
Console::WriteLine( "Failed to serialize. Reason: {0}", e->Message );
throw;
}
finally
{
fs->Close();
}
}
void Deserialize()
{
// Declare the hashtable reference.
Hashtable^ addresses = nullptr;
// Open the file containing the data that we want to deserialize.
FileStream^ fs = gcnew FileStream( "DataFile.soap",FileMode::Open );
try
{
SoapFormatter^ formatter = gcnew SoapFormatter;
// Deserialize the hashtable from the file and
// assign the reference to our local variable.
addresses = dynamic_cast<Hashtable^>(formatter->Deserialize( fs ));
}
catch ( SerializationException^ e )
{
Console::WriteLine( "Failed to deserialize. Reason: {0}", e->Message );
throw;
}
finally
{
fs->Close();
}
// To prove that the table deserialized correctly,
// display the keys/values to the console.
IEnumerator^ myEnum = addresses->GetEnumerator();
while ( myEnum->MoveNext() )
{
DictionaryEntry^ de = safe_cast<DictionaryEntry^>(myEnum->Current);
Console::WriteLine( " {0} lives at {1}.", de->Key, de->Value );
}
}
[STAThread]
int main()
{
Serialize();
Deserialize();
}
using System;
using System.IO;
using System.Collections;
using System.Runtime.Serialization;
// Note: When building this code, you must reference the
// System.Runtime.Serialization.Formatters.Soap.dll assembly.
using System.Runtime.Serialization.Formatters.Soap;
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.
// Use a file stream here.
FileStream fs = new FileStream("DataFile.soap", FileMode.Create);
// Construct a SoapFormatter and use it
// to serialize the data to the stream.
SoapFormatter formatter = new SoapFormatter();
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.soap", FileMode.Open);
try
{
SoapFormatter formatter = new SoapFormatter();
// 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 to the console.
foreach (DictionaryEntry de in addresses)
{
Console.WriteLine("{0} lives at {1}.", de.Key, de.Value);
}
}
}
Imports System.IO
Imports System.Collections
Imports System.Runtime.Serialization
' Note: When building this code, you must reference the
' System.Runtime.Serialization.Formatters.Soap.dll assembly.
Imports System.Runtime.Serialization.Formatters.Soap
Module App
Sub Main()
Serialize()
Deserialize()
End Sub
Sub Serialize()
' Create a hashtable of values that will eventually be serialized.
Dim addresses As 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.
' Use a file stream here.
Dim fs As New FileStream("DataFile.soap", FileMode.Create)
' Construct a SoapFormatter and use it
' to serialize the data to the stream.
Dim formatter As New SoapFormatter
Try
formatter.Serialize(fs, addresses)
Catch e As SerializationException
Console.WriteLine("Failed to serialize. Reason: " & e.Message)
Throw
Finally
fs.Close()
End Try
End Sub
Sub Deserialize()
' Declare the hashtable reference.
Dim addresses As Hashtable = Nothing
' Open the file containing the data that you want to deserialize.
Dim fs As New FileStream("DataFile.soap", FileMode.Open)
Try
Dim formatter As New SoapFormatter
' Deserialize the hashtable from the file and
' assign the reference to the local variable.
addresses = DirectCast(formatter.Deserialize(fs), Hashtable)
Catch e As SerializationException
Console.WriteLine("Failed to deserialize. Reason: " & e.Message)
Throw
Finally
fs.Close()
End Try
' To prove that the table deserialized correctly,
' display the key/value pairs to the console.
Dim de As DictionaryEntry
For Each de In addresses
Console.WriteLine("{0} lives at {1}.", de.Key, de.Value)
Next
End Sub
End Module
注解
注意
从 .NET Framework 2.0 开始,此类已过时。
警告
BinaryFormatter
不安全,无法确保安全。 有关详细信息,请参阅 BinaryFormatter 安全指南。
SoapFormatter和 BinaryFormatter 类实现 IRemotingFormatter 接口以支持远程过程调用 (RPC) ,) IFormatter 继承IRemotingFormatter的接口 (支持对象图的序列化。 类 SoapFormatter 还支持包含 ISoapMessage 对象的 RPC,而不使用 IRemotingFormatter 功能。
在 RPC 期间, IRemotingFormatter 接口允许指定两个单独的对象图:要序列化的对象图,以及包含标头对象数组的附加图形,这些对象 (例如事务 ID 或方法签名) 。 为了进行适当的序列化,第一个图形的根对象必须是实现 IMethodCallMessage 接口或 接口的对象 IMethodReturnMessage 。
在反序列化 RPC 期间, HeaderHandler 将委托指定给 Deserialize 格式化程序的方法。 远程处理基础结构使用 HeaderHandler 委托生成支持 接口的对象 ISerializable 。 此对象包含标头中存储的信息,并成为反序列化程序返回的图形的根。
SoapFormatter还可以处理由实现 ISoapMessage 接口的 对象生成的 RPC。 若要在 IRemotingFormatter 不使用该功能的情况下创建 RPC,请将支持 ISoapMessage 接口的对象放置在要序列化的图形的根目录中。 若要反序列化以这种方式创建的 RPC, TopObject 必须将 属性设置为支持 接口 ISoapMessage 的另一个对象,并包含相关的远程调用信息。
TimeSpan 对象根据 ISO 8601:1998 第 5.5.3.2.1 节“替代”标准进行序列化。
构造函数
SoapFormatter() |
使用默认属性值初始化 SoapFormatter 类的新实例。 |
SoapFormatter(ISurrogateSelector, StreamingContext) |
使用指定的 SoapFormatter 和 ISurrogateSelector 初始化 StreamingContext 类的新实例。 |
属性
AssemblyFormat |
获取或设置与查找加集有关的反序列化器行为。 |
Binder |
获取或设置 SerializationBinder,它控制将序列化对象绑定到类型的过程。 |
Context |
获取或设置与 StreamingContext 一起使用的 SoapFormatter。 |
FilterLevel |
获取或设置用于 .NET Framework 远程处理的自动反序列化的 TypeFilterLevel。 |
SurrogateSelector |
获取或设置 SurrogateSelector,它控制序列化和反序列化过程中的类型替换。 |
TopObject |
获取或设置将 SOAP 顶级对象反序列化为的 ISoapMessage。 |
TypeFormat |
获取或设置类型说明在序列化流中的布局格式。 |
方法
Deserialize(Stream) |
反序列化所提供流中的数据并重新组成对象图形。 |
Deserialize(Stream, HeaderHandler) |
将流反序列化为对象图形,该流中的任何标题都由给定的 HeaderHandler 进行处理。 |
Equals(Object) |
确定指定对象是否等于当前对象。 (继承自 Object) |
GetHashCode() |
作为默认哈希函数。 (继承自 Object) |
GetType() |
获取当前实例的 Type。 (继承自 Object) |
MemberwiseClone() |
创建当前 Object 的浅表副本。 (继承自 Object) |
Serialize(Stream, Object) |
将对象或具有指定根的对象图形序列化为给定的 Stream。 |
Serialize(Stream, Object, Header[]) |
以 SOAP 远程过程调用 (RPC) 格式,将对象或具有指定根的对象图形序列化为给定的 Stream。 |
ToString() |
返回表示当前对象的字符串。 (继承自 Object) |