将自定义配置组移动到单独的文件

Ale*_*ril 20 .net handler custom-configuration

我最近写了一个相当大的自定义配置组.我很好奇是否可以通过以下方式将此配置移动到单独的文件中:

<configuration>
    <configSections>
        <sectionGroup name="MyCustomGroup">
            <section name="MyCustomSection"/>
        </sectionGroup>
    </configSections>
    <MyCustomGroup file="alt.config" />
</configuration>
Run Code Online (Sandbox Code Playgroud)

这与appSettings的文件属性类似.我意识到很可能需要为我的自定义部分处理程序创建一个ConfigurationPropertyAttribute,但是我在找到这方面的任何示例或方向方面都没有成功.

mar*_*c_s 33

据我所知,你不能MyCustomGroup使用configSource属性外化整个SectionGroup(即),但你必须在Section级别(即MyCustomSection)处理它

<configuration>
    <configSections>
        <sectionGroup name="MyCustomGroup">
                <section name="MyCustomSection"/>
        </sectionGroup>
    </configSections>
    <MyCustomGroup>    
       <MyCustomSection configSource="externalfile.config" />
    </MyCustomGroup>
</configuration>
Run Code Online (Sandbox Code Playgroud)

外部文件externalfile.config然后将包含您的实际的配置设置,直接与自己的自定义节标记(无领导启动<?xml....?><configuration>什么的需要):

<MyCustomSection>
    ... your settings here......
</MyCustomSection>
Run Code Online (Sandbox Code Playgroud)

  • 正是我所需要的. (2认同)