I'm trying to run code similar to this:
using System;
using System.Collections.Generic;
using System.IO;
using System.Xml.Serialization;
namespace ConsoleApplication1
{
[Serializable]
[XmlInclude(typeof(List<Class2>))]
public class Class1
{
private IList<Class2> myArray;
public IList<Class2> MyArray
{
get { return myArray; }
set { myArray = value; }
}
}
public class Class2
{
private int myVar;
public int MyProperty
{
get { return myVar; }
set { myVar = value; }
}
}
class Program
{
static void Main(string[] args)
{
XmlSerializer ser = new XmlSerializer(typeof(Class1), new Type[] { typeof(List<Class2>) });
FileStream stream = File.OpenWrite("Data.xml");
ser.Serialize(stream, new List<Class1>());
stream.Close();
}
}
}
Can somebody explain to me what am I doing wrong?
I get a:
Cannot serialize member .. MyArray ... because it is an interface.
Shouldn't the XmlInclude resolve this?
Serializable interface. Serializable is a marker interface (has no data member and method). It is used to "mark" Java classes so that the objects of these classes may get a certain capability. The Cloneable and Remote are also marker interfaces.
A Java object is serializable if its class or any of its superclasses implements either the java. io. Serializable interface or its subinterface, java. io.
Yes, you can extend the Serializable interface. If you do, all classes that implement the new subinterface will also be implementing Serializable .
Serialization is a mechanism of converting the state of an object into a byte stream. Serialization is done using ObjectOutputStream. Deserialization is the reverse process where the byte stream is used to recreate the actual Java object in memory. This mechanism is used to persist the object.
No. You can't serialize an interface. Ever. It just told you that.
An interface is nothing more than a description of a set of behaviors. It says nothing about the contents of an instance. In particular, although an instance of a class implementing an interface must implement all of its members, it will certainly have properties of its own which need to be serialized.
How would it be deserialized?
What class would be used to deserialize the interface on the other side?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With