Click here to Skip to main content
15,892,674 members
Articles / Programming Languages / C#

A Fast/Compact Serialization Framework

Rate me:
Please Sign up or sign in to vote.
4.85/5 (37 votes)
13 Oct 2010GPL35 min read 283.4K   1.2K   175  
A framework for object serializiation/deserialization that is many times faster and yields a compact output.
/*

OpenNxSerialization Framework
Copyright (C) 2006 - 2007 "NeXtreme Innovations"
[The Next Xtreme Innovations]

This program is free software, distributed under the terms of
the GNU General Public License Version 2. See the "License.txt" file
at the top of the source tree.

*/
using System;
using System.Configuration;
using NeXtreme.OpenNxSerialization.Surrogates;

namespace NeXtreme.OpenNxSerialization.Configuration
{
    /// <summary>
    /// Represents a known surrogate element within a configuration file.
    /// </summary>
    public class NxKnownSurrogateElement : ConfigurationElement
    {
        private readonly TypeNameConverter msConvertor = new TypeNameConverter();

        /// <summary>
        /// Default Constructor
        /// </summary>
        public NxKnownSurrogateElement()
        {
        }

        /// <summary>
        /// Constructs the object with the given <paramref name="typeName"/>.
        /// </summary>
        /// <param name="typeName">Type name</param>
        /// <exception cref="ArgumentNullException"></exception>
        /// <remarks>
        /// The specified type must implement <see cref="INxSerializationSurrogate"/>.
        /// </remarks>
        public NxKnownSurrogateElement(string typeName)
        {
            if (typeName == null)
                throw new ArgumentNullException("typeName");
            TypeName = typeName;
        }

        /// <summary>
        /// Constructs the object with the given <paramref name="typeName"/> and <paramref name="typeHandle"/>.
        /// </summary>
        /// <param name="typeName">Type name</param>
        /// <param name="typeHandle">Type handle</param>
        /// <exception cref="ArgumentNullException"></exception>
        /// <remarks>
        /// The specified type must implement <see cref="INxSerializationSurrogate"/>.
        /// </remarks>
        public NxKnownSurrogateElement(string typeName, short typeHandle)
        {
            if (typeName == null)
                throw new ArgumentNullException("typeName");
            TypeName = typeName;
            TypeHandle = typeHandle;
        }

        /// <summary>
        /// Gets the name used to identify this collection of elements in the configuration file
        /// </summary>
        internal static string ElementName
        {
            get { return "surrogate"; }
        }

        /// <summary>
        /// Constructs the object with the given <paramref name="type"/>.
        /// </summary>
        /// <param name="type">Type</param>
        /// <exception cref="ArgumentNullException"></exception>
        /// <remarks>
        /// The specified type must implement <see cref="INxSerializationSurrogate"/>.
        /// </remarks>
        public NxKnownSurrogateElement(Type type)
        {
            if (type == null)
                throw new ArgumentNullException("type");
            TypeName = type.AssemblyQualifiedName;
        }

        /// <summary>
        /// Constructs the object with the given <paramref name="type"/> and <paramref name="typeHandle"/>.
        /// </summary>
        /// <param name="type">Type</param>
        /// <param name="typeHandle">Type handle</param>
        /// <exception cref="ArgumentNullException"></exception>
        /// <remarks>
        /// The specified type must implement <see cref="INxSerializationSurrogate"/>.
        /// </remarks>
        public NxKnownSurrogateElement(Type type, short typeHandle)
        {
            if (type == null)
                throw new ArgumentNullException("type");
            TypeName = type.AssemblyQualifiedName;
            TypeHandle = typeHandle;
        }

        /// <summary>
        /// Get/Set the typeName attribute of the surrogate element in knownSurrogates.
        /// </summary>
        /// <value>The typename for an <see cref="INxSerializationSurrogate"/> object</value>
        /// <remarks>
        /// The specified type must implement <see cref="INxSerializationSurrogate"/>. A given 
        /// surrogate must be specified only once in the list of knownSurrogates.
        /// </remarks>
        [ConfigurationProperty("typeName", IsKey = true, IsRequired = true)]
        public string TypeName
        {
            get
            {
                object value = base["typeName"];
                if (!typeof(INxSerializationSurrogate).IsAssignableFrom((Type)msConvertor.ConvertFrom(value)))
                {
                    throw new ConfigurationErrorsException(value + " is not a valid serialization surrogate");
                }
                return ((string)(value));
            }
            set
            {
                if (!typeof(INxSerializationSurrogate).IsAssignableFrom((Type) msConvertor.ConvertFrom(value)))
                {
                    throw new ConfigurationErrorsException(value + " is not a valid serialization surrogate");
                }
                base["typeName"] = value;
            }
        }

        /// <summary>
        /// Get/Set the typeHandle attribute of the surrogate element in knownSurrogates.
        /// </summary>
        /// <value>The type handle for an <see cref="INxSerializationSurrogate"/> object</value>
        /// <remarks>
        /// Must be in the range <see cref="Int16.MinValue"/> and <see cref="Int16.MaxValue"/>.
        /// </remarks>
        [ConfigurationProperty("typeHandle", IsKey = false, IsRequired = false)]
        [IntegerValidator(MinValue=Int16.MinValue, MaxValue=Int16.MaxValue)]
        public int TypeHandle
        {
            get
            {
                return ((int)(base["typeHandle"]));
            }
            set
            {
                base["typeHandle"] = value;
            }
        }
    }

    /// <summary>
    /// The class that holds onto each element returned by the configuration manager.
    /// </summary>
    public class NxKnownTypeElement : ConfigurationElement
    {
        private readonly TypeNameConverter sConvertor = new TypeNameConverter();

        /// <summary>
        /// Default Constructor
        /// </summary>
        public NxKnownTypeElement()
        {
        }

        /// <summary>
        /// Constructs the object with the given <paramref name="typeName"/>.
        /// </summary>
        /// <param name="typeName">Type name</param>
        /// <exception cref="ArgumentNullException"></exception>
        /// <remarks>
        /// A given type must be specified only once in the list of knownTypes.
        /// </remarks>
        public NxKnownTypeElement(string typeName)
        {
            if (typeName == null)
                throw new ArgumentNullException("typeName");
            TypeName = typeName;
        }

        /// <summary>
        /// Constructs the object with the given <paramref name="typeName"/> and <paramref name="typeHandle"/>.
        /// </summary>
        /// <param name="typeName">Type name</param>
        /// <param name="typeHandle">Type handle</param>
        /// <exception cref="ArgumentNullException"></exception>
        /// <remarks>
        /// A given type must be specified only once in the list of knownTypes.
        /// </remarks>
        public NxKnownTypeElement(string typeName, short typeHandle)
        {
            if (typeName == null)
                throw new ArgumentNullException("typeName");
            TypeName = typeName;
            TypeHandle = typeHandle;
        }

        /// <summary>
        /// Constructs the object with the given <paramref name="type"/>.
        /// </summary>
        /// <param name="type">Type</param>
        /// <exception cref="ArgumentNullException"></exception>
        /// <remarks>
        /// A given type must be specified only once in the list of knownTypes.
        /// </remarks>
        public NxKnownTypeElement(Type type)
        {
            if (type == null)
                throw new ArgumentNullException("type");
            TypeName = type.AssemblyQualifiedName;
        }

        /// <summary>
        /// Constructs the object with the given <paramref name="type"/> and <paramref name="typeHandle"/>.
        /// </summary>
        /// <param name="type">Type</param>
        /// <param name="typeHandle">Type handle</param>
        /// <exception cref="ArgumentNullException"></exception>
        /// <remarks>
        /// A given type must be specified only once in the list of knownTypes.
        /// </remarks>
        public NxKnownTypeElement(Type type, short typeHandle)
        {
            if (type == null)
                throw new ArgumentNullException("type");
            TypeName = type.AssemblyQualifiedName;
            TypeHandle = typeHandle;
        }

        /// <summary>
        /// Gets the name used to identify this collection of elements in the configuration file
        /// </summary>
        internal static string ElementName
        {
            get { return "type"; }
        }

        /// <summary>
        /// Get/Set the typeName attribute of the type element in knownTypes.
        /// </summary>
        /// <value>The typename for the specified type</value>
        /// <remarks>
        /// A given type must be specified only once in the list of knownTypes.
        /// </remarks>
        [ConfigurationProperty("typeName", IsKey = true, IsRequired = true)]
        public string TypeName
        {
            get
            {
                object value = base["typeName"];
                try
                {
                    sConvertor.ConvertFrom(value);
                }
                catch(Exception)
                {
                    throw new ConfigurationErrorsException(value + " is not a valid typename");
                }
                return ((string)(value));
            }
            set
            {
                try
                {
                    sConvertor.ConvertFrom(value);
                }
                catch (Exception)
                {
                    throw new ConfigurationErrorsException(value + " is not a valid typename");
                }
                base["typeName"] = value;
            }
        }

        /// <summary>
        /// Get/Set the typeHandle attribute of the type element in knownTypes.
        /// </summary>
        /// <value>The type handle for the specified type</value>
        /// <remarks>
        /// Must be in the range <see cref="Int16.MinValue"/> and <see cref="Int16.MaxValue"/>.
        /// </remarks>
        [ConfigurationProperty("typeHandle", IsKey = false, IsRequired = false)]
        [IntegerValidator(MinValue = Int16.MinValue, MaxValue = Int16.MaxValue)]
        public int TypeHandle
        {
            get
            {
                return ((int)(base["typeHandle"]));
            }
            set
            {
                base["typeHandle"] = value;
            }
        }
    }
}

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)


Written By
Architect
Pakistan Pakistan
Let a = b ....... (1)
a - b = a - b
a^2 - ab = a^2 - ab
a^2 - ab = a^2 - b^2 (from 1)
a (a - b) = (a + b) (a - b)
a = (a + b) ...... (2)

if a = 1
1 = (1 + 1) (from 1 & 2)
1 = 2 !!

Comments and Discussions