Serialization(序列化)机制

Serialization(序列化)机制

 

1. Serialization and Deserialization

Serialization is the process of converting a complex set of data, such as that which is contained in an object, into a single blob that’s often called a byte stream. The reverse, deserialization, is the process of unpacking the byte stream to re-create the complex set of data.

 

2. Type of Serialization

Remoting uses a type of serialization that finds all the variables contained within an object, and converts their values into a byte stream.

 

Web services use a different serialization scheme. Rather than copying the variables within an object, web-services serialization just scans the object to find any fields that are public in scope, and any public properties that are read-write. Read-only and write-only properties are ignored, as are any nonpublic variables.

 

3. [Serializable()] attribute and ISerializable

Implementing ISerializable is only necessary if the default behavior of serialization is unacceptable, which isn’t usually an issue.

 

4. [NonSerialized()] attribute

Once a class is marked as [Serializable()], all of its member variables will be automatically serialized into a byte stream when the object is serialized. Sometime, however, we can use [NonSerialized()] attribute marks to set some variables that shouldn’t be serialized. In most cases, these will be references to other objects.

 

The one caveat with doing this is that any code that interacts with the above reference object variables must assume that they could be null. If the object is serialized and deserialized, the [NonSerialzied()] reference object variable will have the value null in the newly created object.

 

5. Serialization mechanism

Be aware that Remoting doesn’t transfer the code, but only the data. For this mechanism to work, the DLL containing the required class must be on the client machine along with the client application. In order to deserialize the byte stream on the client, Remoting loads the DLL containing the required class, creates an empty required object, and then populates it with the deserialized data.

 

***

上面关于SerializationDeserialization的阐述来自Rockford LhotkaExpert C# Business Objects》,清楚解释了一些比较容易模糊的概念。

 

 

posted @   Rickie  阅读(1430)  评论(2编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示