Click here to Skip to main content
15,893,622 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 263K   4K   299  
An universal and easy serialization library for .NET and .NET Core.

// Copyright Christophe Bertrand.

using System;
using System.Diagnostics;
using System.Reflection;
using System.Collections.Generic;
using System.Windows.Forms;
using System.IO;

namespace UniversalSerializerLib2
{

	/// <summary>
	/// Serializations methods for WinForm.
	/// </summary>
	public class UniversalSerializerWinForm : UniversalSerializer
	{

		static readonly CustomModifiers InternalParametersWinForm = new CustomModifiers()
		{
			Containers = new ITypeContainer[] { 
				new BindingContextContainer(),
				new BindToObjectContainer(),
				new BindingContainer()
                 },

			FilterSets = new FilterSet[1] 
			 { new FilterSet() 
				 { 
					 AdditionalPrivateFieldsAdder = _AdditionalPrivateFieldsAdder, 
					}
			 },

			ForcedParametricConstructorTypes = new Type[1] { typeof(PropertyManager) }
		};

		// - - - -

		/// <summary>
		/// Prepare a serializer and deserializer following your parameters and modifiers.
		/// </summary>
		/// <param name="stream">The stream where the serialized data will be read or wrote.</param>
		public UniversalSerializerWinForm(
			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 UniversalSerializerWinForm(
			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 to be open or created.</param>
		public UniversalSerializerWinForm(
			string FileName)
			: this(new Parameters() { Stream = new FileStream(FileName, FileMode.OpenOrCreate, FileAccess.ReadWrite) })
		{
			this.FileStreamCreatedByConstructorOnly = (FileStream)this.parameters.Stream; // For Dispose().
		}

		// - - - -

#if false

		public static byte[] SerializeWinForm(object obj)
		{
			return UniversalSerializer.Serialize(obj, UniversalSerializerWinForm.InternalParametersWinForm);
		}

		public static byte[] SerializeWinForm(object obj, CustomModifiers CustomModifiers)
		{
			CustomModifiers cp =
				UniversalSerializer.CombineTwoCustomModifiers(CustomModifiers, UniversalSerializerWinForm.InternalParametersWinForm);
			return UniversalSerializer.Serialize(obj, cp);
		}

		public static T DeserializeWinForm<T>(byte[] bytes)
		{
			return (T)UniversalSerializerWinForm.DeserializeWinForm(bytes, typeof(T));
		}

		public static T DeserializeWinForm<T>(byte[] bytes, CustomModifiers CustomModifiers)
		{
			return (T)UniversalSerializerWinForm.DeserializeWinForm(bytes, typeof(T), CustomModifiers);
		}

		public static object DeserializeWinForm(byte[] bytes, Type type)
		{
			return UniversalSerializer.Deserialize(bytes, type, UniversalSerializerWinForm.InternalParametersWinForm);
		}

		public static object DeserializeWinForm(byte[] bytes, Type type, CustomModifiers CustomModifiers)
		{
			CustomModifiers cp =
				UniversalSerializer.CombineTwoCustomModifiers(CustomModifiers, UniversalSerializerWinForm.InternalParametersWinForm);
			return UniversalSerializer.Deserialize(bytes, type, cp);
		}
#endif

		// - - - -

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

		// -------------

		/// <summary>
		/// Returns a list of field names to be serialized additionnaly.
		/// Or null if no field are to be added.
		/// </summary>
		static FieldInfo[] _AdditionalPrivateFieldsAdder(Type t)
		{
			if (TBinding != null)
			{
				Type t2 = Tools.FindDerivedOrEqualToThisType(t, TBinding); // "System.Windows.Forms.Binding".
				if (t2 != null)
					return new FieldInfo[] { 
						Tools.FieldInfoFromName(t2, "bindToObject"),
						Tools.FieldInfoFromName(t2, "propertyName")
					};
			}
			return null;
		}
		static readonly Type TBinding = typeof(System.Windows.Forms.Binding);

	}
}

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