The Labs \ Source Viewer \ SSCLI \ System.Configuration \ ContextInformation

  1. //------------------------------------------------------------------------------
  2. // <copyright file="ContextInformation.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. using System;
  16. namespace System.Configuration
  17. {
  18.    
  19.     // ContextInformation
  20.     //
  21.     // Class that encapsulates all of the Context Information that
  22.     // is going to be associated with a ConfigurationElement
  23.     //
  24.     public sealed class ContextInformation
  25.     {
  26.         private bool _hostingContextEvaluated;
  27.         // Do we know the Context?
  28.         private object _hostingContext;
  29.         // HostingContext
  30.         private BaseConfigurationRecord _configRecord;
  31.        
  32.         // Constructor
  33.         //
  34.         //
  35.         internal ContextInformation(BaseConfigurationRecord configRecord)
  36.         {
  37.             Debug.Assert(configRecord != null, "configRecord != null");
  38.            
  39.             _hostingContextEvaluated = false;
  40.             _hostingContext = null;
  41.             _configRecord = configRecord;
  42.         }
  43.        
  44.         // HostingContext
  45.         //
  46.         // Retrieve the Context of the environment that we are being
  47.         // evaluated in. (currently this can we WebContext, ExeContext,
  48.         // or null)
  49.         //
  50.         public object HostingContext {
  51.             get {
  52.                 if (!_hostingContextEvaluated) {
  53.                     // Retrieve Context
  54.                     _hostingContext = _configRecord.ConfigContext;
  55.                    
  56.                     _hostingContextEvaluated = true;
  57.                 }
  58.                
  59.                 return _hostingContext;
  60.             }
  61.         }
  62.        
  63.         // IsMachineLevel
  64.         //
  65.         // Is this the machine.config file or not? If it is not
  66.         // then use the Hosting Context to determine where you are
  67.         // and in what hierarchy you are in
  68.         //
  69.         public bool IsMachineLevel {
  70.             get { return _configRecord.IsMachineConfig; }
  71.         }
  72.        
  73.         // GetSection
  74.         //
  75.         // Get a Section within the context of where we are. What
  76.         // ever section you retrieve here will be at the same level
  77.         // in the hierarchy as we are.
  78.         //
  79.         // Note: Watch out for a situation where you request a section
  80.         // that will call you.
  81.         //
  82.         public object GetSection(string sectionName)
  83.         {
  84.             return _configRecord.GetSection(sectionName);
  85.         }
  86.     }
  87. }

Developer Fusion