I have an application runnin .NETFramework 4.6.1, using xml serialization for abstract classes and inherited types, with approach almost identical to this XML Serialization and Inherited Types, and it works great. But after porting application to UWP .NETCore I've encountered a strange exception. Here is a simple example to reproduce it.
public class ClassToSerialize
{
[XmlElement(Type = typeof(CustomSerializer<AnotherOne>))]
public AnotherOne anotherOne;
public ClassToSerialize()
{
}
}
public abstract class AnotherOne
{
public AnotherOne()
{
}
}
public class CustomSerializer<TType> : IXmlSerializable
{
public CustomSerializer()
{
}
public CustomSerializer(TType data)
{
m_data = data;
}
public static implicit operator CustomSerializer<TType>(TType data)
{
return data == null ? null : new CustomSerializer<TType>(data);
}
public static implicit operator TType(CustomSerializer<TType> obj)
{
return obj.m_data;
}
private TType m_data;
public XmlSchema GetSchema()
{
return null;
}
public void ReadXml(XmlReader reader)
{
}
public void WriteXml(XmlWriter writer)
{
}
}
And creating XmlSerializer for this type
XmlSerializer sr = new XmlSerializer(typeof(ClassToSerialize));
causes exception
System.InvalidOperationException: TestApp.AnotherOne, TestApp, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null is not assignable from TestApp.CustomSerializer`1[[TestApp.AnotherOne, TestApp, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]], TestApp, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null.
Same code works in .netframework app. Did they changed something in .netcore or am i missing something?