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

// Copyright Christophe Bertrand.

// A ITypeContainer for BindToObject.
// 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 UniversalSerializerLib3
{

	internal class BindToObjectContainer : ITypeContainer
	{
		#region Store
		public Binding owner;
		public object dataSource;
		public string dataMember;
		#endregion Store

		public ITypeContainer CreateNewContainer(object ContainedObject)
		{
			var c = new BindToObjectContainer();
			c.owner = (Binding) ownerFI.GetValue(ContainedObject);
			c.dataSource = dataSourceFI.GetValue(ContainedObject);
			c.dataMember = ((BindingMemberInfo)dataMemberFI.GetValue(ContainedObject)).BindingMember;
			
			return c;
		}

		static readonly Type _tBindToObject = Tools.GetTypeFromFullName("System.Windows.Forms.BindToObject");
		static readonly FieldInfo ownerFI = _tBindToObject.GetField("owner", BindingFlags.Instance | BindingFlags.NonPublic);
		static readonly FieldInfo dataSourceFI = _tBindToObject.GetField("dataSource", BindingFlags.Instance | BindingFlags.NonPublic);
		static readonly FieldInfo dataMemberFI = _tBindToObject.GetField("dataMember", BindingFlags.Instance | BindingFlags.NonPublic);
		static readonly ConstructorInfo constructor = _tBindToObject.GetConstructor(
			BindingFlags.NonPublic | BindingFlags.Instance, null,
			new Type[] { typeof(Binding), typeof(Object), typeof(String) }, null);

		public object Deserialize()
		{
			object bc = constructor.Invoke(new object[] { this.owner, this.dataSource, this.dataMember });
			return bc;
		}

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

		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