Reputation: 3906
I have a simple serialization class for writing my custom objects to XML on disk:
public class ReaderWriter
{
public static void StoreObjectInformationInFile<T>(T objectToBeWritten, string passedDestinationFileName)
{
XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(objectToBeWritten.GetType());
TextWriter writer = new StreamWriter(passedDestinationFileName);
serializer.Serialize(writer, objectToBeWritten);
writer.Close();
}
}
Is there a way to add a Generic Deserialization function to this Class that does the same thing but in reverse? something like this:
public static <T> GetObjectInformationFromFile<T>(string passedDestinationFileName)
{
...
}
Upvotes: 0
Views: 67
Reputation: 33381
Here is the prototype of generic deserialization method
public static T GetObjectInformationFromFile<T>(string passedDestinationFileName)
{
var serializer = new XmlSerializer(typeof(T));
using(var stream = new StreamReader(passedDestinationFileName))
{
return (T)serializer.Deserialize(stream);
}
}
Upvotes: 2