- Home /
Serializing invetory items data and InvalidOperationException exception
Hi, I have this base class.
using System;
using System.Collections.Generic;
[Serializable]
public class TestBase
{
public int value;
public TestBase()
{
}
public TestBase(int val)
{
value = val;
}
}
And this inherited class
using System;
using System.Collections.Generic;
[Serializable]
class TestChild : TestBase
{
public int childValue;
public TestChild(int parentVal, int childVal): base(parentVal)
{
childValue = childVal;
}
}
Then I have a SerializbleDictionary you might have already seen somewhere on the internet, which I am using like SerializableDictionary. Code for serialization looks like this.
public void WriteXml(System.Xml.XmlWriter writer)
{
XmlSerializer keySerializer = new XmlSerializer(typeof(TKey));
XmlSerializer valueSerializer = new XmlSerializer(typeof(TValue));
foreach (TKey key in this.Keys)
{
writer.WriteStartElement("item");
writer.WriteStartElement("key");
keySerializer.Serialize(writer, key);
writer.WriteEndElement();
writer.WriteStartElement("value");
TValue value = this[key];
valueSerializer.Serialize(writer, value);
writer.WriteEndElement();
writer.WriteEndElement();
}
}
This is obviously just model of problem, in real I am got some items and it would be nice to take advantages of polymorphism. However, when I try to serialize it, I get
System.InvalidOperationException: The type of the argument object 'TestChild' is not primitive.
Can someone explain me please what is the problem ? I have been googling around, found other people fighting with this exception but their problems seemed quite different.
Thank you in advance.
Your answer
Follow this Question
Related Questions
Scriptable Object List values are always null 1 Answer
Saving and Loading XML in Standalone 1 Answer
Which is first, Xml Assignment or Constructor? 1 Answer
Serializing an Action as XML? 0 Answers
Problems with the XML Serializer 1 Answer