在主 app.config 中指定插件的配置

发布于 2024-10-30 14:51:24 字数 665 浏览 1 评论 0原文

我有一个基本的插件系统,其中插件 dll 在主 exe 的 app.config 中指定。我想将插件配置添加到主 app.config 中,如下所示:

<plugins>
  <processorPlugins>
    <plugin type="PluginType1, PluginAssembly1" />
    <plugin type="PluginType2, PluginAssembly2">
      <pluginConfig1 attr="..." />
      <pluginConfig2>
        <pluginOption1 />
        <pluginOption2 />
        <pluginOption3 />
        ...
    </plugin>
  </processorPlugins>
</plugins>

我不知道如何让它与 app.config 顶部指定的自定义配置部分很好地配合。

是否有某种方法加载插件,将插件 dll 中的自定义部分类型添加到 ConfigurationManager 或 configSections,然后使用这些类型重新解释插件选项?或者另一种方法可以做到这一点?

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>
  </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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦情居士 2024-11-06 14:51:24

您可以使用您提到的 ConfigurationSections 来执行此操作,但您需要插件本身之外的插件配置,在其自己的部分中。

例如

<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>

,当您获取配置部分时,在您的代码中..

 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;
        }
    }

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;
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文