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 来读取环境变量: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;
            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
        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.AppSettingsThis 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:  -->

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

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

<!-- 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" />


从此类派生,使用标准 API 从外部源中读取要在 .NET Framework 应用程序中使用的配置 ConfigurationManagerDerive 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. 状态在 上提供,可从环境变量、Azure 密钥保管库和其他一些源进行读取。ConfigurationBuilders are available on to read from environment variables, Azure key vault, and a number of other sources. 中提供了多个状态实现:Several implementations of ConfigurationBuilders are available from



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



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

(继承自 ProviderBase)

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

(继承自 ProviderBase)



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

(继承自 Object)

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

(继承自 Object)

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

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

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

(继承自 ProviderBase)

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

(继承自 Object)

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


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


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

(继承自 Object)