The Labs \ Source Viewer \ SSCLI \ System.ComponentModel.Design.Serialization \ ContextStack

  1. //------------------------------------------------------------------------------
  2. // <copyright file="ContextStack.cs" company="Microsoft">
  3. //
  4. // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
  5. //
  6. // The use and distribution terms for this software are contained in the file
  7. // named license.txt, which can be found in the root of this distribution.
  8. // By using this software in any fashion, you are agreeing to be bound by the
  9. // terms of this license.
  10. //
  11. // You must not remove this notice, or any other, from this software.
  12. //
  13. // </copyright>
  14. //------------------------------------------------------------------------------
  15. namespace System.ComponentModel.Design.Serialization
  16. {
  17.    
  18.     using System;
  19.     using System.Collections;
  20.     using System.Security.Permissions;
  21.    
  22.     /// <devdoc>
  23.     /// A context stack is an object that can be used by serializers
  24.     /// to push various context objects. Serialization is often
  25.     /// a deeply nested operation, involving many different
  26.     /// serialization classes. These classes often need additional
  27.     /// context information when performing serialization. As
  28.     /// an example, an object with a property named "Enabled" may have
  29.     /// a data type of System.Boolean. If a serializer is writing
  30.     /// this value to a data stream it may want to know what property
  31.     /// it is writing. It won't have this information, however, because
  32.     /// it is only instructed to write the boolean value. In this
  33.     /// case the parent serializer may push a PropertyDescriptor
  34.     /// pointing to the "Enabled" property on the context stack.
  35.     /// What objects get pushed on this stack are up to the
  36.     /// individual serializer objects.
  37.     /// </devdoc>
  38.     [HostProtection(SharedState = true)]
  39.     [System.Security.Permissions.PermissionSetAttribute(System.Security.Permissions.SecurityAction.LinkDemand, Name = "FullTrust")]
  40.     public sealed class ContextStack
  41.     {
  42.         private ArrayList contextStack;
  43.        
  44.         /// <devdoc>
  45.         /// Retrieves the current object on the stack, or null
  46.         /// if no objects have been pushed.
  47.         /// </devdoc>
  48.         public object Current {
  49.             get {
  50.                 if (contextStack != null && contextStack.Count > 0) {
  51.                     return contextStack[contextStack.Count - 1];
  52.                 }
  53.                 return null;
  54.             }
  55.         }
  56.        
  57.         /// <devdoc>
  58.         /// Retrieves the object on the stack at the given
  59.         /// level, or null if no object exists at that level.
  60.         /// </devdoc>
  61.         public object this[int level]
  62.         {
  63.             get {
  64.                 if (level < 0) {
  65.                     throw new ArgumentOutOfRangeException("level");
  66.                 }
  67.                 if (contextStack != null && level < contextStack.Count) {
  68.                     return contextStack[contextStack.Count - 1 - level];
  69.                 }
  70.                 return null;
  71.             }
  72.         }
  73.        
  74.         /// <devdoc>
  75.         /// Retrieves the first object on the stack that
  76.         /// inherits from or implements the given type, or
  77.         /// null if no object on the stack implements the type.
  78.         /// </devdoc>
  79.         public object this[Type type]
  80.         {
  81.             get {
  82.                 if (type == null) {
  83.                     throw new ArgumentNullException("type");
  84.                 }
  85.                
  86.                 if (contextStack != null) {
  87.                     int level = contextStack.Count;
  88.                     while (level > 0) {
  89.                         object value = contextStack[--level];
  90.                         if (type.IsInstanceOfType(value)) {
  91.                             return value;
  92.                         }
  93.                     }
  94.                 }
  95.                
  96.                 return null;
  97.             }
  98.         }
  99.        
  100.         /// <devdoc>
  101.         /// Appends an object to the end of the stack, rather than pushing it
  102.         /// onto the top of the stack. This method allows a serializer to communicate
  103.         /// with other serializers by adding contextual data that does not have to
  104.         /// be popped in order. There is no way to remove an object that was
  105.         /// appended to the end of the stack without popping all other objects.
  106.         /// </devdoc>
  107.         public void Append(object context)
  108.         {
  109.             if (context == null) {
  110.                 throw new ArgumentNullException("context");
  111.             }
  112.            
  113.             if (contextStack == null) {
  114.                 contextStack = new ArrayList();
  115.             }
  116.             contextStack.Insert(0, context);
  117.         }
  118.        
  119.         /// <devdoc>
  120.         /// Pops the current object off of the stack, returning
  121.         /// its value.
  122.         /// </devdoc>
  123.         public object Pop()
  124.         {
  125.             object context = null;
  126.            
  127.             if (contextStack != null && contextStack.Count > 0) {
  128.                 int idx = contextStack.Count - 1;
  129.                 context = contextStack[idx];
  130.                 contextStack.RemoveAt(idx);
  131.             }
  132.            
  133.             return context;
  134.         }
  135.        
  136.         /// <devdoc>
  137.         /// Pushes the given object onto the stack.
  138.         /// </devdoc>
  139.         public void Push(object context)
  140.         {
  141.             if (context == null) {
  142.                 throw new ArgumentNullException("context");
  143.             }
  144.            
  145.             if (contextStack == null) {
  146.                 contextStack = new ArrayList();
  147.             }
  148.             contextStack.Add(context);
  149.         }
  150.     }
  151. }

Developer Fusion