Click here to Skip to main content
15,893,668 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.

// A ITypeContainer for Binding.
// It is needed because of the constructor parameter dataMember (the equivalent field has a different type).


using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Windows.Forms;
#if DEBUG
using System.Diagnostics;
#endif

namespace UniversalSerializerLib2
{

	internal class BindingContainer : ITypeContainer
	{
		#region Store
		public string propertyName;
		public object dataSource;
		public string dataMember;
		public bool formattingEnabled;
		public DataSourceUpdateMode dataSourceUpdateMode;
		public object nullValue;
		public string FormatString;
		public IFormatProvider formatInfo;
		#endregion Store

		public ITypeContainer CreateNewContainer(object ContainedObject)
		{
			var c = new BindingContainer();
			Binding b = (Binding)ContainedObject;
			c.propertyName = b.PropertyName;
			c.dataSource = b.DataSource;
			c.dataMember = b.BindingMemberInfo.BindingMember; // Indirect.
			c.formattingEnabled = b.FormattingEnabled;
			c.dataSourceUpdateMode = b.DataSourceUpdateMode;
			c.nullValue = b.NullValue;
			c.FormatString = b.FormatString;
			c.formatInfo = b.FormatInfo;
			
			return c;
		}

		static readonly Type _tBinding = typeof(Binding);
		static readonly ConstructorInfo constructor = _tBinding.GetConstructor(
			BindingFlags.Public | BindingFlags.Instance, null,
			new Type[] { 
					typeof(String), // propertyName
					typeof(Object), // dataSource
					typeof(string), // dataMember
					typeof(bool), // formattingEnabled
					typeof(DataSourceUpdateMode), // dataSourceUpdateMode
					typeof(Object), // nullValue
					typeof(string), // formatString
					typeof(IFormatProvider) // formatInfo
			},
				null);

		public object Deserialize()
		{
			object bc = constructor.Invoke(new object[] { 
				this.propertyName,
				this.dataSource,
				this.dataMember,
				this.formattingEnabled,
				this.dataSourceUpdateMode,
				this.nullValue,
				this.FormatString,
				this.formatInfo
			});
			return bc;
		}

		public bool IsValidType(Type type)
		{
			return Tools.TypeIs(type, _tBinding);
		}

		public bool ApplyEvenIfThereIsANoParamConstructor
		{
			get { return true; }
		}

		public bool ApplyToStructures
		{
			get { return false; }
		}


		public bool ApplyEvenIfThereIsAValidConstructor
		{
			get { return false; }
		}
	}

}

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