ConfigurationBuilder 类

定义

表示将由自定义配置生成器实现扩展的基类。Represents the base class to be extended by custom configuration builder implementations.

public ref class ConfigurationBuilder abstract : System::Configuration::Provider::ProviderBase
public abstract class ConfigurationBuilder : System.Configuration.Provider.ProviderBase
type ConfigurationBuilder = class
    inherit ProviderBase
Public MustInherit Class ConfigurationBuilder
Inherits ProviderBase
继承
ConfigurationBuilder

示例

下面的示例演示如何实现一个简单的 ConfigurationBuilder 来读取环境变量:The following example shows how to implement a simple ConfigurationBuilder to read Environment variables:

using System;
using System.Configuration;
using System.Xml;

namespace Samples.AspNet.Config
{

   public class SampleConfigurationBuilder : ConfigurationBuilder
    {

        public override XmlNode ProcessRawXml(XmlNode rawXml) 
        {

            string rawXmlString = rawXml.OuterXml;

            if (String.IsNullOrEmpty(rawXmlString)) {
                return rawXml;
            }

            rawXmlString = Environment.ExpandEnvironmentVariables(rawXmlString);

            XmlDocument doc = new XmlDocument();
            doc.PreserveWhitespace = true;
            doc.LoadXml(rawXmlString);
            return doc.DocumentElement;

        }

        public override ConfigurationSection ProcessConfigurationSection(ConfigurationSection configSection) 
            => configSection;

    }

}
Imports System.Configuration
Imports System.Xml

Public Class SampleConfigurationBuilder : Inherits ConfigurationBuilder

    Public Overrides Function ProcessRawXml(rawXml As XmlNode)  As XmlNode

        Dim rawXmlString As String = rawXml.OuterXml

        If String.IsNullOrEmpty(rawXmlString) Then
            Return rawXml
        End If

        rawXmlString = Environment.ExpandEnvironmentVariables(rawXmlString)

        Dim doc As New XmlDocument()
        doc.PreserveWhitespace = True
        doc.LoadXml(rawXmlString)
        Return doc.DocumentElement
    End Function

    Public Overrides Function ProcessConfigurationSection(configSection As ConfigurationSection)  As ConfigurationSection
       Return configSection
    End Function

End Class

下面的示例是配置文件的摘录, 因为它适用于前面的示例。The following example is an excerpt of the configuration file as it applies to the previous example. 这会将环境变量应用于 appSettings 配置, 并使这些值ConfigurationManager.AppSettings在下可用。This applies environment variables to the appSettings configuration and makes those values available under ConfigurationManager.AppSettings.

<!-- To declare and use Configuration Builders in your configuration chain, update your app.config or web.config file as follows:  -->

<configSections>
  <section name="configBuilders" type="System.Configuration.ConfigurationBuildersSection, System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" restartOnExternalChanges="false" requirePermission="false"/>
</configSections>

<configBuilders>
  <builders>
    <add name="SampleConfigurationBuilder" type="CustomConfigBuilders.MyConfigBuilder, CustomConfigBuilders" />
  </builders>
</configBuilders>

<!-- To apply Configuration Builders to a configuration section, use the 'configBuilders' tag as follows:  -->
<appSettings configBuilders="SampleConfigurationBuilder">
  <add key="COMPUTERNAME" value="Will Be Replaced by EnvironmentVariable" />
</appSettings>

注解

从此类派生, 使用标准ConfigurationManager API 从外部源中读取要在 .NET Framework 应用程序中使用的配置。Derive from this class to read configuration from an external source that you would like to consume in your .NET Framework application using the standard ConfigurationManager API. 状态在 NuGet.org 上提供, 可从环境变量、Azure 密钥保管库和其他一些源进行读取。ConfigurationBuilders are available on NuGet.org to read from environment variables, Azure key vault, and a number of other sources.

NuGet.org 中提供了多个状态实现:Several implementations of ConfigurationBuilders are available from NuGet.org:

构造函数

ConfigurationBuilder()

初始化 ConfigurationBuilder 类的新实例。Initializes a new instance of the ConfigurationBuilder class.

属性

Description

获取一条简短的易懂描述,它适合在管理工具或其他用户界面 (UI) 中显示。Gets a brief, friendly description suitable for display in administrative tools or other user interfaces (UIs).

(继承自 ProviderBase)
Name

获得一个友好名称,用于在配置过程中引用提供程序。Gets the friendly name used to refer to the provider during configuration.

(继承自 ProviderBase)

方法

Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(继承自 Object)
GetHashCode()

作为默认哈希函数。Serves as the default hash function.

(继承自 Object)
GetType()

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)
Initialize(String, NameValueCollection)

初始化配置生成器。Initializes the configuration builder.

(继承自 ProviderBase)
MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)
ProcessConfigurationSection(ConfigurationSection)

接受配置系统中的 ConfigurationSection 对象,并返回一个修改后的或新的 ConfigurationSection 对象供将来使用。Accepts a ConfigurationSection object from the configuration system and returns a modified or new ConfigurationSection object for further use.

ProcessRawXml(XmlNode)

接受表示配置文件中的原始配置节的 XmlNode,并返回修改后的或新的 XmlNode 供将来使用。Accepts an XmlNode representing the raw configuration section from a config file and returns a modified or new XmlNode for further use.

ToString()

返回表示当前对象的字符串。Returns a string that represents the current object.

(继承自 Object)

适用于