英文辞典里serialize有两个解释:1,publish or broadcast(a story or play) in regular installments 2. arrange(something) in a series. 显然计算机术语是延伸了第一个意思,但是story变成了object, 对象而已. publish对象?
Why serialize?
Wikipedia:
In computer science, in the context of data storage and transmission, serialization is the process of converting an object into a sequence of bits so that it can be persisted on a storage medium (such as a file, or a memory buffer) or transmitted across a network connection link. When the resulting series of bits is reread according to the serialization format, it can be used to create a semantically identical clone of the original object. For many complex objects, such as those that make extensive use of references, this process is not straightforward.
Code 1 2 3using System; 4using System.IO; 5using System.Runtime.Serialization; 6using System.Runtime.Serialization.Formatters.Binary; 7 8publicclass Test 9{ 10publicstaticvoid Main() 11{ 12// Create a new TestSimpleObject object. 13 TestSimpleObject obj =new TestSimpleObject(); 14 15 Console.WriteLine("\n Before serialization the object contains: "); 16 obj.Print(); 17 18// Open a file and serialize the object into binary format. 19 Stream stream = File.Open("DataFile.dat", FileMode.Create); 20 BinaryFormatter formatter =new BinaryFormatter(); 21 22try 23{ 24 formatter.Serialize(stream, obj); 25 26// Print the object again to see the effect of the 27//OnSerializedAttribute. 28 Console.WriteLine("\n After serialization the object contains: "); 29 obj.Print(); 30 31// Set the original variable to null. 32 obj =null; 33 stream.Close(); 34 35// Open the file "data.xml" and deserialize the object from it. 36 stream = File.Open("DataFile.dat", FileMode.Open); 37 38// Deserialize the object from the data file. 39 obj = (TestSimpleObject)formatter.Deserialize(stream); 40 41 Console.WriteLine("\n After deserialization the object contains: "); 42 obj.Print(); 43 } 44catch (SerializationException se) 45{ 46 Console.WriteLine("Failed to serialize. Reason: "+ se.Message); 47throw; 48 } 49catch (Exception exc) 50{ 51 Console.WriteLine("An exception occurred. Reason: "+ exc.Message); 52throw; 53 } 54finally 55{ 56 stream.Close(); 57 obj =null; 58 formatter =null; 59 } 60 61 } 62} 63 64 65// This is the object that will be serialized and deserialized. 66[Serializable()] 67publicclass TestSimpleObject 68{ 69// This member is serialized and deserialized with no change. 70publicint member1; 71 72// The value of this field is set and reset during and 73// after serialization. 74privatestring member2; 75 76// This field is not serialized. The OnDeserializedAttribute 77// is used to set the member value after serialization. 78 [NonSerialized()] 79publicstring member3; 80 81// This field is set to null, but populated after deserialization. 82privatestring member4; 83 84// Constructor for the class. 85public TestSimpleObject() 86{ 87 member1 =11; 88 member2 ="Hello World!"; 89 member3 ="This is a nonserialized value"; 90 member4 =null; 91 } 92 93publicvoid Print() 94{ 95 Console.WriteLine("member1 = '{0}'", member1); 96 Console.WriteLine("member2 = '{0}'", member2); 97 Console.WriteLine("member3 = '{0}'", member3); 98 Console.WriteLine("member4 = '{0}'", member4); 99 } 100 101 [OnSerializing()] 102internalvoid OnSerializingMethod(StreamingContext context) 103{ 104 member2 ="This value went into the data file during serialization."; 105 } 106 107 [OnSerialized()] 108internalvoid OnSerializedMethod(StreamingContext context) 109{ 110 member2 ="This value was reset after serialization."; 111 } 112 113 [OnDeserializing()] 114internalvoid OnDeserializingMethod(StreamingContext context) 115{ 116 member3 ="This value was set during deserialization"; 117 } 118 119 [OnDeserialized()] 120internalvoid OnDeserializedMethod(StreamingContext context) 121{ 122 member4 ="This value was set after deserialization."; 123 } 124} 125 126// Output: 127// Before serialization the object contains: 128// member1 = '11' 129// member2 = 'Hello World!' 130// member3 = 'This is a nonserialized value' 131// member4 = '' 132// 133// After serialization the object contains: 134// member1 = '11' 135// member2 = 'This value was reset after serialization.' 136// member3 = 'This is a nonserialized value' 137// member4 = '' 138// 139// After deserialization the object contains: 140// member1 = '11' 141// member2 = 'This value went into the data file during serialization.' 142// member3 = 'This value was set during deserialization' 143// member4 = 'This value was set after deserialization.' 144 145
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· [AI/GPT/综述] AI Agent的设计模式综述