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:
[DataContract]
class Item
{
[DataMember]
private IEnumerable<KeyValuePair<string, object>> Member1
{
get { return this._Properties; }
set { this._Properties = Value; }
}
}
Where _Properties is of type:
class PropertiesCollection:IEnumerable<KeyValuePair<string, object>>
{
...
}
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:
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.
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.