序列化的概念以及分类
2016-01-20 15:26
337 查看
1.what is serialization in c#?
Serialization is the process of converting an object into a stream of bytes in order to store the object or transmit it to memory, a database, or a file.
Its main purpose is to save the state of an object in order to be able to recreate it when needed.
The reverse process is called deserialization.
2.How many serialization formatters here?Explain them
(1)Binary and XML Serialization
Either binary or XML serialization can be used.
In binary serialization, all members, even those that are read-only, are serialized, and performance is enhanced.
XML serialization provides more readable code, as well as greater flexibility of object sharing and usage for interoperability purposes.
①Binary serialization
Binary serialization uses binary encoding to produce compact serialization for uses such as storage or socket-based network streams.
②XML serialization
XML serialization serializes the public fields and properties of an object, or the parameters and return values of methods, into an XML stream that conforms to a specific XML Schema definition language (XSD) document.
XML serialization results in strongly typed classes with public properties and fields that are converted to XML.
System.Xml.Serialization contains the classes necessary for serializing and deserializing XML.
You can apply attributes to classes and class members in order to control the way the XmlSerializer serializes or deserializes an instance of the class.
③SOAP Serialization
XML serialization can also be used to serialize objects into XML streams that conform to the SOAP specification.
SOAP is a protocol based on XML, designed specifically to transport procedure calls using XML.
As with regular XML serialization, attributes can be used to control the literal-style SOAP messages generated by an XML Web service.
(2)Basic and Custom Serialization
Serialization can be performed in two ways, basic and custom. Basic serialization uses the .NET Framework to automatically serialize the object.
①Basic Serialization
The only requirement in basic serialization is that the object has the SerializableAttribute attribute applied. The NonSerializedAttribute can be used to keep specific fields from being serialized.
When you use basic serialization, the versioning of objects may create problems, in which case custom serialization may be preferable.
Basic serialization is the easiest way to perform serialization, but it does not provide much control over the process.
②Custom Serialization
In custom serialization, you can specify exactly which objects will be serialized and how it will be done.
The class must be markedSerializableAttribute and implement the ISerializable interface.
If you want your object to be deserialized in a custom manner as well, you must use a custom constructor.
(3)Designer Serialization
Designer serialization is a special form of serialization that involves the kind of object persistence usually associated with development tools.
Designer serialization is the process of converting an object graph into a source file that can later be used to recover the object graph.
A source file can contain code, markup, or even SQL table information. For more information, see Designer Serialization Overview.
Demonstrates how serialization can be used to persist an object's data between instances, allowing you to store values and retrieve them the next time the object is instantiated.
How to: Read Object Data from an XML File (C# and Visual Basic)
Shows how to read object data that was previously written to an XML file using the XmlSerializer class.
How to: Write Object Data to an XML File (C# and Visual Basic)
Shows how to write the object from a class to an XML file using the XmlSerializer class.
扩展
What is ISerializable interface used for?
Allows an object to control its own serialization and deserialization.
What is the differences between Serializable attribute and ISerializable interface?
Serializable attribute indicates that a class can be serialized. This class cannot be inherited.
When you use the
in such a way that when at run-time, the serializing facilities will know what to serialize based on the attributes by performing reflection on the class/module/assembly type.
The above indicates that the serializing facility should serialize the entire class
Using the attribute you can selectively choose which fields needs to be serialized.
When you implement the
by overriding
See also this example of a custom serialization here on StackOverflow. It shows how to keep the serialization backwards-compatible with different versionings of the serialized data.
Serialization is the process of converting an object into a stream of bytes in order to store the object or transmit it to memory, a database, or a file.
Its main purpose is to save the state of an object in order to be able to recreate it when needed.
The reverse process is called deserialization.
2.How many serialization formatters here?Explain them
(1)Binary and XML Serialization
Either binary or XML serialization can be used.
In binary serialization, all members, even those that are read-only, are serialized, and performance is enhanced.
XML serialization provides more readable code, as well as greater flexibility of object sharing and usage for interoperability purposes.
①Binary serialization
Binary serialization uses binary encoding to produce compact serialization for uses such as storage or socket-based network streams.
②XML serialization
XML serialization serializes the public fields and properties of an object, or the parameters and return values of methods, into an XML stream that conforms to a specific XML Schema definition language (XSD) document.
XML serialization results in strongly typed classes with public properties and fields that are converted to XML.
System.Xml.Serialization contains the classes necessary for serializing and deserializing XML.
You can apply attributes to classes and class members in order to control the way the XmlSerializer serializes or deserializes an instance of the class.
③SOAP Serialization
XML serialization can also be used to serialize objects into XML streams that conform to the SOAP specification.
SOAP is a protocol based on XML, designed specifically to transport procedure calls using XML.
As with regular XML serialization, attributes can be used to control the literal-style SOAP messages generated by an XML Web service.
(2)Basic and Custom Serialization
Serialization can be performed in two ways, basic and custom. Basic serialization uses the .NET Framework to automatically serialize the object.
①Basic Serialization
The only requirement in basic serialization is that the object has the SerializableAttribute attribute applied. The NonSerializedAttribute can be used to keep specific fields from being serialized.
When you use basic serialization, the versioning of objects may create problems, in which case custom serialization may be preferable.
Basic serialization is the easiest way to perform serialization, but it does not provide much control over the process.
②Custom Serialization
In custom serialization, you can specify exactly which objects will be serialized and how it will be done.
The class must be markedSerializableAttribute and implement the ISerializable interface.
If you want your object to be deserialized in a custom manner as well, you must use a custom constructor.
(3)Designer Serialization
Designer serialization is a special form of serialization that involves the kind of object persistence usually associated with development tools.
Designer serialization is the process of converting an object graph into a source file that can later be used to recover the object graph.
A source file can contain code, markup, or even SQL table information. For more information, see Designer Serialization Overview.
Related Topics and Examples
Walkthrough: Persisting an Object (C# and Visual Basic)Demonstrates how serialization can be used to persist an object's data between instances, allowing you to store values and retrieve them the next time the object is instantiated.
How to: Read Object Data from an XML File (C# and Visual Basic)
Shows how to read object data that was previously written to an XML file using the XmlSerializer class.
How to: Write Object Data to an XML File (C# and Visual Basic)
Shows how to write the object from a class to an XML file using the XmlSerializer class.
扩展
What is ISerializable interface used for?
Allows an object to control its own serialization and deserialization.
What is the differences between Serializable attribute and ISerializable interface?
Serializable attribute indicates that a class can be serialized. This class cannot be inherited.
When you use the
SerializableAttributeattribute you are putting an attribute on a field at compile-time
in such a way that when at run-time, the serializing facilities will know what to serialize based on the attributes by performing reflection on the class/module/assembly type.
[Serializable] public class MyFoo { … }
The above indicates that the serializing facility should serialize the entire class
MyFoo, whereas:
public class MyFoo { private int bar; [Serializable] public int WhatBar { get { return this.bar; } } }
Using the attribute you can selectively choose which fields needs to be serialized.
When you implement the
ISerializableinterface, the serialization effectively gets overridden with a custom version,
by overriding
GetObjectDataand
SetObjectData(and by providing a constructor of the form
MyFoo(SerializationInfo info, StreamingContext context)), there would be a finer degree of control over the serializing of the data.
See also this example of a custom serialization here on StackOverflow. It shows how to keep the serialization backwards-compatible with different versionings of the serialized data.
相关文章推荐
- C++移位运算符详解
- ArrayList用法
- 手动ecache处理
- I.MX6 默认打开 USB adb
- phpstorm使用教程
- 92、QOS区分式服务配置实验之分类&标记
- Unity3D插件之FingerGesture的使用
- mysql、ftp迁移连接慢的问题
- 数据结构的基础学习1
- awakeFromNib,自定义xib的UIView和UIViewController 重载它
- 程序员:如何接手垃圾代码?
- Java中测试异常的多种方式
- PHP移动互联网开发笔记(5)——文件的上传下载
- 快速理解聚合根、实体、值对象的区别和联系
- isNaN() 函数用于检查其参数是否是非数字值。如果是非数字值则返回true
- Unity unsafe
- vmware 无法连接的问题
- boost 统计分布之正态分布
- windows下遍历当前任务管理器中的服务的列表
- 常用Xcode插件推荐(持续更新)