.net下二进制序列化
时间:2011-04-23 来源:Y#
c#下的序列化代码如下所示
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace WindowsApplication2
{
[Serializable]
public class Object5
{
public int i1 = 0;
public int i2 = 0;
public float f3=0;
public string str;
}
private void button1_Click(object sender, System.EventArgs e)
{
Object5 obj = new Object5();
obj.i1 = 128;
obj.i2 = 24;
obj.f3=1.3f;
obj.str = "Some String";
double d1=1.3d;
float f1=1.3f;
int i1=1;
string s1="HelloWorld";
System.Runtime.Serialization.IFormatter formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
System.IO.Stream stream = new System.IO.FileStream("File.bin", System.IO.FileMode.Create, System.IO.FileAccess.Write, System.IO.FileShare.None);
formatter.Serialize(stream, obj);
formatter.Serialize(stream,d1);
formatter.Serialize(stream,f1);
formatter.Serialize(stream,i1);
formatter.Serialize(stream,s1);
stream.Close();
formatter=null;
}
}
相关阅读 更多 +










