2014-03-24 67 views
0

我正在創建一個自定義配置部分,但在嘗試獲取該部分時不斷收到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; } 
     } 
    } 
} 

回答

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))] 

屬性TO

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

3)。您正在集合中創建集合對象。修改下面

return new IPAddressCollection(); 

的代碼

return new IPAddressElement(); 
+0

的工作,謝謝!我是模糊的眼睛看看發生了什麼。在第1點上:它不是遞歸,它是獲取屬性的靜態訪問器。按原樣正常工作。關於第2點:我以前曾在代碼中使用過,但已將其刪除。第3點解決了實際問題 - 我返回了不正確的數據類型!再次感謝,昨晚它已經被打破了 – DiskJunky

+1

感謝提及點1.我完全忽略了靜態關鍵字。我想我早上有模糊的眼睛;-) ... – Riz

相關問題