📄 序列化代码.txt
字号:
相应c#下的序列化代码如下所示,程序把序列化后的数据存入了一个指定的文件file.bin里,分析这个文件数据主要为了能让非.Net下的应用程序读取序列化后数据,这有助于.net与其他语言平台的交互.
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;
}
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.ComponentModel.Container components = null;
public Form1()
{
InitializeComponent();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
this.button1.Location = new System.Drawing.Point(72, 72);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(128, 32);
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
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;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -