无法让自定义配置部分在 app.config 中工作

Cannot get custom configuration section to work in app.config

我按照 Whosebug 上的教程将自定义配置部分添加到我的 exe 的配置文件中,但是在调用它时,它返回 null。它甚至没有进入静态构造函数,所以显然有问题,但我看不出是什么。

这是我的配置文件和我希望找到的部分。

<configuration>
  <appSettings>
  </appSettings>
      <configSections>
           <section name="PresetFilters" type="ImageTool.PresetFiltersConfiguration, ImageTool" />
      </configSections>
  <PresetFilters>
    <add key="Default,-20,0,0,0,0" />
    <add key="No Change,0,0,0,0,0" />
    <add key="Dark Photo,10,10,0,0,-10" />
  </PresetFilters>
</configuration>

我这样称呼它:

 PresetFiltersConfiguration pf = (PresetFiltersConfiguration)ConfigurationManager.GetSection("PresetFilters");

它 returns 为空,甚至没有输入我的 class 或 class 静态数据。这是代码。任何帮助,将不胜感激。谢谢

public class PresetFiltersConfiguration : ConfigurationSection
{
    private static ConfigurationPropertyCollection properties;
    private static ConfigurationProperty propPresets;

    static PresetFiltersConfiguration()
    {
        propPresets = new ConfigurationProperty(null, typeof(PresetFiltersElementCollection),
                                                      null,
                                                      ConfigurationPropertyOptions.IsDefaultCollection);
        properties = new ConfigurationPropertyCollection { propPresets };
    }

    protected override ConfigurationPropertyCollection Properties
    {
        get
        {
            return properties;
        }
    }

    public PresetFiltersElementCollection PresetFilter
    {
        get
        {
            return this[propPresets] as PresetFiltersElementCollection;
        }
    }
}

public class PresetFiltersElementCollection : ConfigurationElementCollection
{
    public PresetFiltersElementCollection()
    {
        properties = new ConfigurationPropertyCollection();
    }

    private static ConfigurationPropertyCollection properties;

    protected override ConfigurationPropertyCollection Properties
    {
        get
        {
            return properties;
        }
    }

    public override ConfigurationElementCollectionType CollectionType
    {
        get
        {
            return ConfigurationElementCollectionType.BasicMap;
        }
    }

    protected override string ElementName
    {
        get
        {
            return "add";
        }
    }

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

    protected override object GetElementKey(ConfigurationElement element)
    {
        var elm = element as PresetFiltersElement;
        if (elm == null) throw new ArgumentNullException();
        return elm.KeyName;
    }
}

public class PresetFiltersElement : ConfigurationElement
{
    private static ConfigurationPropertyCollection properties;
    private static ConfigurationProperty propKey;

    protected override ConfigurationPropertyCollection Properties
    {
        get
        {
            return properties;
        }
    }

    public PresetFiltersElement()
    {
        propKey = new ConfigurationProperty("key", typeof(string),
                                                      null,
                                                      ConfigurationPropertyOptions.IsKey);
        properties = new ConfigurationPropertyCollection { propKey };
    }

    public PresetFiltersElement(string keyName)
        : this()
    {
        KeyName = keyName;
    }

    public string KeyName
    {
        get
        {
            return this[propKey] as string;
        }
        set
        {
            this[propKey] = value;
        }
    }
}

您的 app.config 中需要这样的东西,否则应用程序将不知道如何处理您的新部分。

  <configSections>
    <sectionGroup name="pageAppearanceGroup">
      <section 
        name="PresetFilters" 
        type="PresetFiltersConfiguration" 
        allowLocation="true" 
        allowDefinition="Everywhere"
      />
    </sectionGroup>
  </configSections>

应用修复程序后它起作用了,但实际的错误是它不适用于 class 库,我为此创建了一个新问题。

谢谢。