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

UniversalSerializer

Rate me:
Please Sign up or sign in to vote.
4.97/5 (108 votes)
15 Apr 2018Ms-RL31 min read 262.2K   4K   299  
An universal and easy serialization library for .NET and .NET Core.

// Copyright Christophe Bertrand.

using System;
using System.IO;

namespace UniversalSerializerLib2
{

	/// <summary>
	/// Serializations methods for WPF.
	/// </summary>
	public class UniversalSerializerWPF : UniversalSerializer
	{

		static readonly CustomModifiers InternalParametersWPF = new CustomModifiers()
		{
			Containers = new ITypeContainer[] { 
				new XAMLValueSerializerContainer()
                 },

			FilterSets = new FilterSet[0]
		};

		// - - - -

		/// <summary>
		/// Prepare a serializer and deserializer following your parameters and modifiers.
		/// </summary>
		/// <param name="stream">The stream that stores, or will store, the serialized data.</param>
		public UniversalSerializerWPF(
			Stream stream)
			: base(CheckParameters(new Parameters() { Stream = stream }))
		{
		}

		// - - - -

		/// <summary>
		/// Prepare a serializer and deserializer following your parameters and modifiers.
		/// Parameters.Stream must be defined.
		/// </summary>
		/// <param name="parameters"></param>
		public UniversalSerializerWPF(
			Parameters parameters)
			: base(CheckParameters(parameters))
		{
		}

		// - - - -

		/// <summary>
		/// Prepare a serializer and deserializer that work on a file.
		/// Do not forget to call Dispose() when you release UniversalSerializer, or to write using(). Otherwize an IO exception could occure when trying to access the file for the second time, saying the file can not be open (in fact the file would have not be closed by this first instance of UniversalSerializer).
		/// </summary>
		/// <param name="FileName">The name of the file that will be open or created.</param>
		public UniversalSerializerWPF(
			string FileName)
			: this(new Parameters() { Stream = new FileStream(FileName, FileMode.OpenOrCreate, FileAccess.ReadWrite) })
		{
			this.FileStreamCreatedByConstructorOnly = (FileStream)this.parameters.Stream; // For Dispose().
		}

		// - - - -

		static Parameters CheckParameters(Parameters parameters)
		{
			if (parameters.customModifiers != null)
				parameters.customModifiers = UniversalSerializer.CombineTwoCustomParameters(parameters.customModifiers, InternalParametersWPF);
			else
				parameters.customModifiers = InternalParametersWPF;
			return parameters;
		}

	}


	// ####################################################################################

}

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 Microsoft Reciprocal License


Written By
Software Developer (Senior) independent
France France
Hi !

I made my first program on a Sinclair ZX-81.
Since that day, I have the virus of computers. Smile | :)

Here is my website:
https://chrisbertrand.net

And my blog:
https://chrisbertrandprogramer.wordpress.com/

Comments and Discussions