移动自定义配置组到一个单独的文件自定义、文件

2023-09-02 23:57:03 作者:我在听你要我学的那首情歌

我最近写了一个相当大的自定义配置组。我很好奇,如果有可能该结构通过以下移动到一个单独的文件:

I've recently wrote a rather large custom configuration group. I'm curious if it is possible to move this configuration to a separate file via the following:

<configuration>
    <configSections>
    	<sectionGroup name="MyCustomGroup">
    		<section name="MyCustomSection"/>
    	</sectionGroup>
    </configSections>
    <MyCustomGroup file="alt.config" />
</configuration>

这是类似于您可以与的appSettings文件属性做一些事情。我知道有是最有可能需要建立一个ConfigurationPropertyAttribute为我的自定义节处理程序,但我已经在寻找这方面的例子或方向是不成功的。

This is something similar to what you can do with the file attribute for appSettings. I realize there is most likely a need to create a ConfigurationPropertyAttribute for my custom section handler, however I've been unsuccessful in finding any example or direction in this regard.

推荐答案

据我所知,你的不能的外部化整个SectionGroup(即 MyCustomGroup )使用 configSource 属性,但你必须处理这对科级(即 MyCustomSection

As far as I know, you cannot externalize an entire SectionGroup (i.e. MyCustomGroup) using the configSource attribute, but you have to handle this on the Section level (i.e. MyCustomSection)

<configuration>
    <configSections>
        <sectionGroup name="MyCustomGroup">
                <section name="MyCustomSection"/>
        </sectionGroup>
    </configSections>
    <MyCustomGroup>    
       <MyCustomSection configSource="externalfile.config" />
    </MyCustomGroup>
</configuration>

外部文件 externalfile.config 将包含您的实际的配置设置,直接用自己的自定义栏目标签开始(没有前导&LT ;? ?XML ....&GT; &lt;结构&GT; 或需要什么):

The external file externalfile.config would then contain your actual config settings, starting directly with your own custom section tag (no leading <?xml....?> or <configuration> or anything needed):

<MyCustomSection>
    ... your settings here......
</MyCustomSection>

马克·

 
精彩推荐
图片推荐