系列化例[導入]
類
[Serializable]
????public?class?MyObject?

????
{
????????public?int?n1?=?0;
????????public?int?n2?=?0;
????????public?String?str?=?null;
????}
using?System;
using?System.Drawing;
using?System.Collections;
using?System.ComponentModel;
using?System.Windows.Forms;
using?System.Data;
using?System.IO?;
using?System.Runtime?.Serialization?.Formatters?.Binary?;
using?System.Runtime?.Serialization?;
//系列化
private?void?button1_Click(object?sender,?System.EventArgs?e)

????????
{
????????????MyObject?obj?=?new?MyObject();
????????????obj.n1?=?1;
????????????obj.n2?=?24;
????????????obj.str?=?"Some?String";
????????????IFormatter?formatter?=?new?BinaryFormatter();
????????????Stream?stream?=?new?FileStream("MyFile.bin",?FileMode.Create,?FileAccess.Write,?FileShare.None);
????????????formatter.Serialize(stream,?obj);
????????????stream.Close();

????????}
//反。。。。。。
????????private?void?button2_Click(object?sender,?System.EventArgs?e)

????????
{
????????????IFormatter?formatter?=?new?BinaryFormatter();
????????????Stream?stream?=?new?FileStream("MyFile.bin",?FileMode.Open,?FileAccess.Read,?FileShare.Read);
????????????MyObject?obj?=?(MyObject)?formatter.Deserialize(stream);
????????????stream.Close();
????????????MessageBox.Show?(obj.n1.ToString?()?);

????????}













































posted on 2006-05-28 14:25 record java and net 閱讀(322) 評論(0) 編輯 收藏 所屬分類: dot net相關