title: Collection interfaces and WCF
body: <p>I'm using C# and WCF to do web services. I have a member variable of a class that implements IEnumerable. I tried to have it be serialized as part of my data contract by doing:</p>
  
  <pre><code>[DataContract]
  class Item
  {
      [DataMember]
      private IEnumerable&lt;KeyValuePair&lt;string, object&gt;&gt; Member1
      {
          get { return this._Properties; }
          set { this._Properties = Value; }
      }
  }
  </code></pre>
  
  <p>Where _Properties is of type:</p>
  
  <pre><code>class PropertiesCollection:IEnumerable&lt;KeyValuePair&lt;string, object&gt;&gt;
  {
      ...
  }
  </code></pre>
  
  <p>I thought that WCF would serialize Member1 as an IEnumerable, because that's the type I gave to that member, but I'm getting an InvalidDataContractExceptiom, with this message:</p>
  
  <blockquote>
    <p>Type PropertyCollectioncannot be serialized. Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute.  If the type is a collection, consider marking it with the CollectionDataContractAttribute.  See the Microsoft .NET Framework documentation for other supported types.</p>
  </blockquote>
  
  <p>It seems WCF is looking at the runtime type of the object being passed via Member1 (PropertyCollection) rather than the type of the property (IEnumenable). Is this understanding correct? Is the only fix adding [CollectionDataContract] to PropertiesCollection? I would prefer to keek the service contract non-specific (i.e. it just knows that iot is an IEnumerable), if possible.</p>