4

I have a small issue, basically I would like to achieve the following result in my config file:

<customSection>
<settings>
<setting key="" value="" />
<setting key="" value=""/>
...
</settings>
</customSection>

I've created the following code, but on the first run i cannot have it created the structure with the values that i pass!

Here is the code that initializes this configuration code and builds the default structure with values:

ConfigurationSectionManager config = new ConfigurationSectionManager("CustomSection");
config.CreateDefaultConfigurationSection("CustomSection");
Log("Get value  = " + (config.GetSection() as ConfigurationSectionManager).Settings[1].Value); //instead of [1] a key should be set ...

Now the code for the configuration section manager:

public class ConfigurationSectionManager: ConfigurationSection
{
    private const string defaultSectionName = "Default";

    private string sectionName;
    public string SectionName 
    {
        get 
        {
            if (string.IsNullOrEmpty(sectionName))
            {
                return defaultSectionName;
            }
            return sectionName;
        }
        set
        {
            sectionName = value;
        }
    }

    public SancoConfigurationSectionManager(string sectionName)
    {
        SectionName = sectionName;
    }

    public void CreateDefaultConfigurationSection(string sectionName)
    {
        ConfigurationSectionManager defaultSection = new ConfigurationSectionManager(sectionName);
        SettingsConfigurationCollection settingsCollection = new SettingsConfigurationCollection();
        settingsCollection[0] = new SettingConfigurationElement() { Key="Element", Value="Element value" };
        settingsCollection[1] = new SettingConfigurationElement() { Key = "NewElement", Value = "NeValueElement" };
        settingsCollection[2] = new SettingConfigurationElement() { Key = "NewElement2", Value = "NeValueElement2" };
        defaultSection.Settings = settingsCollection;
        CreateConfigurationSection(sectionName, defaultSection);
    }

    public void CreateConfigurationSection(string sectionName, ConfigurationSection section)
    {
        var config = ConfigurationManager.OpenExeConfiguration(null);
        if (config.Sections[SectionName] == null)
        {
            config.Sections.Add(SectionName, section);
            section.SectionInformation.ForceSave = true;
            config.Save(ConfigurationSaveMode.Full);
        }
    }

    public object GetSection()
    {
        return ConfigurationManager.GetSection(SectionName);
    }

    public override bool IsReadOnly()
    {
        return false;
    }

    public void Save()
    {
        Configuration config = ConfigurationManager.OpenExeConfiguration(null);
        ConfigurationSectionManager instance = (ConfigurationSectionManager)config.Sections[SectionName];
        instance.Settings = this.Settings;
        config.Save(ConfigurationSaveMode.Full);
    }

    [ConfigurationProperty("Settings", IsRequired = false)]
    public SettingsConfigurationCollection Settings 
    { 
        get { return this["Settings"] as SettingsConfigurationCollection; }
        set { this["Settings"] = value; }
    }

}

Now the code for the settings collection

public class SettingsConfigurationCollection : ConfigurationElementCollection
{
    public SettingConfigurationElement this[int index] 
    { 
        get 
        { 
            return BaseGet(index) as SettingConfigurationElement; 
        } 
        set 
        { 
            if (Count > index && BaseGet(index) != null) 
            { 
                BaseRemoveAt(index); 
            } 
            BaseAdd(index, value); 
        } 
    }     

    protected override ConfigurationElement CreateNewElement() 
    { 
        return new SettingConfigurationElement(); 
    }      

    protected override object GetElementKey(ConfigurationElement element) 
    { 
        return ((SettingConfigurationElement)element).Key; 
    }
}

And the code for the setting elements

public class SettingConfigurationElement : ConfigurationElement
{
    [ConfigurationProperty("key", IsRequired = true)]
    public string Key 
    { 
        get { return this["key"] as string; }
        set { this["key"] = value; }
    }

    [ConfigurationProperty("value", IsRequired = true)]
    public string Value 
    { 
        get { return this["value"] as string; }
        set { this["value"] = value; }
    }

    public override bool IsReadOnly()
    {
        return false;
    }

}

When i try to do all of this i get an error:

Unable to load type 'MyApp.Utilities.ConfigurationSectionManager, MyApp, Version=1.0.0.0, Culture=neutral, PublicKeyToken=cd112ea1ee9f48f2' because it is not public.    at System.Configuration.TypeUtil.GetConstructorWithReflectionPermission(Type type, Type baseType, Boolean throwOnError)
   at System.Configuration.MgmtConfigurationRecord.AddConfigurationSection(String group, String name, ConfigurationSection configSection)
   at System.Configuration.ConfigurationSectionCollection.Add(String name, ConfigurationSection section)

So basically i cannot create settings etc ...

Anyone has any idea how to make all of this work?

4

1 回答 1

3

Your ConfigurationSectionManager is missing the default constructor. Add it and execute.

于 2013-09-04T12:11:03.517 回答