context: //
  // System.ComponentModel.Design.Serialization.DesignerSerializationManager
  //
  // Authors:	 
  //	  Ivan N. Zlatev (contact i-nZ.net)
  //
  // (C) 2007 Ivan N. Zlatev
  
  //
  // Permission is hereby granted, free of charge, to any person obtaining
  // a copy of this software and associated documentation files (the
  // "Software"), to deal in the Software without restriction, including
  // without limitation the rights to use, copy, modify, merge, publish,
  // distribute, sublicense, and/or sell copies of the Software, and to
  // permit persons to whom the Software is furnished to do so, subject to
  // the following conditions:
  // 
  // The above copyright notice and this permission notice shall be
  // included in all copies or substantial portions of the Software.
  // 
  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  //
  
  #if NET_2_0
  
  using System;
  using System.Collections;
  using System.Collections.Generic;
  using System.Reflection;
  using System.ComponentModel;
  using System.ComponentModel.Design;
  
  
  
  namespace System.ComponentModel.Design.Serialization
  {
  	
  	public class DesignerSerializationManager : IDesignerSerializationManager, IServiceProvider
  	{
  		
  		private class Session : IDisposable
  		{
  			
  			private DesignerSerializationManager _manager;
  			
  			public Session (DesignerSerializationManager manager)
  			{
  				_manager = manager;
  			}
  			
  			public void Dispose ()
  			{
  				_manager.OnSessionDisposed (EventArgs.Empty);
  			}
  		}
  
  		
  		
  		public DesignerSerializationManager () : this (null)
  		{
  		}
  		
  		// This constructor sets the PreserveNames and ValidateRecycledTypes properties to true.
  		//
  		public DesignerSerializationManager (IServiceProvider provider)
  		{
  			_serviceProvider = provider;
  			_preserveNames = true;
  			_validateRecycledTypes = true;
  		}
  		
  		private IServiceProvider _serviceProvider;
  		private bool _preserveNames = false;
  		private bool _validateRecycledTypes = false;
  		private bool _recycleInstances = false;
  		private IContainer _designerContainer = null;
  		private object _propertyProvider = null;
  		private Session _session = null;
  		private ArrayList _errors = null;
  		private List <IDesignerSerializationProvider> _serializationProviders;
  		private Dictionary <Type, object> _serializersCache = null; // componentType - serializer instance
  		private Dictionary <string, object> _instancesByNameCache = null; // name - instance
  		private Dictionary <object, string> _instancesByValueCache = null; // instance - name
  		private ContextStack _contextStack = null;
  		
  		
  		public bool RecycleInstances {
  			get { return _recycleInstances; }
  			set {
  				VerifyNotInSession ();
  				_recycleInstances = value; 
  			}
  		}
  		
  		public bool PreserveNames {
  			get { return _preserveNames; }
  			set {
  				VerifyNotInSession ();
  				_preserveNames = value; 
  			}
  		}
  		
  		public bool ValidateRecycledTypes {
  			get { return _validateRecycledTypes; }
  			set {
  				VerifyNotInSession ();
  				_validateRecycledTypes = value; 
  			}
  		}
  		
  		public IContainer Container { 
  			get {
  				if (_designerContainer == null) {
  					_designerContainer = (this.GetService (typeof (IDesignerHost)) as IDesignerHost).Container;
  				}
  				return _designerContainer;
  			}
  			set{
  				VerifyNotInSession ();
  				_designerContainer = value;
  			}
  		}
  		
  		public object PropertyProvider {
  			get { return _propertyProvider; }
  			set { _propertyProvider = value; }
  		}
  		
  		public IList Errors {
  			get { return _errors; }
  		}
  		
  		public event EventHandler SessionDisposed;
  		public event EventHandler SessionCreated;
  		
  		protected virtual void OnSessionCreated (EventArgs e)
  		{
  			if (SessionCreated != null) {
  				SessionCreated (this, e);
  			}
  		}
  			
  		// For behaviour description:
  		//
  		// http://msdn2.microsoft.com/en-us/library/system.componentmodel.design.serialization.designerserializationmanager.validaterecycledtypes.aspx
  		// http://msdn2.microsoft.com/en-us/library/system.componentmodel.design.serialization.designerserializationmanager.preservenames.aspx
  		//
  		protected virtual object CreateInstance (Type type, ICollection arguments, string name, bool addToContainer)
  		{
  			VerifyInSession ();
  			object instance = null;
  
  			if (name != null && _recycleInstances) {
  				_instancesByNameCache.TryGetValue (name, out instance);
  				if (instance != null && _validateRecycledTypes) {
  					if (instance.GetType () != type)
  						instance = null;
  				}
  			}
  			
  			if (instance == null || !_recycleInstances)
  				instance = this.CreateInstance (type, arguments);
  		
  			if (addToContainer && instance != null && this.Container != null && typeof (IComponent).IsAssignableFrom (type)) {
  				if (_preserveNames) {
  					this.Container.Add ((IComponent) instance, name);
  				}
  				else {
  					if (name != null && this.Container.Components[name] != null) {
  						this.Container.Add ((IComponent) instance);
  					}
  					else {
  						this.Container.Add ((IComponent) instance, name);
  					}
  				}
  				ISite site = ((IComponent)instance).Site; // get the name from the site in case a name has been generated.
  				if (site != null)
  					name = site.Name;
  			}
  			
  			if (instance != null && name != null) {
  				_instancesByNameCache[name] = instance;
  				_instancesByValueCache[instance] = name;
  			}
  			
  			return instance;
  		}
  
  		// Invokes the constructor that matches the arguments
  		//
  		private object CreateInstance (Type type, ICollection argsCollection)
  		{
  			object instance = null;
  			object[] arguments = null;
  			Type[] types = new Type[0];
  
  			if (argsCollection != null) {
  				arguments = new object[argsCollection.Count];
  				types = new Type[argsCollection.Count];
  				argsCollection.CopyTo (arguments, 0);
  
  				for (int i=0; i < arguments.Length; i++) {
  					if (arguments[i] == null)
  						types[i] = null;
  					else
  						types[i] = arguments[i].GetType ();
  				}
  			}
  
  			ConstructorInfo ctor = type.GetConstructor (types);
  			if (ctor != null) {
  				instance = ctor.Invoke (arguments);
  			}
  
  			return instance;
  		}
  
  		public object GetSerializer (Type componentType, Type serializerType)
  		{
  			VerifyInSession ();
  			
  			if (serializerType == null)
  				throw new ArgumentNullException ("serializerType");
  				
  			object serializer = null;
  
  			if (componentType != null) {
  				// try 1: from cache
  				//
  				_serializersCache.TryGetValue (componentType, out serializer);
  
  				// check for provider attribute and add it to the list of providers
  				//
  				if (serializer != null && !serializerType.IsAssignableFrom (serializer.GetType ()))
  					serializer = null;
  				
  				AttributeCollection attributes = TypeDescriptor.GetAttributes (componentType);
  				DefaultSerializationProviderAttribute providerAttribute = attributes[typeof (DefaultSerializationProviderAttribute)] 
  																			   as DefaultSerializationProviderAttribute;
  				if (providerAttribute != null && this.GetType (providerAttribute.ProviderTypeName) == serializerType) {
  					object provider = Activator.CreateInstance (this.GetType (providerAttribute.ProviderTypeName), 
  																 BindingFlags.CreateInstance | BindingFlags.Public | BindingFlags.NonPublic, 
  																 null, null, null);
  					((IDesignerSerializationManager)this).AddSerializationProvider ((IDesignerSerializationProvider) provider);
  				}
  			}
  
  			// try 2: DesignerSerializerAttribute
  			//
  			if (serializer == null && componentType != null) {
  				AttributeCollection attributes = TypeDescriptor.GetAttributes (componentType);
  				DesignerSerializerAttribute serializerAttribute = attributes[typeof (DesignerSerializerAttribute)] as DesignerSerializerAttribute;
  				if (serializerAttribute != null && 
  					this.GetType (serializerAttribute.SerializerBaseTypeName) == serializerType) {
  					try {
  						serializer = Activator.CreateInstance (this.GetType (serializerAttribute.SerializerTypeName), 
  										       BindingFlags.CreateInstance | BindingFlags.Instance | 
  										       BindingFlags.Public | BindingFlags.NonPublic, 
  										       null, null, null);
  					} catch {}
  				}
  				
  				if (serializer != null)
  					_serializersCache[componentType] = serializer;
  			}
  
  			// try 3: from provider
  			//
  			if (serializer == null && _serializationProviders != null) {
  				foreach (IDesignerSerializationProvider provider in _serializationProviders) {
  					serializer = provider.GetSerializer (this, null, componentType, serializerType);
  					if (serializer != null)
  						break;
  				}
  			}
  
  			return serializer;
  		}
  
  		private void VerifyInSession ()
  		{
  			if (_session == null)
  				throw new InvalidOperationException ("Not in session.");
  		}
  		
  		private void VerifyNotInSession ()
  		{
  
gt: 			if (_session != null)