Remoting学习之一:对象的序列化

     Remoting的实质就是远程对象的调用,就是调用另一台机器的某个对象,那么第一步当然就是将对象序列化,这样才能在网络中传输。那么肯定就有2种方式来传递,一是将对象序列化为二进制形式,通过TCP协议来传输;二是序列化为XML格式,通过http/soap来传输。这是学习Remoting的必备条件。 

    序列化为二进制:

            // create a file stream to write the file
            FileStream fileStream = new FileStream("DoSum.bin", FileMode.Create);
            // use the CLR binary formatter
            BinaryFormatter binaryFormatter = new BinaryFormatter();
            // serialize to disk
            binaryFormatter.Serialize(fileStream, sObj);
            fileStream.Close();

    序列化为XML:

            // create a file stream to write the file
            FileStream fileStream = new FileStream("DoSum.xml", FileMode.Create);
            // use the CLR binary formatter
            System.Xml.Serialization.XmlSerializer
            formatter = new XmlSerializer(typeof(SumOf));
            // serialize to disk
            formatter.Serialize(fileStream, sObj);
            fileStream.Close();

    序列化为SOAP:

            // create a file stream to write the file
            FileStream fileStream = new FileStream("DoSum_Soap.xml", FileMode.Create);
            // use the CLR binary formatter
            SoapFormatter formatter = new SoapFormatter();
            // serialize to disk
            formatter.Serialize(fileStream, sObj);
            fileStream.Close();

    另外:Sumof类要标注为可序列化的,使用[Serializable]

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值