using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
Stream stream;
Document documentBefore = new Document();
documentBefore.Title = "A caammaa ss ss s ss s s s s s s";
documentBefore.Data = "xiannxx xxx xx x";
documentBefore._WindowHandle = 1110011;
Document documentAfter;
using (stream = File.Open(documentBefore.Title + ".bin", FileMode.Create))
{
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(stream, documentBefore);
}
using (stream = File.Open(documentBefore.Title + ".bin", FileMode.Open))
{
BinaryFormatter formatter = new BinaryFormatter();
documentAfter = (Document)formatter.Deserialize(stream);
}
Console.WriteLine(documentAfter.Title);
}
}
[Serializable]
class Document
{
public string Title = null;
public string Data = null;
[NonSerialized]
public long _WindowHandle = 0;
class Image
{ }
[NonSerialized]
private Image Picture = new Image();
}
}
C# Serialize和Deserialize
最新推荐文章于 2025-05-04 17:13:04 发布