context: //
  // System.Runtime.Remoting.Contexts.Context..cs
  //
  // Author:
  //   Miguel de Icaza (miguel@ximian.com)
  //   Lluis Sanchez Gual (lluis@ideary.com)
  //   Patrik Torstensson
  //
  // (C) Ximian, Inc.  http://www.ximian.com
  //
  
  //
  // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  //
  // 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.
  //
  
  using System.Collections;
  using System.Collections.Generic;
  using System.Threading;
  using System.Runtime.Remoting;
  using System.Runtime.Remoting.Proxies;
  using System.Runtime.Remoting.Activation;
  using System.Runtime.Remoting.Messaging;
  using System.Runtime.Remoting.Lifetime;
  using System.Runtime.InteropServices;
  
  
  namespace System.Runtime.Remoting.Contexts {
  
  	[System.Runtime.InteropServices.ComVisible (true)]
  	[StructLayout (LayoutKind.Sequential)]
  	public class Context 
  	{
  #pragma warning disable 169, 414
  		#region Sync with domain-internals.h
  		int domain_id;
  		int context_id;
  		UIntPtr static_data; /* GC-tracked */
  		#endregion
  #pragma warning restore 169, 414
  
  		// Default server context sink chain
  		static IMessageSink default_server_context_sink;
  
  		// The sink chain that has to be used by all calls entering the context
  		IMessageSink server_context_sink_chain = null;
  
  		// The sink chain that has to be used by all calls exiting the context
  		IMessageSink client_context_sink_chain = null;
  
  		object[] datastore;
  		List<IContextProperty> context_properties;
  //		bool frozen;
  		
  		static int global_count;
  
  		/* Wrap this in a nested class so its not constructed during shutdown */
  		class NamedSlots {
  			public static Hashtable namedSlots = new Hashtable ();
  		}
  
  		static DynamicPropertyCollection global_dynamic_properties;
  		DynamicPropertyCollection context_dynamic_properties;
  		ContextCallbackObject callback_object = null;
  		
  		public Context ()
  		{
  			domain_id = Thread.GetDomainID();
  			context_id = 1 + global_count++;
  		}
  
  		~Context ()
  		{
  		}
  
  		public static Context DefaultContext {
  			get {
  				return AppDomain.InternalGetDefaultContext ();
  			}
  		}
  
  		public virtual int ContextID {
  			get {
  				return context_id;
  			}
  		}
  
  		public virtual IContextProperty[] ContextProperties
  		{
  			get 
  			{
  				if (context_properties == null)
  					return new IContextProperty[0];
  				
  				return context_properties.ToArray ();
  			}
  		}
  		
  		internal bool IsDefaultContext
  		{
  			get { return context_id == 0; }
  		}
  
  		internal bool NeedsContextSink
  		{
  			get {
  				return context_id != 0 || 
  					(global_dynamic_properties != null && global_dynamic_properties.HasProperties) || 
  					(context_dynamic_properties != null && context_dynamic_properties.HasProperties);
  			}
  		}
  
  		public static bool RegisterDynamicProperty(IDynamicProperty prop, ContextBoundObject obj, Context ctx)
  		{
  			DynamicPropertyCollection col = GetDynamicPropertyCollection (obj, ctx);
  			return col.RegisterDynamicProperty (prop);
  		}
  
  		public static bool UnregisterDynamicProperty(string name, ContextBoundObject obj, Context ctx)
  		{
  			DynamicPropertyCollection col = GetDynamicPropertyCollection (obj, ctx);
  			return col.UnregisterDynamicProperty (name);
  		}
  		
  		static DynamicPropertyCollection GetDynamicPropertyCollection(ContextBoundObject obj, Context ctx)
  		{
  			if (ctx == null && obj != null)
  			{
  				if (RemotingServices.IsTransparentProxy(obj))
  				{
  					RealProxy rp = RemotingServices.GetRealProxy (obj);
  					return rp.ObjectIdentity.ClientDynamicProperties;
  				}
  				else
  					return obj.ObjectIdentity.ServerDynamicProperties;
  			}
  			else if (ctx != null && obj == null)
  			{
  				if (ctx.context_dynamic_properties == null) ctx.context_dynamic_properties = new DynamicPropertyCollection ();
  				return ctx.context_dynamic_properties;
  			}
  			else if (ctx == null && obj == null)
  			{
  				if (global_dynamic_properties == null) global_dynamic_properties = new DynamicPropertyCollection ();
  				return global_dynamic_properties;
  			}
  			else
  				throw new ArgumentException ("Either obj or ctx must be null");
  		}
  		
  		internal static void NotifyGlobalDynamicSinks  (bool start, IMessage req_msg, bool client_site, bool async)
  		{
  			if (global_dynamic_properties != null && global_dynamic_properties.HasProperties) 
  				global_dynamic_properties.NotifyMessage (start, req_msg, client_site, async);
  		}
  
  		internal static bool HasGlobalDynamicSinks
  		{
  			get { return (global_dynamic_properties != null && global_dynamic_properties.HasProperties); }
  		}
  
  		internal void NotifyDynamicSinks  (bool start, IMessage req_msg, bool client_site, bool async)
  		{
  			if (context_dynamic_properties != null && context_dynamic_properties.HasProperties) 
  				context_dynamic_properties.NotifyMessage (start, req_msg, client_site, async);
  		}
  
  		internal bool HasDynamicSinks
  		{
  			get { return (context_dynamic_properties != null && context_dynamic_properties.HasProperties); }
  		}
  
  		internal bool HasExitSinks
  		{
  			get
  			{
  				// Needs to go through the client context sink if there are custom
  				// client context or dynamic sinks.
  
  				return ( !(GetClientContextSinkChain() is ClientContextTerminatorSink) || HasDynamicSinks || HasGlobalDynamicSinks);
  			}
  		}
  
  		public virtual IContextProperty GetProperty (string name)
  		{
  			if (context_properties == null)
  				return null;
  
  			foreach (IContextProperty p in context_properties)
  				if (p.Name == name)
  					return p;
  			
  			return null;
  		}
  
  		public virtual void SetProperty (IContextProperty prop)
  		{
  			if (prop == null)
  				throw new ArgumentNullException ("IContextProperty");
  			if (this == DefaultContext)
  				throw new InvalidOperationException ("Can not add properties to " +
  								     "default context");
  //			if (frozen)
  //				throw new InvalidOperationException ("Context is Frozen");
  			
  			if (context_properties == null)
  				context_properties = new List<IContextProperty> ();
  
  			context_properties.Add (prop);
  		}
  
  		public virtual void Freeze ()
  		{
  
gt: 			if (context_properties != null)