How do you ignore a function / method of a class so that you can serialize it to disk for later use?
I have searched and really cant find any answers. I need the ToString()because I add each instance of the Person to a list box. The ToString() is what is displayed in the list box. I then use the selected items to get it back again to work with the object.
x = (Person)listbox selected items.
If I try t serialize it as below it blow up.
public class Person
{
public Person()
{
messages = new List<Message>();
}
public string name{ get; set;}
public List<Message> messages{ get; set;}
// How do you ignore this????
// [XMLIgnore] does not work
public override string ToString()
{
return name;
}
}
Here is the call to serialize it. It blows up when I try to create the XmlSearlizer object.
Other classes that have no methods / functions work fine. No problems.
public void serializeToXML<T>(T test)
{
// If object has method or function it blows up here.
XmlSerializer serializer = new XmlSerializer(typeof (T));
// Hardcoded just for a test.
TextWriter textWriter = new StreamWriter(@"C:\test.xml");
serializer.Serialize(textWriter, test);
textWriter.Close();
}