Home  >  Article  >  Backend Development  >  C# WCF DataContractSerializer 类

C# WCF DataContractSerializer 类

黄舟
黄舟Original
2017-02-15 11:28:381307browse

Original address: Click to open the link

## DataContractSerializer class

Serializes and deserializes type instances to an XML stream or document using the provided data contract. This class cannot be inherited.

Namespace: System.Runtime.Serialization

## Assembly: System.Runtime .Serialization (in System.Runtime.Serialization.dll)

Remarks## Use the DataContractSerializer class to convert types Instances are serialized and deserialized into XML streams or documents. You can specify which properties and fields to serialize by applying the DataContractAttribute attribute to classes and the DataMemberAttribute attribute to class members.

The literal meaning is: data contract serialization. This article mainly explains serialization and deserialization using DataContractSerializer.

DataContractAttribute From the AttributeUsageAttribute attribute applied to DataContractAttribute, This feature can only be used for enumerations, classes, and structures and cannot be applied to interfaces. From the keyword sealed, we know that the DataContractAttribute cannot be inherited. The AllowMutiple attribute is False, indicating that only one DataContractAttribute attribute can be applied to a data type.

From the above definition of DataContractAttribute, we can see that DataContractAttribute only contains 5 attribute members, among which Name and Namespace represent the name and namespace of the data contract, and IsReference represents whether to maintain the existing reference of the object during serialization. Structure, the default value of this attribute is False. Data contract members adopt a display selection mechanism, that is to say, attributes/fields of data types to which the DataContractAttribute attribute is applied will not automatically generate data members of the contract, but only those attributes/fields to which the DataMemberAttribute attribute is applied. Be a member of the data contract.


//------------------------------------------------------------
// Copyright (c) Microsoft Corporation.  All rights reserved.
//------------------------------------------------------------
 
namespace System.Runtime.Serialization
{
	// 摘要:
    //     指定该类型要定义或实现一个数据协定,并可由序列化程序(如 System.Runtime.Serialization.DataContractSerializer)进行序列化。
    //     若要使其类型可序列化,类型作者必须为其类型定义数据协定。
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Enum, Inherited = false, AllowMultiple = false)]
    public sealed class DataContractAttribute : Attribute
    {
        string name;
        string ns;
        bool isNameSetExplicitly;
        bool isNamespaceSetExplicitly;
        bool isReference;
        bool isReferenceSetExplicitly;
		// 摘要:
        //     初始化 System.Runtime.Serialization.DataContractAttribute 类的新实例。
        public DataContractAttribute()
        {
        }
		 // 摘要:
         //     获取或设置一个值,该值指示是否保留对象引用数据。
         //
         // 返回结果:
         //     如果使用标准 XML 保留对象引用数据,则为 true;否则为 false。 默认值为 false。
        public bool IsReference
        {
            get { return isReference; }
            set
            {
                isReference = value;
                isReferenceSetExplicitly = true;
            }
        } 
	
        public bool IsReferenceSetExplicitly
        {
            get { return isReferenceSetExplicitly; }
        }
 
		//
         // 摘要:
         //     获取或设置类型的数据协定的命名空间。
         //
         // 返回结果:
         //     协定的命名空间。
        public string Namespace
        {
            get { return ns; }
            set
            {
                ns = value;
                isNamespaceSetExplicitly = true;
            }
        }
 
        public bool IsNamespaceSetExplicitly
        {
            get { return isNamespaceSetExplicitly; }
        }
 
		//
        // 摘要:
        //     获取或设置类型的数据协定的名称。
        //
        // 返回结果:
        //     数据协定的本地名称。 默认值是应用了该属性的类的名称。
        public string Name
        {
            get { return name; }
            set
            {
                name = value;
                isNameSetExplicitly = true;
            }
        }
 
        public bool IsNameSetExplicitly
        {
            get { return isNameSetExplicitly; }
        }
 
    }
}

DataMemberAttribute



## The AttributeUsageAttribute attribute applied to DataMemberAttribute from above It seems that this feature can only be applied to fields and attributes. Because only these two elements are "data" members. The four attributes represent the following meanings respectively.


Name: The name of the data member, defaults to the name of the field or attribute.

Order: The position where the corresponding data member appears in the final serialized XML. The smaller the Order value, the better the test. The default value is -1.

IsRequired: Indicates whether the attribute member is a required member. The default value is false, indicating that this member can be defaulted.


EmitDefaultValue: Gets or sets a value that specifies whether to serialize the default value of the field or property being serialized. true if the member's default value should be generated in the serialization stream; otherwise, false. The default value is true.

  • Source code source: Click to open the link

  • The above is the content of the C# WCF DataContractSerializer class. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn