By default, the DataContractSerializer serializes all publicly visible types. All public read/write properties and fields of the type are serialized.
You can change the default behavior by applying the DataContractAttribute and DataMemberAttribute attributes to the types and members This feature can be useful in situations in which you have types that are not under your control and cannot be modified to add attributes(for example the types from other compiled assembly). The DataContractSerializer recognizes such "unmarked" types.
the following Customer Type is exactly the type what I metioned above.
namespace CustomerLib { public class Customer { public string Id { get; set; } public string Name { get; set; } public int Age { get; set; } public DateTime Birthday { get; set; } } }
This sample demonstrates the serialization support for unmarked types; that is, types to which serialization attributes have not been applied, sometimes referred to as Plain Old CLR Object (POCO) types. The DataContractSerializer infers a data contract for all public unmarked types that have a default constructor. Data contracts allow you to pass this kind of structured data to and from services. For more information about unmarked types, see Serializable Types.
The Customer
class is used in the ServiceContract
. The Customer
type does not have the DataContractAttribute and DataMemberAttribute attributes, as shown in the following sample code. By default, all public properties and fields are serialized by DataContractSerializer.
namespace CustomerLib { [ServiceContract] interface ICustomerServices { [OperationContract] string GetCustomer(Customer c); } public class CustomerServiceImp : ICustomerServices { public string GetCustomer(Customer c) { c.Name = "wq"; return c.Name; } } }
consequently we will use a test client to consume this ServiceContract by Adding service reference. and we can see the Customer Type is available on the client side .
here is the client main code :
class Program { static void Main(string[] args) { CustomerServicesClient client = new CustomerServicesClient(); string sName =client.GetCustomer(new Customer { Id = "1" }); Console.WriteLine(sName); Console.ReadLine(); } }
that is because the DataContract can serialize the Public get set fields or properties of Types without any attibutes out and from services.
but in this way . maybe you will lose the adventages or capability of DataContract or DataMember attributes.
like:
-
without
[DataContract]
, you cannot define an XML namespace for your data to live in -
without
[DataMember]
, you cannot serialize non-public properties or fields - without
[DataMember]
, you cannot define an order of serialization (Order=
) and the DCS will serialize all properties alphabetically - without
[DataMember]
, you cannot define a different name for your property (Name=
) - without
[DataMember]
, you cannot define things likeIsRequired=
or other useful attributes - without
[DataMember]
, you cannot leave out certain public properties - all public properties will be serialized by the DCS
That is the exactly what said in the MSDN .
You can apply the DataContractAttribute and DataMemberAttribute attributes to explicitly control or customize the serialization of types and members. In addition, you can apply these attributes to private fields.
However, even types that are not marked with these attributes are serialized and deserialized. The following rules and exceptions apply:
- The DataContractSerializer infers a data contract from types without attributes using the default properties of the newly-created types.
- All public fields, and properties with public
get
andset
methods are serialized, unless you apply theIgnoreDataMemberAttribute attribute to that member. - The serialization semantics are similar to those of the XmlSerializer.
- In unmarked types, only public types with constructors that do not have parameters are serialized. The exception to this rule is ExtensionDataObject used with the IExtensibleDataObject interface.
- Read-only fields, properties without a
get
orset
method, and properties with internal or privateset
orget
methods are not serialized. Such properties are ignored and no exception is thrown, except in the case of get-only collections. - XmlSerializer attributes (such as XmlElement, XmlAttribute, XmlIgnore, XmlInclude, and so on) are ignored.
- If you do not apply the DataContractAttribute attribute to a given type, the serializer ignores any member in that type to which the DataMemberAttribute attribute is applied.
- The KnownTypes property is supported in types not marked with the DataContractAttribute attribute. This includes support for the KnownTypeAttribute attribute on unmarked types.
- To "opt out" of the serialization process for public members, properties, or fields, apply theIgnoreDataMemberAttribute attribute to that member.
see also:
http://www.cnblogs.com/zlgcool/archive/2008/11/22/1338850.html