Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

DataMemberAttribute Class

When applied to the member of a type, specifies that the member is part of a data contract and is serializable by the DataContractSerializer.

System.Object
  System.Attribute
    System.Runtime.Serialization.DataMemberAttribute

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

[AttributeUsageAttribute(AttributeTargets.Property|AttributeTargets.Field, Inherited = false, 
	AllowMultiple = false)]
public sealed class DataMemberAttribute : Attribute

The DataMemberAttribute type exposes the following members.

  NameDescription
Public methodSupported by Silverlight for Windows PhoneDataMemberAttributeInitializes a new instance of the DataMemberAttribute class.
Top

  NameDescription
Public propertySupported by Silverlight for Windows PhoneEmitDefaultValueGets or sets a value that specifies whether to serialize the default value for a field or property being serialized.
Public propertySupported by Silverlight for Windows PhoneIsRequiredGets or sets a value that instructs the serialization engine whether the member must be present in the serialized data when it is being deserializing.
Public propertySupported by Silverlight for Windows PhoneNameGets or sets a data member name.
Public propertySupported by Silverlight for Windows PhoneOrderGets or sets the order of serialization and deserialization of a member.
Top

  NameDescription
Public methodSupported by Silverlight for Windows PhoneEqualsInfrastructure. Returns a value that indicates whether this instance is equal to a specified object. (Inherited from Attribute.)
Protected methodSupported by Silverlight for Windows PhoneFinalizeAllows an object to try to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.)
Public methodSupported by Silverlight for Windows PhoneGetHashCodeReturns the hash code for this instance. (Inherited from Attribute.)
Public methodSupported by Silverlight for Windows PhoneGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodSupported by Silverlight for Windows PhoneMatchWhen overridden in a derived class, returns a value that indicates whether this instance equals a specified object. (Inherited from Attribute.)
Protected methodSupported by Silverlight for Windows PhoneMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodSupported by Silverlight for Windows PhoneToStringReturns a string that represents the current object. (Inherited from Object.)
Top

Apply the DataMemberAttribute attribute in conjunction with the DataContractAttribute to identify members of a type that are part of a data contract. One of the serializers that can serialize data contracts is the DataContractSerializer.

The data contract model for Silverlight 5 is an opt-out model. This means that the members are serialized by default unless the IgnoreDataMemberAttribute is applied to a field or property explicitly to specify that the member value is not to be serialized.

By default, the CLR member name is used as the name of the data member. By setting the Name property, you can customize the name of the data member. This can be used to provide a name that may not be allowed as a CLR member name. When mapping to XML using the DataContractSerializer, this name is used as the name of the schema element in a type.


	    // Define the data contract.
    	[DataContract(Name = "Customer" , Namespace = "http://www.contoso.com" ) ]
    	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;
        	}
    	}


Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Windows Phone OS 7.0

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

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

Community Additions

Show:
© 2014 Microsoft