I've a bunch of XML files that are deserialized into different class objects. Instead of having separate methods for each one I'd like to have a single method that can handle all of them. I believe I can do this using the generic type T and interfaces. I've read up on the basic principles of interfaces and type generics and have written the code below, and I'm hoping someone could offer feedback.
public interface ILoader < T >
{
T Load(string path);
}
public XMLReader < T > : ILoader < T >
{
public T Load(string path)
{
T obj;
try
{
XmlSerializer serializer = new XmlSerializer( typeof( T ) );
FileStream stream = new FileStream( path, FileMode.Open );
obj = serializer.Deserialize( stream ) as T;
stream.Close();
}
catch
{
Debug.LogWarning( "Could not open " + FileName );
}
return obj;
}
}
XMLReader<People> r = new XMLReader<People>();
r.Load("People.xml");