app.config 中的自定义配置部分 -> 无法识别的元素“添加”

时间:2021-07-07 20:53:15

标签: c# .net app-config

因此,我正在尝试为我的 app.config 创建一个自定义配置部分,它将处理以下模式:

  <domainSolutionSection>
    <domainSolutionGroups>
      <Group groupname="Legacy">
        <add name="Old App #1"/>
        <add name="Old App #2" />
      </Group>
      <Group groupname="Modern">
        <add name="New App #1" />
        <add name="New App #2" />
        <add name="New App #3" />
      </Group>
    </domainSolutionGroups>
  </domainSolutionSection>

我花了一整天的时间试图让它发挥作用。
现在,我收到一个错误:

System.Configuration.ConfigurationErrorsException: 无法识别的元素“add”。

以下是我的 DomainSolutionSection 库中的类:

public class DomainSolutionSection : ConfigurationSection
{
    [ConfigurationProperty("domainSolutionGroups")]
    public DomainSolutionGroupCollection DomainSolutionGroups
    {
        get
        {
            return (DomainSolutionGroupCollection) this["domainSolutionGroups"];
        }
        set
        {
            this["domainSolutionGroups"] = value;
        }
    }
}

[ConfigurationCollection(typeof(GroupConfigElement))]
public class DomainSolutionGroupCollection : ConfigurationElementCollection
{
    protected override ConfigurationElement CreateNewElement()
    {
        return new GroupConfigElement();
    }

    protected override object GetElementKey(ConfigurationElement element)
    {
        return ((GroupConfigElement)element).GroupName;
    }

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

    protected override bool IsElementName(string elementName)
    {
        return !String.IsNullOrEmpty(elementName) && elementName == "Group";
    }

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

    public GroupConfigElement this[int index]
    {
        get
        {
            return base.BaseGet(index) as GroupConfigElement;
        }
    }

    public new GroupConfigElement this[string key]
    {
        get
        {
            return base.BaseGet(key) as GroupConfigElement;
        }
    }
}
   
public class GroupConfigElement : ConfigurationElement
{
    [ConfigurationProperty("groupname", IsRequired = true, IsKey = true)]
    public string GroupName
    {
        get { return (string)this["groupname"]; }
        set { this["groupname"] = value; }
    }

    public PhraseCollection Phrases
    {
        get { return (PhraseCollection) base["Groups"]; } 
    }
}

[ConfigurationCollection(typeof(PhraseConfigElement), AddItemName = "add", CollectionType = ConfigurationElementCollectionType.BasicMap)]
public class PhraseCollection: ConfigurationElementCollection
{
    protected override ConfigurationElement CreateNewElement()
    {
        return new PhraseConfigElement();
    }

    protected override object GetElementKey(ConfigurationElement element)
    {
        return ((PhraseConfigElement)element).Name;
    }
}

public class PhraseConfigElement: ConfigurationElement
{
    [ConfigurationProperty("name", IsRequired = true, IsKey = true)]
    public string Name
    {
        get { return (string)this["name"]; }
        set { this["name"] = value; }
    }
}

没有找到任何在此嵌套级别使用“添加”属性的示例,因此很明显,我没有正确“连接”“组”。有人可以提供任何指导吗?

谢谢!

2 个答案:

答案 0 :(得分:0)

很抱歉这不是您问题的直接答案。我没有时间分解你所做的事情,但这里有一个类似的例子,说明我过去做过的事情,并且有效。也许这里有什么可以帮忙的。

myconfig.config:

<?xml version="1.0"?>
<myconfig>
  <moduleIPAccessRules>
    <allowRules>
      <add module="ModuleA"   subnetIP="172.16.0.0"     subnetMask="255.255.0.0"      name="ModuleA intranet users" />
      <add module="ModuleA"   subnetIP="127.0.0.0"      subnetMask="255.255.0.0"      name="ModuleA local box" />

      <add module="ModuleB"     subnetIP="172.16.0.0"     subnetMask="255.255.0.0"      name="ModuleB intranet users" />
      <add module="ModuleB"     subnetIP="127.0.0.0"      subnetMask="255.255.0.0"      name="ModuleB local box" />
      <add module="ModuleB"     subnetIP="123.45.67.89"   subnetMask="255.255.255.255"  name="some remote machine" />
    </allowRules>
  </moduleIPAccessRules>
</myconfig>

支持类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;

namespace Myproject.Configuration
{
    public class IPAccessSection : ConfigurationSection
    {
        public static IPAccessSection GetConfig()
        {
            return ConfigurationManager.GetSection("myconfig/moduleIPAccessRules") as IPAccessSection;
        }

        [ConfigurationProperty("allowRules")]
        public AllowRuleCollection AllowRules
        {
            get { return this["allowRules"] as AllowRuleCollection; }
        }
    }

    public class AllowRuleCollection : ConfigurationElementCollection
    {
        public AllowRule this[int index]
        {
            get
            {
                return base.BaseGet(index) as AllowRule;
            }
            set
            {
                if (base.BaseGet(index) != null)
                    base.BaseRemoveAt(index);

                this.BaseAdd(index, value);
            }
        }

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

        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((AllowRule)element).Name;
        }
    }

    public class AllowRule : ConfigurationElement
    {
        [ConfigurationProperty("name", IsRequired = true)]
        public string Name
        {
            get { return this["name"] as string; }
        }

        [ConfigurationProperty("module", IsRequired = true)]
        public string Module
        {
            get { return this["module"] as string; }
        }

        [ConfigurationProperty("subnetIP", IsRequired = true)]
        public string SubnetIP
        {
            get { return this["subnetIP"] as string; }
        }

        [ConfigurationProperty("subnetMask", IsRequired = true)]
        public string SubnetMask
        {
            get { return this["subnetMask"] as string; }
        }
    }
}

答案 1 :(得分:0)

实际上,我大约在 10 分钟前才找到答案。基本上是我没有真正将 PhraseCollection 正确地“连接”到 GroupConfigElement ......我也没有完全充实 PhraseCollection。:

    public class GroupConfigElement : ConfigurationElement
{
    [ConfigurationProperty("groupname", IsRequired = true, IsKey = true)]
    public string GroupName
    {
        get { return (string)this["groupname"]; }
        set { this["groupname"] = value; }
    }

    [ConfigurationProperty("", IsDefaultCollection = true, IsRequired = true)]
    public PhraseCollection Phrases
    {
        get { return (PhraseCollection) base[""]; }
    }
}

[ConfigurationCollection(typeof(PhraseConfigElement))]
public class PhraseCollection: ConfigurationElementCollection
{
    protected override ConfigurationElement CreateNewElement()
    {
        return new PhraseConfigElement();
    }

    protected override object GetElementKey(ConfigurationElement element)
    {
        return ((PhraseConfigElement)element).Name;
    }

    public PhraseConfigElement this[int index]
    {
        get
        {
            return base.BaseGet(index) as PhraseConfigElement;
        }
    }

    public new PhraseConfigElement this[string key]
    {
        get
        {
            return base.BaseGet(key) as PhraseConfigElement;
        }
    }
}

public class PhraseConfigElement: ConfigurationElement
{
    [ConfigurationProperty("name", IsRequired = true, IsKey = true)]
    public string Name
    {
        get { return (string)this["name"]; }
        set { this["name"] = value; }
    }
}

}

关键修复是“短语”定义......然后我不得不将这两个公共方法添加到“短语集合”定义中。

谢谢!

相关问题