Lire en anglais

Partager via


ContextInformation Classe

Définition

Encapsule les informations de contexte qui sont associées à un objet ConfigurationElement. Cette classe ne peut pas être héritée.

C#
public sealed class ContextInformation
Héritage
ContextInformation

Exemples

L’exemple de code suivant montre comment utiliser le ContextInformation type .

C#
#region Using directives

using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Web;
using System.Web.Configuration;

#endregion

namespace Samples.Aspnet.ConfigurationSample
{
  class UsingContextInformation
  {
    static void Main(string[] args)
    {
      try
      {
        // Set the path of the config file.
        string configPath = "";

        // Get the Web application configuration object.
        Configuration config = WebConfigurationManager.OpenWebConfiguration(configPath);

        // Get the section related object.
        HealthMonitoringSection configSection =
          (HealthMonitoringSection)config.GetSection("system.web/healthMonitoring");

        // Display title and info.
        Console.WriteLine("ASP.NET Configuration Info");
        Console.WriteLine();

        // Display Config details.
        Console.WriteLine("File Path: {0}",
          config.FilePath);
        Console.WriteLine("Section Path: {0}",
          configSection.SectionInformation.Name);

          // IsMachineLevel property.
          Console.WriteLine("IsMachineLevel: {0}",
            config.EvaluationContext.IsMachineLevel);

          // Create an object based on HostingContext.
          WebContext myWC =
            (WebContext)config.EvaluationContext.HostingContext;
          // Use the WebContext object to determine
          // the ApplicationLevel.
          Console.WriteLine("ApplicationLevel: {0}",
            myWC.ApplicationLevel);
        }

      catch (Exception e)
      {
        // Error.
        Console.WriteLine(e.ToString());
      }

      // Display and wait.
      Console.ReadLine();
    }
  }
}

Remarques

L’objet ContextInformation fournit des détails d’environnement liés à un élément de la configuration. Par instance, vous pouvez utiliser la IsMachineLevel propriété pour déterminer si un ConfigurationElement a été défini dans Machine.config, ou vous pouvez déterminer à quelle hiérarchie appartient un ConfigurationElement à l’aide de la HostingContext propriété .

Propriétés

HostingContext

Obtient le contexte de l'environnement dans lequel la propriété de configuration est évaluée.

IsMachineLevel

Obtient une valeur spécifiant si la propriété de configuration est évaluée au niveau de la configuration de l'ordinateur.

Méthodes

Equals(Object)

Détermine si l'objet spécifié est égal à l'objet actuel.

(Hérité de Object)
GetHashCode()

Fait office de fonction de hachage par défaut.

(Hérité de Object)
GetSection(String)

Fournit un objet contenant des informations de section de configuration en fonction du nom de section spécifié.

GetType()

Obtient le Type de l'instance actuelle.

(Hérité de Object)
MemberwiseClone()

Crée une copie superficielle du Object actuel.

(Hérité de Object)
ToString()

Retourne une chaîne qui représente l'objet actuel.

(Hérité de Object)

S’applique à

Produit Versions
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9