Freigeben über


Generating Types with Lists

I have a data contract that contains a collection type but the generated proxy appears as an array. How can I make the proxy use a collection type as well?

I've talked in the past about how the representation of a type in metadata is decoupled from the CLR representation of a type in the service. For example, if I have a data contract that uses a List:

 [DataContract]
class Data
{
   [DataMember]
   public List<string> data;
}

Then, the metadata representation of this data contract is actually described as an array because arrays are the only primitive type for collections in schema.

 <xs:schema xmlns:tns="schemas.datacontract.org/2004/07/" elementformdefault="qualified" targetnamespace="schemas.datacontract.org/2004/07/" xmlns:xs="www.w3.org/2001/XMLSchema">
  <xs:import namespace="schemas.microsoft.com/2003/10/Serialization/Arrays">
  <xs:complexType name="Data">
    <xs:sequence>
      <xs:element xmlns:q1="schemas.microsoft.com/2003/10/Serialization/Arrays" minoccurs="0" name="data" nillable="true" type="q1:ArrayOfstring">
    </xs:element>
  </xs:sequence>
  <xs:element name="Data" nillable="true" type="tns:Data">
</xs:element>
</xs:complexType></xs:import></xs:schema>

However, just as the metadata representation isn't coupled to the service, the metadata representation also isn't coupled to the client. You can on the client generate proxies with any type for this collection that similarly can be serialized or deserialized to an array. The mechanism for doing this with svcutil.exe is the /ct switch.

The /ct switch, which stands for collectionType, allows you to give a qualified type name that is used for collection data types when generating a proxy.

As an example, to get back to the original collection class used by the server, the proxy would need to be constructed using /ct:System.Collections.Generic.List`1 as the option passed to svcutil.exe. However, you could leave the proxy using arrays or provide a different collection class such as /ct:System.Collections.ObjectModel.Collection`1 and with any of these configurations the proxy would be able to exchange messages with the server.

Next time: Setting the Configuration Name

Comments

  • Anonymous
    May 07, 2008
    Why does a data contract with private or internal members generate a proxy with public fields? The obvious