DatacontractAttribute:
DatacontractAttribute , defined in the System.Runtime.Serialization namespace, is used to declare a type as Data contract.
- DatacontractAttribute can be applied to Enums, Structs and classes only.
- DatacontractAttribute is not inherited
- DatacontractAttribute has only two parameters
Named parameter options for the DatacontractAttribute:-
Name : Determines the type name as it is generated in the resulting schema. The default is
the .Net name as it is declared.
NameSpace: Sets the target namespace for the schema.
The DataMemberAttibute:
It is also part of the System.Runtime.Serialization namespace, is applied to only members and it is declare that the member should be included in the serialization of the data structure. This attributes several parameters for the controlling the resulted schema generated for a complex type.
Named parameter options for the DataMemberAttribute:-
Name:- It controls the schema element name generated for the member the attribute adorns.
The default behavior is to use the filed or property name as defined in the .NET type.
IsRequired:- It controls the min occurs attribute for the schema element. The default value is
false, that is , the element is optional, which is translate into minOccurs=0.
Order:- It controls the order of the each element in the schema. By default, if this property is not
set explicitly, the data members appear alphabetically, followed by elements for which
this property is set explicitly.
EmitDefaultValue:- It controls whether default value will be included in the serialization. This
property is true by default, so that all the datamembers are serialized. If
this property is set to false, any members that is set to its default value
for the type will not be serialized.
Some few points keep in the mind:
- having the EmitDefaultValue property set to false can cause problems when the IsRequired property is set to true because the serializer might emit a default value if does not find something it was expecting when, in fact, the correct behavior might be to thrown an exception.
- If you apply the DataMemberAttribute to both a property and a field that is associated with calculating the property, you will generate duplicate member in the schema.
The EnumMemberAttribute:-
The final Data contract- related attribute to consider is the EnumMemberAttribute. It is used to declare that a given element of an enum that has been declared with the DataContractAttribute
should be considered as part of the Data contract. The only property is the Value property, which can be used to provide an enumerated value to be serialized. The default is its actual value within the enumeration.
No comments :
Post a Comment