ITPub博客

首页 > Linux操作系统 > Linux操作系统 > C# 串行化和并行化

C# 串行化和并行化

原创 Linux操作系统 作者:kirinri 时间:2009-04-07 09:53:26 0 删除 编辑

using System;

using System.Collections.Generic;

using System.Text;

using System.Runtime.Serialization.Formatters.Binary;

using System.Runtime.Serialization.Formatters.Soap;

using System.IO;

 

namespace SerializableTest

{

    class Program

    {

        static void Main(string[] args)

        {

 

            SeriMethod();

            DeseriMethod();

        }

 

        static void SeriMethod()

        {

            Person per = new Person(27, "David");

            Stream str = File.Open(@"C:\SerializableTest.dat", FileMode.Create);

 

            BinaryFormatter bf = new BinaryFormatter();

            bf.Serialize(str, per);

            

            Person perSoap = new Person(37, "Old David");

            Stream strSoap = File.Open(@"C:\SerializableTest_Soap.dat", FileMode.Create);

 

            SoapFormatter soapFor = new SoapFormatter();

            soapFor.Serialize(strSoap, perSoap);

           

            str.Close();

            strSoap.Close();

 

        }

 

        static void DeseriMethod()

        {

            Stream str = File.Open(@"C:\SerializableTest.dat", FileMode.Open);

            Stream strSoap = File.Open(@"C:\SerializableTest_Soap.dat", FileMode.Open);

 

            BinaryFormatter bf = new BinaryFormatter();

            SoapFormatter soapFor = new SoapFormatter();

           

 

            object o = bf.Deserialize(str);

            object Soap = soapFor.Deserialize(strSoap);

 

            Person per = (Person)o;

            Person perSoap = (Person)oSoap;

 

            if (per != null)

                Console.WriteLine("Binary DeSerialized Person aged:{0} name:{1}", per.age, per.name);

 

            if (perSoap != null)

                Console.WriteLine("Soap DeSerialized Person aged:{0} name:{1}", perSoap.age, perSoap.name);

 

            str.Close();

            strSoap.Close();

        }

    }

 

    [Serializable]

    class Person

    {

        public int age;

        //[NonSerialized]

        public string name = string.Empty;

        public Person(int argAge, string argName)

        {

            age = argAge;

            name = argName;

        }

    }

}

 

Result:

 ÿ?!   Ȁ 䝓敲楡汩穡扬敔敳琬⁖敲獩潮㴱⸰⸰⸰Ⱐ䍵汴畲攽湥畴牡氬⁐畢汩捋敹呯步渽湵汬ԁ 卥物慬楺慢汥呥獴⹐敲獯渂 慧攄湡浥ࠂ  ̀ Մ慶楤

http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:clr="http://schemas.microsoft.com/soap/encoding/clr/1.0" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">

http://schemas.microsoft.com/clr/nsassem/SerializableTest/SerializableTest%2C%20Version%3D1.0.0.0%2C%20Culture%3Dneutral%2C%20PublicKeyToken%3Dnull">
37
Old David



--------------------------------------------------------------------------------------------
Binary DeSerialized Person aged:27 name:David
Soap DeSerialized Person aged:37 name:Old David
Press any key to continue . . .

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/12476590/viewspace-586435/,如需转载,请注明出处,否则将追究法律责任。

上一篇: 速记 线程池
请登录后发表评论 登录
全部评论

注册时间:2007-12-21

  • 博文量
    257
  • 访问量
    497781