Click here to Skip to main content
15,898,010 members
Articles / Programming Languages / XML

Yet Another XML Serialization Library for the .NET Framework

Rate me:
Please Sign up or sign in to vote.
4.92/5 (91 votes)
2 Oct 2012MIT24 min read 518.1K   207  
A flexible XML serialization library that lets developers design the XML file structure, and select the exception handling policy. YAXLib supports polymorphic serialization and serializing generic and non-generic collection classes and arrays.
// Copyright 2009 - 2010 Sina Iravanian - <sina@sinairv.com>
//
// This source file(s) may be redistributed, altered and customized
// by any means PROVIDING the authors name and all copyright
// notices remain intact.
// THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED. USE IT AT YOUR OWN RISK. THE AUTHOR ACCEPTS NO
// LIABILITY FOR ANY DATA DAMAGE/LOSS THAT THIS PRODUCT MAY CAUSE.
//-----------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace YAXLib
{
    /// <summary>
    /// Implements a singleton pool of type-wrappers to prevent excessive creation of 
    /// repetetive type wrappers
    /// </summary>
    internal class TypeWrappersPool
    {
        /// <summary>
        /// The instance to the pool, to implement the singleton
        /// </summary>
        private static TypeWrappersPool s_instance = null;

        /// <summary>
        /// A dictionary from types tp their corresponding wrappers
        /// </summary>
        private Dictionary<Type, UdtWrapper> m_dicTypes = new Dictionary<Type, UdtWrapper>();

        /// <summary>
        /// Prevents a default instance of the <c>TypeWrappersPool</c> class from being created, from
        /// outside the scope of this class.
        /// </summary>
        private TypeWrappersPool()
        {
        }

        /// <summary>
        /// Gets the type wrappers pool.
        /// </summary>
        /// <value>The type wrappers pool.</value>
        public static TypeWrappersPool Pool
        {
            get
            {
                if (s_instance == null)
                    s_instance = new TypeWrappersPool();
                return s_instance;
            }
        }

        /// <summary>
        /// Cleans up the pool.
        /// </summary>
        public static void CleanUp()
        {
            if (s_instance != null)
            {
                s_instance = null;
                // TODO: not sure if it's good work to do
                GC.Collect();
            }
        }

        /// <summary>
        /// Gets the type wrapper corresponding to the specified type.
        /// </summary>
        /// <param name="t">The type whose wrapper is needed.</param>
        /// <param name="caller">reference to the serializer instance which called this method.</param>
        /// <returns>the type wrapper corresponding to the specified type</returns>
        public UdtWrapper GetTypeWrapper(Type t, YAXSerializer caller)
        {
            UdtWrapper result;
            if (!m_dicTypes.TryGetValue(t, out result))
            {
                result = new UdtWrapper(t, caller);
                m_dicTypes.Add(t, result);
            }
            else
            {
                result.SetYAXSerializerOptions(caller);
            }

            return result;
        }
    }
}

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 MIT License


Written By
Software Developer
Australia Australia
A software designer and developer

Comments and Discussions