Skip to main content This browser is no longer supported. Show
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
DataContractAttribute Class
DefinitionSpecifies that the type defines or implements a data contract and is serializable by a serializer, such as the DataContractSerializer. To make their type serializable, type authors must define a data contract for their type. In this article
InheritanceAttributesExamplesThe following example serializes and deserializes a class named
RemarksApply 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 Communication Foundation (WCF) infrastructure, you should also apply the DataContractAttribute to any classes that hold and manipulate data sent in messages. For more information about data contracts, see Using Data Contracts. You must 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. Caution You can apply the DataMemberAttribute to private fields. Be aware that the data returned by the field (even if it is private) is serialized and deserialized, and thus can be viewed or intercepted by a malicious user or process. For more information about data contracts, see the topics listed in Using Data Contracts. Data ContractsA 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 may 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. Note 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. XML Schema Documents and the SvcUtil ToolIf you are exchanging data with other services, you must describe the data contract. For the current version of the DataContractSerializer, an XML schema can be used to define data contracts. (Other forms of metadata/description could be used for the same purpose.) To create an XML schema from your application, use the ServiceModel Metadata Utility Tool (Svcutil.exe) with the /dconly command line option. When the input to the tool is an assembly, by default, the tool generates a set of XML schemas that define all the data contract types found in that assembly. Conversely, you can also use the Svcutil.exe tool to create Visual Basic or C# class definitions that conform to the requirements of XML schemas that use constructs that can be expressed by data contracts. In this case, the /dconly command line option is not required. If the input to the Svcutil.exe tool is an XML schema, by default, the tool creates a set of classes. If you examine those classes, you find that the DataContractAttribute has been applied. You can use those classes to create a new application to process data that must be exchanged with other services. You can also run the tool against an endpoint that returns a Web Services Description Language (WSDL) document to automatically generate the code and configuration to create an Windows Communication Foundation (WCF) client. The generated code includes types that are marked with the DataContractAttribute. Reusing Existing TypesA data contract has two basic requirements: a stable name and 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 VersioningA data contract can also accommodate later versions of itself. That is, when a later version of the contract includes extra data, that data is stored and returned to a sender untouched. To do this, implement the IExtensibleDataObject interface. For more information about versioning, see Data Contract Versioning. ConstructorsProperties
Methods
Explicit Interface Implementations
Applies toSee also
Additional resourcesIn this articleIs code that specifies the data attributes and methods for a particular type of object?A class is code that specifies the data attributes and methods for a particular type of object. A class is a definition of the attributes and methods that the class will have.
What method in a class initializes an object's data attributes?The initialization method is a special method that is invoked automatically when an object is created by calling the class. The name of this method is __init__ (two underscore characters, followed by init , and then two more underscores).
What method can be used to place an item in the list at a specific index?Using insert() method you can insert the elements in given list at required index.
Is a set of statements that defines a class's methods and data attributes?the instance is the object created from the class. Each object that is created from a class is called an instance of the class. is a set of statements that define a class's methods and data attributes. the self parameter is required in every method of a class.
|