Reputation: 11
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?
Upvotes: 1
Views: 2682
Reputation: 34079
Try downloading System.Xml.XmlSerializer
from Nuget. I think it is developed in .NETStandard
here lists all versions of .NET Standard and the platforms supported
Upvotes: 0
Reputation: 1
Use XAML serializer:
nuget> Install-Package Portable.Xaml
public class ClassToSerialize
{
public AnotherOne anotherOne { get; set; }
public ClassToSerialize()
{
}
}
public abstract class AnotherOne
{
public AnotherOne()
{
}
}
public class ContainerOne : AnotherOne
{
public uint placeholder = 0xdeadcafe;
}
public void Test()
{
ClassToSerialize obj = new ClassToSerialize();
obj.anotherOne = new ContainerOne();
//or FileStream..
using (MemoryStream ms = new MemoryStream())
{
Portable.Xaml.XamlServices.Save(ms, obj);
ms.Seek(0, SeekOrigin.Begin);
ClassToSerialize obj2 = Portable.Xaml.XamlServices.Load(ms) as ClassToSerialize;
}
}
Upvotes: 0
Reputation: 11
Your issue linked with type that represented serialization type. For universal App their must be derived from (as I can see) For example:
[TestClass]
public class UnitTest1
{
[TestMethod]
public void TestMethod1()
{
XmlSerializer sr = new XmlSerializer(typeof(ClassToSerialize));
var demo = new DemoChild();
var ser = new ClassToSerialize {anotherOne = demo};
var stream = new MemoryStream();
sr.Serialize(stream, ser);
}
}
public class ClassToSerialize
{
[XmlElement(Type = typeof(DemoChild))]
public AnotherOne anotherOne;
public ClassToSerialize()
{
}
}
public abstract class AnotherOne : IXmlSerializable
{
protected AnotherOne()
{
}
public XmlSchema GetSchema()
{
return null;
}
public void ReadXml(XmlReader reader)
{
}
public void WriteXml(XmlWriter writer)
{
}
}
public class DemoChild: AnotherOne
{
}
Using generic serializer is important for you? I've tested on universal App unit test and all work correctly.
P.s. Type - The of an object derived from the member's type. From documentation
Upvotes: 1