如何向 IApplicationSettingsProvider 类提供额外信息? [英] How do I supply extra info to IApplicationSettingsProvider class?

查看:50
本文介绍了如何向 IApplicationSettingsProvider 类提供额外信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

也许这个问题以前有人用不同的方式问过,但我一直找不到.

Perhaps this question has been asked before in a different way, but I haven’t been able to find it.

例如,我的应用程序中有一个或多个插件适配器程序集都具有 IPlugin 类型.每个适配器都有自己的设置结构存储在一个公共目录中.它们是存储在一个连续的文件中还是存储在单独的文件中并不重要.每个适配器都可以有一个或多个与之关联的设置.设置将具有名称和将用于的插件.
我将如何使用以下要求创建这样的配置系统:

I have one or more plugin adapter assemblies in my application all having the type IPlugin, for instance. Each adapter has its own settings structures stored in a common directory. Whether they are stored in one contiguous file or in separate ones doesn’t matter. Each adapter can have one or more settings associated with it. The settings will have both a name and the Plugin it will be used for.
How would I create such a configuration system using the following requirements:

  1. 我想使用内置的 .NET设置系统并避免写入从头开始
  2. 宿主应用程序将负责定位插件设置并将其传递给插件
  3. 每个插件都将负责阅读和写作自己的设置分开担忧.主机应用程序应该调用 Plugin.Save(thePath) 和它做自己的事.
  4. 所有设置都在用户范围内

到目前为止,我意识到我需要编写自己的 SettingsProvider,但提供程序似乎是独立工作的,因为无法向它传递参数,例如插件目录的路径和设置的名称.我见过的所有示例代码都让提供程序从运行时环境中获取数据.

So far, I realize that I would need to write my own SettingsProvider, but the provider seems to work in isolation in that there’s no way to pass it parameters such as the path of the plugin directory and the name of the settings. All of the example code I've seen has the provider getting the data from the runtime environment.

推荐答案

您示例中的插件会新建一个 PluginSettings 然后像这样调用它:

The plugins in your example would new up a PluginSettings and then call into it like this:

PluginSettings["age"] = "5";
int age;
if (int.TryParse(PluginSettings["age"], out age)
{

}
else
{
}

插件设置代码:

using System.Configuration;
using System.Xml;

public sealed class PluginSettings
{
    public string this[string propertyName]
    {
        get
        {
            var store = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal);
            UserSettingsGroup values = (UserSettingsGroup)store.SectionGroups["userSettings"];
            if (values == null)
            {
                return null;
            }
            ClientSettingsSection myValues = (ClientSettingsSection)values.Sections[typeof(DebuggerSettings).FullName];
            if (myValues == null)
            {
                return null;
            }
            SettingElement setting = myValues.Settings.Get(propertyName);
            if (setting == null)
            {
                return null;
            }
            string returnValue = setting.Value.ValueXml.InnerText;
            return returnValue;
        }
        set
        {
            var store = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal);
            UserSettingsGroup addSectionGroup = (UserSettingsGroup)store.SectionGroups["userSettings"];
            if (addSectionGroup == null)
            {
                addSectionGroup = new UserSettingsGroup();
                store.SectionGroups.Add("userSettings",addSectionGroup);
            }
            string sectionName = (typeof(DebuggerSettings).FullName);
            ClientSettingsSection clientSettingsSection = (ClientSettingsSection)addSectionGroup.Sections[sectionName];
            if (clientSettingsSection == null)
            {
                clientSettingsSection = new ClientSettingsSection();
                clientSettingsSection.SectionInformation.AllowExeDefinition = ConfigurationAllowExeDefinition.MachineToLocalUser;
                addSectionGroup.Sections.Add(sectionName, clientSettingsSection);
            }
            SettingElement addMe = new SettingElement(propertyName, SettingsSerializeAs.String);
            XmlElement element = new XmlDocument().CreateElement("value");
            element.InnerText = value;
            addMe.Value.ValueXml = element;
            clientSettingsSection.Settings.Add(addMe);

            store.Save();
        }
    }
}

我遇到了同样的问题并博客.

I had the same issue and blogged about it.

这篇关于如何向 IApplicationSettingsProvider 类提供额外信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆