无法识别的属性自定义配置(再次)

时间:2014-03-24 22:39:11

标签: c# config

我正在创建自定义配置部分,但在尝试获取该部分时,我一直收到Attribute Not Recognized错误。

我很确定这是一些愚蠢的错字 - 希望有人可以发现它。

代码:

<configSections>
  <section name="ClientFilterSettings" type="ESPDB.Config.ClientFilterSettings, ESPDB"/>
</configSections>
<ClientFilterSettings>
  <AllowedIPs>
    <IPAddress IP="255.255.255.255"/>
  </AllowedIPs>
</ClientFilterSettings>

部分:

namespace ESPDB.Config
{
    public class ClientFilterSettings : ConfigurationSection
    {
        private static ClientFilterSettings _settings = 
            ConfigurationManager.GetSection(typeof(ClientFilterSettings).Name) as ClientFilterSettings
            /*?? new ClientFilterSettings()*/;

        private const string _allowedIPs = "AllowedIPs";

        public static ClientFilterSettings Settings
        {
            get { return _settings; }
        }

        [ConfigurationProperty(_allowedIPs, IsRequired = true)]
        [ConfigurationCollection(typeof(IPAddressCollection))]
        public IPAddressCollection AllowedIPs
        {
            get { return (IPAddressCollection)this[_allowedIPs]; }
            set { this[_allowedIPs] = value; }
        }
    }
}

收集:

namespace ESPDB.Config
{
    public class IPAddressCollection : ConfigurationElementCollection
    {
        protected override ConfigurationElement CreateNewElement()
        {
            return new IPAddressCollection();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return (element as IPAddressElement).IP;
        }

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

        public IPAddressElement this[int index]
        {
            get
            {
                return base.BaseGet(index) as IPAddressElement;
            }
            set
            {
                if (base.BaseGet(index) != null)
                {
                    base.BaseRemoveAt(index);
                }
                this.BaseAdd(index, value);
            }
        }

        public new IPAddressElement this[string responseString]
        {
            get { return (IPAddressElement)BaseGet(responseString); }
            set
            {
                if (BaseGet(responseString) != null)
                {
                    BaseRemoveAt(BaseIndexOf(BaseGet(responseString)));
                }
                BaseAdd(value);
            }
        }
    }
}

元素

namespace ESPDB.Config
{
    public class IPAddressElement : ConfigurationElement
    {
        private const string _ip = "IP";

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

1 个答案:

答案 0 :(得分:3)

您的代码中存在多个问题。

1)。您以递归方式创建ClientFilterSettings的对象。删除以下代码,不需要。

private static ClientFilterSettings _settings =
                 ConfigurationManager.GetSection(typeof(ClientFilterSettings).Name) as ClientFilterSettings         /*?? new ClientFilterSettings()*/; 
public static ClientFilterSettings Settings     
{       
      get { return _settings; }     
}

2)。从

更改属性
[ConfigurationCollection(typeof(IPAddressCollection))]

[ConfigurationCollection(typeof(IPAddressElement), AddItemName = "IPAddress", CollectionType = ConfigurationElementCollectionType.BasicMap)]

3)。您正在集合中创建集合对象。修改下面的代码

return new IPAddressCollection();

使用

return new IPAddressElement();