/*SoapFormatter 和 BinaryFormatter 两个类实现 IRemotingFormatter 接口以支持远程过程调用 (RPC),
* 实现 IFormatter 接口(由 IRemotingFormatter 继承)以支持对象图形的序列化。 该 SoapFormatter 类
* 还支持对 ISoapMessage 对象进行 RPC,而不必使用 IRemotingFormatter 功能。
* 在 RPC 期间, IRemotingFormatter 接口允许指定两个独立的对象图:要序列化的对象图和一个附加图,后者包含
* 一个传送有关远程函数调用信息(例如事务 ID 或方法签名)的标题对象数组。使用 BinaryFormatter 的 RPC 分
* 为两个不同的部分:方法调用,它们将发送到包含被调用方法的远程对象所在的服务器;方法响应,它们包含被调用方法
* 的状态信息和响应信息,从服务器发送到客户端。在方法调用的序列化过程中,对象图形的第一个对象必须支持
* IMethodCallMessage 接口。 若要将方法调用反序列化,请使用带有 HeaderHandler 参数的 Deserialize 方法。
* 远程处理基础结构使用 HeaderHandler 委托来生成支持 ISerializable 接口的对象。 当 BinaryFormatter
* 调用 HeaderHandler 委托时,它将返回包含所调用方法的远程对象的 URI。 所返回图形中的第一个对象支持
* IMethodCallMessage 接口。除对象图形的第一个对象必须支持 IMethodReturnMessage 接口之外,方法响应的
* 序列化过程与方法调用的序列化过程相同。 若要将方法响应反序列化,请使用 DeserializeMethodResponse 方法。
* 为节省时间,在方法调用期间不向远程对象发送有关调用方对象的详细信息。 这些详细信息将从初始方法调用中获取,
* 初始方法调用会传递到 IMethodCallMessage 参数中的 DeserializeMethodResponse 方法。
* DeserializeMethodResponse 方法返回的图形中的第一个对象支持 IMethodReturnMessage 接口。
*/
using System;
using System.IO;
using System.Collections;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization.Formatters.Soap;//引用中引入程序集
using System.Runtime.Serialization;
namespace BinaryFormatAndSoapFormat_
{
public class App
{
[STAThread]//指示应用程序的COM线程模型为单线程单元SAT
static void Main()
{
Console.WriteLine("B->二进制格式 or S-> SOAP格式 将对象或整个连接对象图形序列化和反序列化");
string input=Console.ReadLine();
if (input == "Q")
{
Console.WriteLine("you have quit");
return;
}
else if (input =="B" ||input=="S")
{
Serialize(input);
Deserialize(input);
}
Console.ReadLine();
}
static void Serialize(string input)
{
// 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
try
{
if (input=="B")
{
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(fs, addresses);
}
else if (input=="S")
{
SoapFormatter formatter = new SoapFormatter();
formatter.Serialize(fs, addresses);
}
}
catch (SerializationException e)
{
Console.WriteLine("Failed to serialize. Reason: " + e.Message);
throw;
}
finally
{
fs.Close();
}
}
static void Deserialize(string input)
{
// 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
{
if (input=="B")
{
BinaryFormatter formatter = new BinaryFormatter();
// Deserialize the hashtable from the file and
// assign the reference to the local variable.
addresses = (Hashtable)formatter.Deserialize(fs);
}
else if (input=="S")
{
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.
foreach (DictionaryEntry de in addresses)
{
Console.WriteLine("{0} lives at {1}.", de.Key, de.Value);
}
}
}
}
SoapFormatter 和 BinaryFormatter 序列化对象
最新推荐文章于 2021-04-11 16:29:12 发布