Moving a custom configuration group to a separate file

陌路散爱 提交于 2019-11-26 22:57:45

问题


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>

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.


回答1:


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>

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>

Marc



来源:https://stackoverflow.com/questions/1562679/moving-a-custom-configuration-group-to-a-separate-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!