BinaryFormatter 类

.NET Framework 类库
BinaryFormatter 类

以二进制格式将对象或整个连接对象图形序列化和反序列化。

命名空间:System.Runtime.Serialization.Formatters.Binary
程序集:mscorlib(在 mscorlib.dll 中)

C# :

[ComVisibleAttribute(true)]
public sealed class BinaryFormatter : IRemotingFormatter, IFormatter

SoapFormatterBinaryFormatter 两个类实现 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;

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);
        }
    }
}

 
通过以上方法可以很容易实现 类型实例的深度复制。也可以使用下面代码:
public   object  Clone(){        //  深度复制
    RefPoint rPoint  =   new  RefPoint();        //  对于引用类型,创建新对象
    rPoint.x  =   this .rPoint.x;            //  复制当前引用类型成员的值 到 新对象
    ValPoint vPoint  =   this .vPoint;           //  值类型,直接赋值
    RefLine newLine  =   new  RefLine(rPoint, vPoint);
    
return  newLine;
}
 
可以看到,如果每个对象都要这样去进行深度复制的话就太麻烦了,所以采用前述 BinaryFormatter类,利用序列化/反序列化来对类型实例(对象)
进行深度复制:先把对象序列化(Serialize)到内存中,然后再进行反序列化化,通过这种方式来进行对象的深度复制。也可以参考一下代码:
 
public   object  Clone() {
    BinaryFormatter bf 
=   new  BinaryFormatter();
    MemoryStream ms 
=   new  MemoryStream();
    bf.Serialize(ms, 
this );
    ms.Position 
=   0 ;

    
return  (bf.Deserialize(ms)); ;
}
 

这里需要注意:如果想将对象进行序列化,那么对象本身,及其所有的自定义成员(类、结构),都必须使用Serializable特性进行标记。所以,如果想让上面的代码运行,我们之前定义的类都需要进行这样的标记:

[Serializable()]
public   class  ClassA{    /* */   }

 

补充:

浅复制(浅克隆):

public   object  Clone() {
    
return  MemberwiseClone();
}

转载于:https://www.cnblogs.com/Dlonghow/archive/2009/01/12/1374021.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值