Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Serializable Extra Types for .NET 4

, 25 May 2011
Serialization for Rapid Application Development.
SerializableExtraTypes.zip
TestLibraryThree
Properties
TestLibraryThree.csproj.vspscc
TestLibraryTwo
Properties
TestLibraryTwo.csproj.user
TestLibraryTwo.csproj.vspscc
SerializableExtraTypes
AMightyOak.Net.ico
Properties
SerializableExtraTypes.csproj.user
SerializableExtraTypes.csproj.vspscc
SerializableExtraTypes.Tests
Properties
SerializableExtraTypes.Tests.csproj.user
SerializableExtraTypes.Tests.csproj.vspscc
TestLibraryOne
Properties
TestLibraryOne.csproj.user
TestLibraryOne.csproj.vspscc
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Xml;
using System.Xml.Serialization;
using Net.AMightyOak;

namespace System
{
    public static class SerializableExtensionMethods
    {
        /// <summary>
        /// Serialize the object.
        /// </summary>
        /// <typeparam name="T">the Root type used for serialization</typeparam>
        public static string SerializeEx<T>(this T obj)
        {
            Type[] t = SerializableExtraTypes.GetTypes(typeof(T)).ToArray();
            var x = new XmlSerializer(typeof(T), t);
            StringWriter sw = new StringWriter();
            x.Serialize(sw, obj);
            return sw.ToString();
        }

        /// <summary>
        /// Serialize the object
        /// </summary>
        /// <param name="root">the Root type used for serialization</param>
        /// <returns></returns>
        public static string SerializeEx<T>(this T obj, params Type[] root)
        {
            Type[] t = (from y in root
                        select SerializableExtraTypes.GetTypes(y))
                        .Flatten()
                        .Union(SerializableExtraTypes.GetTypes(typeof(T)))
                        .Distinct()
                        .ToArray();
            var x = new XmlSerializer(typeof(T), t);
            StringWriter sw = new StringWriter();
            x.Serialize(sw, obj);
            return sw.ToString();
        }

        /// <summary>
        /// Deserialize the object to the given type.
        /// </summary>
        public static T DeserializeEx<T>(this string xml)
        {
            Type[] t = SerializableExtraTypes.GetTypes(typeof(T)).ToArray();
            var x = new XmlSerializer(typeof(T), t);
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(xml);
            var r = XmlReader.Create(new StringReader(xml));
            if (!x.CanDeserialize(r))
                throw new FormatException(string.Format("Unable to deserialize string into {0}, root node does not match.", typeof(T).FullName));
            return (T)x.Deserialize(r);
        }

        /// <summary>
        /// Deserialize the object to the given type.
        /// </summary>
        /// <param name="root">the root type used for deserialization</param>
        public static T DeserializeEx<T>(this string xml, params Type[] root)
        {
            Type[] t = (from y in root
                        select SerializableExtraTypes.GetTypes(y))
                        .Flatten()
                        .Union(SerializableExtraTypes.GetTypes(typeof(T)))
                        .Distinct()
                        .ToArray();
            var x = new XmlSerializer(typeof(T), t);
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(xml);
            var r = XmlReader.Create(new StringReader(xml));
            if (!x.CanDeserialize(r))
                throw new FormatException(string.Format("Unable to deserialize string into {0}, root node does not match.", typeof(T).FullName));
            return (T)x.Deserialize(r);
        }

        /// <summary>
        /// Returns all types that match the func comparison.
        /// </summary>
        /// <typeparam name="T">they Type that is compared to</typeparam>
        /// <param name="func">comparison method</param>
        /// <param name="asmblys">assemblies to search</param>
        /// <returns>matching types</returns>
        internal static IEnumerable<Type> GetTypes<T>(this T obj, Func<T, Type, bool> func, params Assembly[] asmblys)
        {
            foreach (Assembly a in asmblys)
                foreach (Type t in a.GetTypes())
                    if (func(obj, t))
                        yield return t;
        }

        internal static IEnumerable<T> Flatten<T>(this IEnumerable<IEnumerable<T>> obj)
        {
            foreach (var v in obj)
                foreach (var w in v)
                    yield return w;
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)

About the Author

Daniel Gidman
Architect Stackify
United States United States
No Biography provided
Follow on   Twitter   Google+

| Advertise | Privacy | Mobile
Web02 | 2.8.140721.1 | Last Updated 25 May 2011
Article Copyright 2011 by Daniel Gidman
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid