I am trying to create a class that derives from ObservableCollection and restricts itself to only being used with a particular base class (BaseMetadata). It also needs to implement the IXmlSerializable interface as I am adding a persistence capability to the collection.
Here is the class definition of the collection…
public class CollectionMetadata<T> : ObservableCollection<T> where T : BaseMetadata,
IXmlSerializable
{
XmlSchema IXmlSerializable.GetSchema()
{
return null;
}
public void ReadXml(XmlReader reader)
{
}
public void WriteXml(XmlWriter writer)
{
}
}
…the BaseMetadata can be simplied to an empty class and still produce the error…
public class BaseMetadata
{
}
…I get the following error….
CollectionMetadata<T>.IXmlSerializable.GetSchema()':
containing type does not implementinterface
'System.Xml.Serialization.IXmlSerializable'
…on the following line from the above code…
XmlSchema IXmlSerializable.GetSchema()
I must be missing something really obvious?
In it’s current form, you’re applying the
IXmlSerializableas a constraint on theT; not declaring it as an interface ofCollectionMetadata<T>. So your explicit interface implementation is moaning, because it can’t find the interface.You just need to move the
IXmlSerializableinterface to before the constraint:As a side note – it’s potentially a really good thing that you used an explicit interface implementation for this, otherwise this could have been a real head-scratcher later on. Because as a public method, the compiler wouldn’t moan at this point, but would have complained if you’d tried to pass the collection as
IXmlSerializable.