关于序列化--完整for beta1 [转]

本文详细介绍了如何使用C#进行对象的序列化和反序列化操作,包括基本类、包含属性和静态成员的类以及对象数组的序列化过程。通过实例演示了BinaryFormatter的使用方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在要序列化的类定义前加上标记,在beta2中运行正常
Introduction
In simple words serialization is a process of storing the object instance to a disk file. Serialization stores state of the object i.e. member variable values to disk. Deserialization is reverse of serialization i.e. it's a process of reading objects from a file where they have been stored. In this code sample we will see how to serialize and deserialize objects using C#.

Namespaces involved
Following namespaces are involved in serialization process :

System.Runtime.Serialization
System.Runtime.Serialization.Formatters.Binary
Example 1
using System;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
public class SerialTest
{

    public void SerializeNow()
    {
        ClassToSerialize c=new ClassToSerialize();
        File f=new File("temp.dat");
        Stream s=f.Open(FileMode.Create);
        BinaryFormatter b=new BinaryFormatter();
        b.Serialize(s,c);
        s.Close();
    }

    public void DeSerializeNow()
    {
        ClassToSerialize c=new ClassToSerialize();
        File f=new File("temp.dat");
        Stream s=f.Open(FileMode.Open);
        BinaryFormatter b=new BinaryFormatter();
        c=(ClassToSerialize)b.Deserialize(s);
        Console.WriteLine(c.name);
        s.Close();
    }

    public static void Main(string[] s)
    {

        SerialTest st=new SerialTest();
        st.SerializeNow();
        st.DeSerializeNow();

    }

}
public class ClassToSerialize
{
    public int age=100;
    public string name="bipin";

}


Explanation
Here we have our own class named ClassToSerialize. This class has two public valiables name and age with some default values. We will write this class to a disk file (temp.dat) using SerializeTest class.

SerializeTest class has two methods SerializeNow() and DeSerializeNow() which perform the task of serialization and deserialization respectively.

The general steps for serializing are :

Create an instance of File that will store serialized object
Create a stream from the file object
Create an instance of BinaryFormatter
Call serialize method of the instance passing it stream and object to serialize
The steps for de-serializing the object are similar. The only change is that you need to call deserialize method of BinaryFormatter object.

Now, let us see an example where we have used 'real' class with public and shared members and properties to encapsulate them. The class also uses another supporting class. This is just to make clear that if your class contains further classes, all the classes in the chain will be serialized.

Example 2
using System;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

public class SerialTest
{

    public void SerializeNow()
    {
        ClassToSerialize c=new ClassToSerialize();
        c.Name="bipin";
        c.Age=26;
        ClassToSerialize.CompanyName="xyz";
        File f=new File("temp.dat");
        Stream s=f.Open(FileMode.Create);
        BinaryFormatter b=new BinaryFormatter();
        b.Serialize(s,c);
        s.Close();
    }

    public void DeSerializeNow()
    {
        ClassToSerialize c=new ClassToSerialize();
        File f=new File("temp.dat");
        Stream s=f.Open(FileMode.Open);
        BinaryFormatter b=new BinaryFormatter();
        c=(ClassToSerialize)b.Deserialize(s);
        Console.WriteLine("Name :" + c.Name);
        Console.WriteLine("Age :" + c.Age);
        Console.WriteLine("Company Name :" + ClassToSerialize.CompanyName);
        Console.WriteLine("Company Name :" + c.GetSupportClassString());
        s.Close();
    }

    public static void Main(string[] s)
    {

        SerialTest st=new SerialTest();
        st.SerializeNow();
        st.DeSerializeNow();

    }

}

public class ClassToSerialize
{
    private int age;
    private string name;
    static string companyname;
    SupportClass supp=new SupportClass();

    public ClassToSerialize()
    {
        supp.SupportClassString="In support class";

    }

    public int Age
    {
        get
        {
            return age;
        }
        set
        {
            age=value;
        }

    }

    public string Name
    {
        get
        {
            return name;
        }
        set
        {
            name=value;
        }

    }

    public static string CompanyName
    {
        get
        {
            return companyname;
        }
        set
        {
            companyname=value;
        }

    }

    public string GetSupportClassString()
    {
        return supp.SupportClassString;
    }


}

public class SupportClass
{
    public string SupportClassString;

}





Example 3
The final example shows how to serialize array of objects.

using System;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
public class SerialTest
{

    public void SerializeNow()
    {
        ClassToSerialize[] c=new ClassToSerialize[3];

        c[0]=new ClassToSerialize();
        c[0].Name="bipin";
        c[0].Age=26;

        c[1]=new ClassToSerialize();
        c[1].Name="abc";
        c[1].Age=75;

        c[2]=new ClassToSerialize();
        c[2].Name="pqr";
        c[2].Age=50;

        ClassToSerialize.CompanyName="xyz";
        File f=new File("temp.dat");
        Stream s=f.Open(FileMode.Create);
        BinaryFormatter b=new BinaryFormatter();
        b.Serialize(s,c);
        s.Close();
    }

    public void DeSerializeNow()
    {
        ClassToSerialize[] c;
        File f=new File("temp.dat");
        Stream s=f.Open(FileMode.Open);
        BinaryFormatter b=new BinaryFormatter();
        c=(ClassToSerialize[])b.Deserialize(s);
        Console.WriteLine("Name :" + c[2].Name);
        Console.WriteLine("Age :" + c[2].Age);
        Console.WriteLine("Company Name :" + ClassToSerialize.CompanyName);
        s.Close();
    }

    public static void Main(string[] s)
    {

        SerialTest st=new SerialTest();
        st.SerializeNow();
        st.DeSerializeNow();

    }

}
public class ClassToSerialize
{
    private int age;
    private string name;
    static string companyname;

    public int Age
    {
        get
        {
            return age;
        }
        set
        {
            age=value;
        }

    }

    public string Name
    {
        get
        {
            return name;
        }
        set
        {
            name=value;
        }

    }

    public static string CompanyName
    {
        get
        {
            return companyname;
        }
        set
        {
            companyname=value;
        }

    }

}

转载于:https://www.cnblogs.com/lxinxuan/archive/2006/09/07/497577.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值