Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
356 views
in Technique[技术] by (71.8m points)

appsettings - How do I supply extra info to IApplicationSettingsProvider class?

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

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. I want to use .NETs built in settings system and avoid writing one from scratch
  2. The host application will be responsible for locating the plugin settings and passing it to the plugin
  3. Each plugin will be responsible for reading and writing its own settings to separate concerns. The host application should call Plugin.Save(thePath) and it does its thing.
  4. All settings are user scoped

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I found an answer albeit a bit convoluted and requires going deep into the architecture of .NET, which isn't well documented. Although ApplicationSettingsBase and IApplicationSettingsProvider are decoupled, there is a bit of recoupling involved to make this work. The solution involves modifying the Settings or your own customized version like so:

[SettingsProvider(typeof(CustomSettingProviders.MySettingsProvider))]
internal sealed partial class Settings {

    public Settings(string name)
    {
         this.Context.Add("Name", name);
    }

Alternatively, you can get around making changes to this class by setting the Context before it's used like so:

        settings.Context.Add("Name", "hello");

In the settings SetPropertyValues of the MySettingsProvider, you can actually grab the data and do something with it:

    public override void SetPropertyValues(SettingsContext context, SettingsPropertyValueCollection propvals)
    {
        if (context.Contains("Name"))
            ApplicationName = context["Name"].ToString();

To use the settings, simply instantiate the class with the parametrized constructor, or alternatively set the Context before using it:

        var settings = new Properties.Settings("Hello") { Setting1 = "Hello, is anyone home!" }
        // alternative: settings.Context.Add("Name", "hello");
        settings.Save();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...