2014-03-24 2 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(IPAddressElement), AddItemName = "IPAddress", CollectionType = ConfigurationElementCollectionType.BasicMap)] 

3)에게

[ConfigurationCollection(typeof(IPAddressCollection))] 

에서 속성을 변경합니다. 콜렉션 내에서 콜렉션 오브젝트를 작성 중입니다.

return new IPAddressElement(); 
+0

가 근무와 함께

return new IPAddressCollection(); 

아래의 코드를 수정, 감사합니다! 나는 무엇이 일어나는지보기 위해 흐릿 해 보이는 법이었다. 포인트 1 : 그것은 재귀가 아니며 속성에 접근하기위한 정적 접근 자입니다. 그대로 잘 작동합니다. 포인트 2 : 실제로 코드에서 이전에 코드를 제거했지만 제거했습니다. 포인트 3 실제 문제를 해결 - 내가 잘못된 데이터 형식을 반환했다! 다시 한번 감사드립니다. 어제 밤에 그것을 보지 못했습니다. – DiskJunky

+1

1 점을 언급 해 주셔서 고맙습니다. 나는 완전히 정적 키워드를 간과했습니다. 나는 아침에 흐릿한 눈을 가졌다 고 생각한다 ;-) ... – Riz

관련 문제