How do I supply extra info to IApplicationSettingsProvider class?

前端 未结 2 1148
长发绾君心
长发绾君心 2021-01-21 00:55

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 ha

2条回答
  •  死守一世寂寞
    2021-01-21 01:28

    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();
    

提交回复
热议问题