Build your .Read()
method as a static function that returns the read object:
public static Options Read(string path)
{
XmlSerializer deserializer = new XmlSerializer(typeof(Options));
using (TextReader textReader = new StreamReader(path))
{
return (Options)deserializer.Deserialize(textReader);
}
}
Then change your calling code so rather than something like this:
Options myOptions = new Options();
myOptions.Read(@"C:Options.xml");
You do something like this:
Options myOptions = Options.Read(@"C:Options.xml");
The nice difference is that it's impossible to ever have an Options object that doesn't have some data behind it.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…