Take the 2-minute tour ×

I am writing a plugin for an application that exposes an abstract base class type, and from which my class is supposed to be derived. When it stores my instantiated class away for later retrieval, the application uses its XML serializer to serialize the class, without exposing the serializer to me.

Obviously, when it tries to serialize my class, the XML serializer throws a System.InvalidOperationException exception, with the message

"The type YourDerivedType was not expected. Use the XmlInclude or SoapInclude attribute to specify types that are not known statically."

Since I don't have access to the base class, I cannot decorate it with the XmlInclude attribute, and because the application uses its own serializer, I cannot use a new XmlSerializer, pass my derived type to its constructor, and then use this XmlSerializer to serialize.

Any suggestions on how (or whether!) I can get my class serialized and make this exception go away?

share|improve this question
This does all sound harder than it should be. Are there no guidelines provided by the application you're plugging into? If they've specifically designed this architecture for plugins either it should be straightforward or they've messed it up. If it's an established app then presumably not the latter. Any examples you can refer to to see if you're approaching it wrong? –  Rory Jan 23 '11 at 23:19
I agree Rory, it is more convoluted than it ideally should be. I have contacted the product team of the application for any suggestions on how we can get the thing to work. –  Satyajit Jan 24 '11 at 7:16

1 Answer 1

Try implementing IXmlSerializable in your derived class, and hoping that the XML Serializer recognises it and uses it. If not, I think you're out of luck.

Make sure your class has a parameter-less constructor (easy to forget but essential for IXmlSerializable).

share|improve this answer
Hi Rory, yes I just tried doing that, but I am now confronted with a different System.InvalidOperationException exception, with the message: "The type YourDerivedType may not be used in this context. to use YourDerivedType as a parameter, return type, or member of a class or struct, the parameter, return type, or member must be declared as type YourDerivedType (it cannot be object). Objects of type YourDerivedType may not be used in un-typed collections, such as ArrayLists." Due to this exception, I haven't yet reached the point when any of my IXmlSerializable methods are actually being called –  Satyajit Jan 23 '11 at 20:05

Your Answer


