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
[DataMember]
, you cannot define an order of serialization (Order=
) and the DCS will serialize all properties alphabetically[DataMember]
, you cannot define a different name for your property (Name=
)[DataMember]
, you cannot define things like IsRequired=
or other useful attributes[DataMember]
, you cannot leave out certain public properties - all public properties will be serialized by the DCSThat 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:
get
and set
methods are serialized, unless you apply theIgnoreDataMemberAttribute attribute to that member.get
or set
method, and properties with internal or private set
or get
methods are not serialized. Such properties are ignored and no exception is thrown, except in the case of get-only collections.
see also:
http://www.cnblogs.com/zlgcool/archive/2008/11/22/1338850.html