什么是串行化

什么是串行化如果你处在计算机行业 肯定听说过串行化这个词

大家好,欢迎来到IT知识分享网。


前言

如果你处在计算机行业,肯定听说过串行化这个词。串行化是计算机行业的一个术语,我之前的文章中也提到过,术语是为了方便大家沟通交流的。不要害怕陌生的术语。下面我们从宏观上介绍串行化。


一、什么是串行化?

二、使用步骤

1.定义可被串行化的类

代码如下(示例):

[Serializable] public class Person { 
      public string name; public int age; [NonSerialized] public float weight; } 

C#使用Serializable来表示可串行化类。不需要初始化的属性,用NonSerialized关键字标识。

2.加入串行化函数

代码如下(示例):

[Serializable] public class Person { 
      public string name; public int age; [NonSerialized] public float weight; public void Serialize(string serFile) { 
      using (MemoryStream ms = new MemoryStream()) { 
      BinaryFormatter formatter = new BinaryFormatter(); formatter.Serialize(ms, this); File.WriteAllBytes(serFile, ms.ToArray()); } } } 

3.加入反串行化函数

代码如下(示例):

[Serializable] public class Person { 
      public string name; public int age; [NonSerialized] public float weight; public void Serialize(string serFile) { 
      using (MemoryStream ms = new MemoryStream()) { 
      BinaryFormatter formatter = new BinaryFormatter(); formatter.Serialize(ms, this); File.WriteAllBytes(serFile, ms.ToArray()); } } public static Person Deserialize(string serFile) { 
      byte[] buff = File.ReadAllBytes(serFile); using (MemoryStream ms = new MemoryStream(buff)) { 
      BinaryFormatter formatter = new BinaryFormatter(); Person person = (Person)formatter.Deserialize(ms); return person; } } } 

反串行化函数一般是Static的成员函数,因为就是要用这个函数产生类,所以不能把它做成类的普通成员函数。这就成了先有鸡还是先有蛋的问题。


总结

串行化是软件行业避不过去的一个词,如果从宏观上理解了串行化的含义,对于以后的学习大有裨益。

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://haidsoft.com/123034.html

(0)
上一篇 2025-10-12 09:45
下一篇 2025-10-12 10:00

相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注微信