• 序列化的概念以及分类


    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.

     

    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 SerializableAttribute attribute 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 ISerializable interface, the serialization effectively gets overridden with a custom version,

    by overriding GetObjectData and 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.

  • 相关阅读:
    linux 定时任务
    fastcgi_param 详解
    MVC 中 注册不成功 或其他操作不成功 提示办法
    关于MVC 中EF调用存储过程
    关于Mvc的分页写法
    关于Linq翻译Inner join ,Left join (本文为转载)
    GridView自定义自增长的 序号 列
    asp.net操作GridView添删改查的两种方法 及 光棒效果
    Jquery编历数组
    在客户与服务器之间传递二进制结构
  • 原文地址:https://www.cnblogs.com/chucklu/p/5145353.html
Copyright © 2020-2023  润新知