开发者

Specify a plugin's configuration in the main app.config

开发者 https://www.devze.com 2023-02-23 06:11 出处:网络
I\'ve got a basic plugin system, where the plugin dlls are specified in the app.config of the main exe. I want to add plugin config to the main app.config like so:

I've got a basic plugin system, where the plugin dlls are specified in the app.config of the main exe. I want to add plugin config to the main app.config like so:

<plugins>
  <processorPlugins>
    <plugin type="PluginType1, PluginAssembly1" />
    <plugin type="PluginType2, PluginAssembly2">
      <pluginConfig1 attr="..." />
      <pluginConfig2>
        <pluginOption1 />
        <pluginOption2 />
        <pluginOption3 />
        ...
    </plugin>
  &l开发者_JS百科t;/processorPlugins>
</plugins>

I can't figure out how to get this to play nicely with custom configuration sections specified at the top of the app.config.

Is there some way of loading the plugin, adding the custom section types in the plugin dll to the ConfigurationManager or configSections, then re-interpreting the plugin options using those types? Or another way of doing it?


You can do this using ConfigurationSections as you mentioned, but you need to have the config for the plugin outside of the plugin itself, in its own section..

For example

<configuration>
    <configSections>
        <sectionGroup name="PluginConfigs">
            <section name="PluginConfigs.MyPlugin" type="Plugins.MyPlugin.Config.RootConfigSection, Plugins" />
        </sectionGroup>
    </configSections>

    .......
<plugins>
    <processorPlugins>
        <plugin Type="MyPlugin, Plugins" Config="Plugins.MyPlugin" />
    </processorPlugins>
<plugins> 


<PluginConfigs>

    <PluginConfigs.MyPlugin SomeConfigValue="">        
    </PluginConfigs.MyPlugin>
</PluginConfigs>

Then in your code when you get the config section..

 public sealed class RootConfigSection : ConfigurationSection
{
    private static volatile RootConfigSection instance;
    private static volatile object syncRoot = new object();

    public static RootConfigSection Instance
    {
        get
        {
            if (instance == null)
            {
                lock (syncRoot)
                {
                    if (instance == null)
                    {
                        instance = ConfigurationManager.GetSection("PluginConfigs/PluginConfigs.MyPlugin") as RootConfigSection;
                        if (instance == null)
                        {
                            throw new ConfigurationErrorsException("You must add the PluginConfigs/PluginConfigs.MyPlugin' section to your configuration file");
                        }
                    }
                }
            }
            return instance;
        }
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号