
May 7th, 2008, 02:19 AM
|
|
Contributing User
|
|
Join Date: Jul 2004
Posts: 130
Time spent in forums: 23 h 46 m 23 sec
Reputation Power: 5
|
|
|
Error Durring XML Serialization
I am currently creating a Library class that is completely independant of dictionaries since dictionaries do not xml serialize properly. I have created the class and it works great, except I can not get the XMLSerializer to properly serialize the class.
The exception is: "There was an error reflecting type 'ClassLibrary1.Library`3[System.String,System.String,System.String]'."
and the code is
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
namespace ClassLibrary1
{
public class Program
{
static void Main(string[] args)
{
Library<string, string, string> Lib = new Library<string, string, string>();
Lib.Subjects.Add(new LibrarySubject<string,string,string>("Test!"));
Lib["Test!"].Entries.Add(new LibrarySubjectEntry<string,string>("Rawr!", "RARRR"));
Console.WriteLine(Lib["Test!"]["Rawr!"]);
System.IO.FileStream FS = System.IO.File.Create("Test.xml");
XmlSerializer S = new XmlSerializer(Lib.GetType());
S.Serialize(FS, Lib);
FS.Flush();
FS.Close();
}
}
public class Library<TSubKey, TKey, TVal>
where TSubKey : IComparable
where TKey : IComparable
{
public Library()
{
Subjects = new List<LibrarySubject<TSubKey, TKey, TVal>>();
}
[XmlIgnore]
public LibrarySubject<TSubKey, TKey, TVal> this[TSubKey SubjectKey]
{
get
{
return Subjects[Subjects.FindIndex((LibrarySubject<TSubKey, TKey, TVal> Subject) => Subject.SubjectKey.CompareTo(SubjectKey) == 0)];
}
set
{
Subjects[Subjects.FindIndex((LibrarySubject<TSubKey, TKey, TVal> Subject) => Subject.SubjectKey.CompareTo(SubjectKey) == 0)] = value;
}
}
public List<LibrarySubject<TSubKey, TKey, TVal>> Subjects;
}
public class LibrarySubject<TSubKey, TKey, TVal>
where TSubKey : IComparable
where TKey : IComparable
{
public LibrarySubject(TSubKey SubjectKey)
{
this.SubjectKey = SubjectKey;
this.Entries = new List<LibrarySubjectEntry<TKey, TVal>>();
}
[XmlIgnore]
public TVal this[TKey Key]
{
get
{
return Entries[Entries.FindIndex((LibrarySubjectEntry<TKey, TVal> Entry) => Entry.Key.CompareTo(Key) == 0)].Value;
}
set
{
Entries[Entries.FindIndex((LibrarySubjectEntry<TKey, TVal> Entry) => Entry.Key.CompareTo(Key) == 0)].Value = value;
}
}
public TSubKey SubjectKey;
public List<LibrarySubjectEntry<TKey, TVal>> Entries;
}
public class LibrarySubjectEntry<TKey, TVal>
where TKey : IComparable
{
public LibrarySubjectEntry(TKey Key, TVal Value)
{
this.Key = Key;
this.Value = Value;
}
public TKey Key;
public TVal Value;
}
}
Is there any possible way I can get this to serialize using XMLSerializer??
|