XML-serializer with ABSTRACT class
-
Hi ! I have a problem with serializing a class, which has a variable with an abstract-class-type. If I'm using the specific class Test2, everything works fine, but when I use the abstract class Test, I get the following error-message: An unhandled exception of type 'System.InvalidOperationException' occurred in system.xml.dll Additional information: There was an error generating the XML document. Does anybody know whats the matter here? using System; using System.Xml; using System.Xml.Serialization; using System.IO; namespace xmlTest { class Class1 { [STAThread] static void Main(string[] args) { myObject o = new myObject(); XmlSerializer mySerializer = new XmlSerializer(typeof(myObject)); StreamWriter myWriter = new StreamWriter("test.xml"); mySerializer.Serialize(myWriter, o); } } public class myObject { //public Test2 t = new Test2(); public Test t = new Test2(); } public abstract class Test { public string s = "Test"; } public class Test2:Test { new public string s="Test2"; } } Thank you !!!!
-
Hi ! I have a problem with serializing a class, which has a variable with an abstract-class-type. If I'm using the specific class Test2, everything works fine, but when I use the abstract class Test, I get the following error-message: An unhandled exception of type 'System.InvalidOperationException' occurred in system.xml.dll Additional information: There was an error generating the XML document. Does anybody know whats the matter here? using System; using System.Xml; using System.Xml.Serialization; using System.IO; namespace xmlTest { class Class1 { [STAThread] static void Main(string[] args) { myObject o = new myObject(); XmlSerializer mySerializer = new XmlSerializer(typeof(myObject)); StreamWriter myWriter = new StreamWriter("test.xml"); mySerializer.Serialize(myWriter, o); } } public class myObject { //public Test2 t = new Test2(); public Test t = new Test2(); } public abstract class Test { public string s = "Test"; } public class Test2:Test { new public string s="Test2"; } } Thank you !!!!
As the documentation states, serializable classes must have a publicly accessible default constructor because they are instantiated. Abstract classes cannot be instantiated, however. That is the problem. The solution is easy: don't hardcode the type (using
typeof
in your example) in your serializable code. For one, one of the benefits (one could say, the "point") of using abstract classes is that you can use a generic to refer to multiple types without caring about the specificType
of the object. Instead, useo.GetType()
. Remember that in OO, even though a variable might be declared asTest
orObject
(pulling examples from your code), they will still be instances ofmyObject
orTest2
. So, usingo.GetType()
will still return theType
formyObject
(even if it were declared as anObject
). If you use that in the constructor for theXmlSerializer
, you shouldn't have any problems. This way, also, you don't hardcode that the serializer only acceptsmyObject
. You will want to, however, use a condition or something before serializing to make sure that the object to be serialized is aTest
derivative:public void SaveTest(Test t)
{
// In this case, the CLR verifies that the object is of type Test
// (or a derivative) because the parameter above is a Test type.
XmlSerializer serializer = new XmlSerializer(t.GetType());
StreamWriter writer = new StreamWriter("test.xml");
serializer.Serialize(writer, t);
}
public void Save(object o)
{
// In this case, we should check to make sure that the object is
// a Test type. For brevity, well check and pass to SaveTest.
if (o is Test)
SaveTest(o as Test);
}-----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----