Click to Rate and Give Feedback

  Switch on low bandwidth view
This page is specific to
Microsoft Silverlight 3

Other versions are also available for the following:
.NET Framework Class Library for Silverlight
DataContractAttribute Class

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

Namespace:  System.Runtime.Serialization
Assembly:  System.Runtime.Serialization (in System.Runtime.Serialization.dll)
Visual Basic (Declaration)
<AttributeUsageAttribute(AttributeTargets.Class Or AttributeTargets.Struct Or AttributeTargets.Enum, Inherited := False,  _
    AllowMultiple := False)> _
Public NotInheritable Class DataContractAttribute _
    Inherits Attribute
Visual Basic (Usage)
Dim instance As DataContractAttribute
C#
[AttributeUsageAttribute(AttributeTargets.Class|AttributeTargets.Struct|AttributeTargets.Enum, Inherited = false, 
    AllowMultiple = false)]
public sealed class DataContractAttribute : Attribute

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 Silverlight version 2 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.

Silverlight 2 also supports a simplified opt-out model for serialization, where these attributes can be omitted from the type and the members of the type to be serialized. The data contract is implicit in the sense that the visibility modifiers (public, private) used by the type determines whether it and its members are included in the data contract. In this model, the name of a member is used to identify it in the serialized representation. A new IgnoreDataMemberAttribute attribute can be used to opt-out a member that is public when required.

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.

Visual Basic

    ' Define the data contract with non-default name and namespace.
    <DataContract(Name := "Customer", Namespace := "http://www.contoso.com")> _
    Public Class User
        Private privateName As String
        <DataMember> _
        Public Property Name() As String
            Get
                Return privateName
            End Get
            Set(ByVal value As String)
                privateName = value
            End Set
        End Property

        Private privateAge As Integer
        <DataMember> _
        Public Property Age() As Integer
            Get
                Return privateAge
            End Get
            Set(ByVal value As Integer)
                privateAge = value
            End Set
        End Property

        Public Sub New()
        End Sub

        Public Sub New(ByVal newName As String, ByVal newAge As Integer)
                Name = newName
                Age = newAge
        End Sub
    End Class



    ' Define the data contract.
    <DataContract(Name := "Buyer", Namespace := "http://www.contoso.com")> _
    Public Class Customer
        Private privateName As String
        <DataMember> _
        Public Property Name() As String
            Get
                Return privateName
            End Get
            Set(ByVal value As String)
                privateName = value
            End Set
        End Property

        'Opt out of the data contract.
        Private privateAge As Integer
        <IgnoreDataMember> _
        Public Property Age() As Integer
            Get
                Return privateAge
            End Get
            Set(ByVal value As Integer)
                privateAge = value
            End Set
        End Property

        Public Sub New()
        End Sub

        Public Sub New(ByVal newName As String, ByVal newAge As Integer)
                Name = newName
                Age = newAge
        End Sub

    End Class


System..::.Object
  System..::.Attribute
    System.Runtime.Serialization..::.DataContractAttribute
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker