Type ‘MyType’ is an invalid collection...
Recently I was adding some new features to DivotDog that use JSON. This JSON was intended to be a serialized version of a custom collection that was needed. In an effort to take advantage of WCF’s baked-in serialization features, I created a collection that looks something like the following:
[DataContract]
public class MyTypeCollection : CollectionBase
{
public MyTypeCollection()
{ }
public int Add(MyType item)
{ return List.Add(item); }
public void Insert(int index, MyType item)
{ List.Insert(index, item); }
public void Remove(MyType item)
{ List.Remove(item); }
public bool Contains(MyType item)
{ return List.Contains(item); }
public int IndexOf(MyType item)
{ return List.IndexOf(item); }
public void CopyTo(MyType[] array, int index)
{ List.CopyTo(array, index); }
}
While everything compiled fine, when I navigated to MyService.svc in my browser window, I received an error that stated:
Type ‘MyTypeCollection’ is an invalid collection type since it has DataContractAttribute attribute
To overcome this issue, I simply replaced the DataContract attribute on MyTypeCollection with an attribute called CollectionDataContract. This attribute basically enables you to easily serialize a custom collection. More information can be found here.

