KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
The XML serialization in .NET allows polymorphic objects through the extraTypes[] parameter of the XmlSerializer constructor. It also allows customization of XML serialization for types that implement IXmlSerializable . However, I’m unable to combine these two features – as demonstrated in this minimal example: using System; using System.IO; using System.Xml; using System.Xml.Schema; using System.Xml.Serialization; namespace CsFoo { public class CustomSerializable : IXmlSerializable { public XmlSchema GetSchema() { return null; } public void ReadXml(XmlReader xr) { } public void WriteXml(XmlWriter xw) { } } class CsFoo { static void Main() { XmlSerializer xs = new XmlSerializer( typeof(object), new Type[] { typeof(CustomSerializable) }); xs.Serialize(new StringWriter(), new CustomSerializable()); } } The last line throws System.InvalidOperationException with this message: The type CsFoo.CustomSerializable may not be used in this context to use CsFoo.CustomSerializable as a parameter, return type, or member of a class or struct, the parameter, return type, or member must be declared as type CsFoo.CustomSerializable (it cannot be object). Objects of type CsFoo.CustomSerializable may not be used in un-typed collections, such as ArrayLists. Wading through the dynamically generated XML assemblies, we ultimately come back to .NET standard library code by calling: System.Xml.Serialization.XmlSerializationWriter.WriteTypedPrimitive( String, String, Object, Boolean) : Void In turn, this leads to: protected Exception CreateUnknownTypeException(Type type) { if (typeof(IXmlSerializable).IsAssignableFrom(type)) { return new InvalidOperationExce
Tags (comma-separated)
Save Edits
Cancel