How do I supply extra info to IApplicationSettingsProvider class?

前端 未结 2 1118
长发绾君心
长发绾君心 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:34

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

    Code for PluginSettings:

    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.

提交回复
热议问题