【问题标题】:Cannot get custom configuration section to work in app.config无法让自定义配置部分在 app.config 中工作
【发布时间】:2018-08-24 00:18:32
【问题描述】:

我按照 Stackoverflow 上的教程将自定义配置部分添加到我的 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");

它返回 null 甚至不进入我的类或类静态。这是代码。任何帮助,将不胜感激。谢谢。

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

【问题讨论】:

  • 需要添加configSection,见msdn.microsoft.com/en-us/library/2tw134k3.aspx
  • 谢谢,这样做(作为我的编辑),仍然没有任何反应。
  • 如果我将它移到控制台应用程序中它可以正常工作,这是一个类库并且它不起作用。它正在读取 file.dll.config 文件,因为我在配置中的其他部分工作正常。

标签: c# .net configurationmanager


【解决方案1】:

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

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

【讨论】:

  • 谢谢,这样做(作为我的编辑),仍然没有任何反应。也试过一组,同样的。
  • 如果我将它移到控制台应用程序中它可以正常工作,这是一个类库并且它不起作用。它正在读取 file.dll.config 文件,因为我在配置中的其他部分工作正常。
【解决方案2】:

应用修复后它可以工作,但实际的错误是它不适用于类库,我为此创建了一个新问题。

谢谢。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-10-02
    • 1970-01-01
    • 2012-10-04
    • 2015-07-05
    • 2011-05-25
    • 2011-11-21
    • 2011-12-10
    • 2010-10-20
    相关资源
    最近更新 更多