当前位置:
首页 > Python基础教程 >
-
C#序列化与反序列化学习笔记(2)
void Main(string[] args)
{
#region 对象序列化为二进制文件
Book book = new Book() { Id = 101, Name = "C#程序设计", Price = 79.5f };
//对象序列化为二进制文件
BinaryFormatter formatter = new BinaryFormatter();
using (FileStream stream = new FileStream(@"E:\book.txt", FileMode.OpenOrCreate))
{
formatter.Serialize(stream, book);
}
Console.Read();
#endregion
}
}
可以通过BinaryFormatter类型实例的Deserialize()方法把二进制文本反序列化为对象,代码如下:
public class Program { /// <summary> /// Book类 /// </summary> [DataContract] [Serializable] public class Book { [DataMember] public int Id { get; set; } [DataMember] public string Name { get; set; } [DataMember] public float Price { get; set; } } static void Main(string[] args) { #region 将二进制文件反序列化为对象 Book book = new Book() { Id = 101, Name = "C#程序设计", Price = 79.5f }; //序列化文件 BinaryFormatter formatter = new BinaryFormatter(); using (FileStream stream = new FileStream(@"E:\book.txt", FileMode.OpenOrCreate)) { Book outBook = formatter.Deserialize(stream) as Book; Console.WriteLine($"{outBook.Id} {outBook.Name} {outBook.Price}"); } Console.Read(); #endregion } }
运行结果如下:
我们同样也可以把序列化和把序列化为二进制文件的方法封装成泛型方法,全部代码如下:
public class Program { /// <summary> /// Book类 /// </summary> [DataContract] [Serializable] public class Book { [DataMember] public int Id { get; set; } [DataMember] public string Name { get; set; } [DataMember] public float Price { get; set; } } /// <summary> /// 序列化反序列化帮助类 /// </summary> public class SerializerHelper { #region 二进制文件序列化反序列化 /// <summary> /// 将对象序列化为字符串 /// </summary> /// <typeparam name="T">类型</typeparam> /// <param name="t">实例</param> /// <returns>字符串</returns> public static string ObjectToString<T>(T t) { BinaryFormatter formatter = new BinaryFormatter(); using (MemoryStream stream = new MemoryStream()) { formatter.Serialize(stream, t); string result = Encoding.UTF8.GetString(stream.ToArray()); return result; } } /// <summary> /// 将对象序列化为二进制文件 /// </summary> /// <typeparam name="T">类型</typeparam> /// <param name="t">实例</param> /// <param name="path">存放路径</param> public static void ObjectToBinaryFile<T>(T t, string path) { BinaryFormatter formatter = new BinaryFormatter(); using (FileStream stream = new FileStream(path, FileMode.OpenOrCreate)) { formatter.Serialize(stream, t); stream.Flush(); } } /// <summary> /// 将字符串反序列为对象 /// </summary> /// <typeparam name="T">类型</typeparam> /// <param name="s">字符串</param> /// <returns>对象</returns> public static T StringToObject<T>(string s) where T : class { byte[] buffer = Encoding.UTF8.GetBytes(s); BinaryFormatter formatter = new BinaryFormatter(); using (MemoryStream stream = new MemoryStream(buffer)) { T result = formatter.Deserialize(stream) as T; return result; } } /// <summary> /// 将二进制文件反序列化为对象 /// </summary> /// <typeparam name="T">类型</typeparam> /// <param name="path">路径</param> /// <returns>对象</returns> public static T BinaryFileToObject<T>(string path) where T : class { using (FileStream stream = new FileStream(path, FileMode.Open)) { BinaryFormatter formatter = new BinaryFormatter(); T result = formatter.Deserialize(stream) as T; return result; } } #endregion } static void Main(string[] args) { #region 二进制文件序列化反序列化泛型方法 Book book = new Book() { Id = 101, Name = "C#程序设计", Price = 79.5f }; //对象序列化为二进制文件 SerializerHelper.ObjectToBinaryFile(book, @"E:\book.txt"); //二进制文件反序列化为对象 Book outBook = SerializerHelper.BinaryFileToObject<Book>(@"E:\book.txt") as Book; Console.WriteLine($"{outBook.Id} {outBook.Name} {outBook.Price}"); Console.Read(); #endregion } }
运行结果如下: