EN
Ce contenu n’est pas disponible dans votre langue. Voici la version anglaise.

DataContractAttribute Class

May 11, 2014

Specifies that the type defines or implements a data contract and can be serialized by a serializer, such as the DataContractSerializer.

System.Object
  System.Attribute
    System.Runtime.Serialization.DataContractAttribute

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

[AttributeUsageAttribute(AttributeTargets.Class|AttributeTargets.Enum|AttributeTargets.Struct, Inherited = false, 
	AllowMultiple = false)]
public sealed class DataContractAttribute : Attribute

The DataContractAttribute type exposes the following members.

  NameDescription
Public methodDataContractAttributeInitializes a new instance of the DataContractAttribute class.
Top

  NameDescription
Public propertyIsReferenceGets or sets a value that indicates whether to preserve object reference data.
Public propertyNameGets or sets the name of the data contract for the type.
Public propertyNamespaceGets or sets the namespace for the data contract for the type.
Top

  NameDescription
Public methodEqualsInfrastructure. Returns a value that indicates whether this instance is equal to a specified object. (Inherited from Attribute.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetHashCodeReturns the hash code for this instance. (Inherited from Attribute.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodMatchWhen overridden in a derived class, returns a value that indicates whether this instance equals a specified object. (Inherited from Attribute.)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Top

Apply the DataContractAttribute attribute to types (classes, structures, or enumerations) that are used in serialization and deserialization operations by the DataContractSerializer. If you send or receive messages by using the Windows Phone infrastructure, you can also apply the DataContractAttribute to any classes that hold and manipulate data sent in messages.

You should also apply the DataMemberAttribute to any field, property, or event that holds values you want to serialize. By applying the DataContractAttribute, you explicitly enable the DataContractSerializer to serialize and deserialize the data.

Data Contracts

A data contract is an abstract description of a set of fields with a name and data type for each field. The data contract exists outside of any single implementation to allow services on different platforms to interoperate. As long as the data passed between the services conforms to the same contract, all the services can process the data. This processing is also known as a loosely coupled system. A data contract is also similar to an interface in that the contract specifies how data must be delivered so that it can be processed by an application. For example, the data contract might call for a data type named Person that has two text fields, named FirstName and LastName. To create a data contract, apply the DataContractAttribute to the class and apply the DataMemberAttribute to any fields or properties that must be serialized. When serialized, the data conforms to the data contract that is implicitly built into the type.

NoteNote:

A data contract differs significantly from an actual interface in its inheritance behavior. Interfaces are inherited by any derived types. When you apply the DataContractAttribute to a base class, the derived types do not inherit the attribute or the behavior. However, if a derived type has a data contract, the data members of the base class are serialized. However, you must apply the DataMemberAttribute to new members in a derived class to make them serializable.

Reusing Existing Types

A data contract has two basic requirements:

  • A stable name.

  • A list of members.

The stable name consists of the namespace uniform resource identifier (URI) and the local name of the contract. By default, when you apply the DataContractAttribute to a class, it uses the class name as the local name and the class's namespace (prefixed with "http://schemas.datacontract.org/2004/07/") as the namespace URI. You can override the defaults by setting the Name and Namespace properties. You can also change the namespace by applying the ContractNamespaceAttribute to the namespace. Use this capability when you have an existing type that processes data exactly as you require but has a different namespace and class name from the data contract. By overriding the default values, you can reuse your existing type and have the serialized data conform to the data contract.

NoteNote:

In any code, you can use the word DataContract instead of the longer DataContractAttribute.


// Define the data contract.
[DataContract(Name = "Customer", Namespace = "http://www.contoso.com", IsReference = true)]
public class User
{
    [DataMember(Name = "Last", EmitDefaultValue = true, IsRequired = true, Order = 2)]
    public string Name { get; set; }

    [DataMember(Order = 1)]
    public int Age { get; set; }

    public User() { }

    public User(string newName, int newAge)
    {
        Name = newName;
        Age = newAge;
    }
}


Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Afficher:
© 2014 Microsoft