XmlSerializer made Generic

Here’s a little generic improvement to the XmlSerializer:

    public class XmlSerializer<T> : XmlSerializer
    {
        public XmlSerializer()
            : base(typeof (T))
        {
        }

        public void Serialize(FileInfo fileInfo, T o)
        {
            using (FileStream stream = File.Create(fileInfo.FullName))
            {
                Serialize(stream, o);
            }
        }

        public static T Deserialize(FileInfo fileInfo)
        {
            var serializer = new XmlSerializer<T>();
            using (FileStream stream = fileInfo.OpenRead())
            {
                var result = (T) serializer.Deserialize(stream);
                return result;
            }
        }
    }

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s


Follow

Get every new post delivered to your Inbox.