Click here to Skip to main content
12,251,889 members (60,916 online)
Click here to Skip to main content

Tagged as

Stats

24.8K views
305 downloads
13 bookmarked
Posted

Serializable Extra Types for .NET 4

, 25 May 2011 GPL3
Serialization for Rapid Application Development.
TestLibraryThree
Properties
TestLibraryThree.csproj.vspscc
TestLibraryTwo
Properties
TestLibraryTwo.csproj.user
TestLibraryTwo.csproj.vspscc
Local.testsettings
SerializableExtraTypes.gpState
SerializableExtraTypes.sln.docstates
SerializableExtraTypes.vsmdi
SerializableExtraTypes.vssscc
TraceAndTestImpact.testsettings
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)

Share

About the Author

Daniel Gidman
Architect Stackify
United States United States
No Biography provided

You may also be interested in...

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