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

// Copyright Christophe Bertrand.

#if true//! SILVERLIGHT
using System;
using System.Collections.Generic;
using System.IO;
using System.Collections;

namespace UniversalSerializerLib3
{
	/// <summary>
	/// For classes inheriting Type.
	/// </summary>
	internal class CLRTypeContainer : ITypeContainer
	{

		public CLRTypeContainer()
		{ }

		public object Deserialize()
		{
			throw new NotSupportedException();
		}

		bool _IsValidType(System.Type type)
		{
			return type.Is(tType);
		}
		public bool IsValidType(System.Type type)
		{
			bool ret;
			if (!IsValidTypeCache.TryGetValue(type, out ret))
			{
				ret = this._IsValidType(type);
				IsValidTypeCache.Add(type, ret);
			}
			return ret;
		}
		static Dictionary<Type, bool> IsValidTypeCache = new Dictionary<Type, bool>();
		static Type tType = typeof(Type);

		public ITypeContainer CreateNewContainer(object ContainedObject)
		{
			if (ContainedObject == null)
				return null;

			Type type = (Type)ContainedObject;
			ITypeContainer obj = CreateATypeContainerGeneric(type);

			return obj;
		}

		static ITypeContainer CreateATypeContainerGeneric(
			Type SourceObjectType)
		{
			Type g;
			if (!GenericContainersTypeCache.TryGetValue(SourceObjectType, out g))
			{
				Type[] typeArgs = { SourceObjectType };
				g = GenericType.MakeGenericType(typeArgs);
				GenericContainersTypeCache.Add(SourceObjectType, g);
			}
			object o = Activator.CreateInstance(g, SourceObjectType.AssemblyQualifiedName);
			return o as ITypeContainer;
		}
		static Type GenericType = typeof(CLRTypeContainerGeneric<>);
		static Dictionary<Type, Type> GenericContainersTypeCache
			= new Dictionary<Type, Type>();

		public bool ApplyEvenIfThereIsAValidConstructor
		{
			get { return false; }
		}

		// ----------------------------------------------------------------------------------
		// ###########################

		/// <summary>
		/// Internal generic adaptation.
		/// For classes inheriting Type.
		/// </summary>
		internal class CLRTypeContainerGeneric<TSourceObject> : ITypeContainer
		{
			public string AssemblyQualifiedName;

			public CLRTypeContainerGeneric()
			{ }

			public CLRTypeContainerGeneric(string AssemblyQualifiedName)
			{
				this.AssemblyQualifiedName = AssemblyQualifiedName;
			}

			public object Deserialize()
			{
				return Tools.GetTypeFromFullName(this.AssemblyQualifiedName);
			}

			static string SerializeObject(object o)
			{
				throw new NotSupportedException();
			}

			public bool IsValidType(Type type)
			{
				throw new NotSupportedException();
			}

			public ITypeContainer CreateNewContainer(object ContainedObject)
			{
				throw new NotSupportedException();
			}

			public bool ApplyEvenIfThereIsAValidConstructor
			{
				get
				{
					throw new NotSupportedException();
				}
			}


			public bool ApplyToStructures
			{
				get { throw new NotImplementedException(); }
			}
		}

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

		public bool ApplyToStructures
		{
			get { return false; }
		}
	}
}
#endif

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