I'm trying to serialize a custom object into a file. i've tried many thing but no one works, i must have missed a thing.
Here's the problem.
I have a singleton class i'm using to store my objects. Here is the code :
using DataLibrary.Model.Tests;
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization;
using System.Xml.Serialization;
using testsPsychotechniques.View;
namespace testsPsychotechniques.Model
{
[Serializable()]
public class testSaver : ISerializable
{
private String lastName;
private String firstName;
private List<ITest> tests;
public static testSaver Instance
{
get
{
return Nested.instance;
}
}
public void addTest(ITest test)
{
tests.Add(test);
}
public Boolean save()
{
try
{
FileStream file = File.Open(".\\result.data",
FileMode.Create,
FileAccess.ReadWrite,
FileShare.None);
XmlSerializer serializer = new XmlSerializer(typeof(testSaver));
serializer.Serialize(file, testSaver.Instance);
file.Close();
return true;
}
catch
{
return false;
}
}
private testSaver()
{
this.firstName = Identification.firstName;
this.lastName = Identification.lastName;
this.tests = new List<ITest>();
}
private class Nested
{
internal static readonly testSaver instance = new testSaver();
}
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue("lastName", this.lastName);
info.AddValue("firstName", this.firstName);
info.AddValue("testsResults", this.tests);
}
}
}
Now the real problem :
When i'm calling the save method, in the xml file generated i only have this data :
<?xml version="1.0"?>
<testSaver xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" />
The classes implementing the ITest
interface are all marked serializable and have a method getObjectData.
Another hint is that the functions getObjectData are never used.
Thanks for any help