2010-05-06 9 views
7

전자 메일을 키로 사용하여 내 디자인의 ConfigurationElementCollection을 설치했습니다. 이제 뭐? 실제로 웹상에서 찾기가 어렵습니다. 어떻게해야합니까 :ConfigurationElementCollection에서 config 요소 가져 오기

  1. 통해 반복할까요?

  2. 특정 요소가 있는지 확인하십시오.

  3. 특정 요소를 얻으시겠습니까? 주어진

... :

YourConfigElement config = 
    ConfigurationManager.GetSection("YourSectionName") as YourConfigElement; 

부분 대답

1.

foreach (X x in config.XCollection) 
      <code here> 

2. 여기에서 "코드"를

{ 
    if (x.Y == needle) 
    { 
     hasIndeed = true; 
     break; 
    } 
} 

3으로 바꿉니다. 여기에 "코드"를

{ if (x.Y == needle) 
     cameUpWith = x; 
     break; 
} 

으로 대체하십시오.

답변

2

나는 완전히 당신의 문제가 무엇인지 이해하지 않는 -하지만 당신은 사용자 정의 구성 요소가있는 경우 기본적으로, 당신은 같은 것을 사용하여 config 파일에서 해당를 검색 할 수 있어야한다 : 당신이 한 번

YourConfigElement config = 
    ConfigurationManager.GetSection("YourSectionName") as YourConfigElement ; 

구성 요소가 있으면 원하는대로 할 수 있습니다. 요청한 모든 것을 구현할 수 있습니다. 요소의 존재 여부를 확인하고 특정 요소를 가져옵니다.

또한 Jon Rista의 세 부분으로 구성되어 있습니다 시리즈에 대한 자세한 내용은 CodeProject에서 .NET 2.0 구성 - 어쩌면 그 기사는 요 잠금을 해제하는 데 도움이됩니다 UR 설정 "도전";-)

적극 권장, 잘 쓰여진 매우 도움이!

그리고 아직 발견하지 못했다면 Codeplex에 뛰어난 Configuration Section Designer이 있습니다. 구성 섹션과 컬렉션을 시각적으로 디자인하고 모든 끈적 거리는 코드를 간단하게 작성합니다. 매우 편리합니다.

마크

+0

답장을 보내 주셔서 감사합니다. 나를위한 어려운 부분은 컬렉션 유형입니다. 나는 # 1을 풀었지만 하나의 키를 찾는다면 더 예쁘고 빠른 방법이 있어야합니다. 나는 너의 연결을 검사 할 것이다. – Martin

+1

나는 실제로 아주 즐겁다. 내가 추천 한 페이지는 처음 방문한 유사한 콘텐츠가 아닙니다. 이제는 컬렉션 형식을 정의하고 구성에 요소를 추가하는 데 능숙합니다. 실제로 모든 코드를 컬렉션에서 사용합니다. AsQueryable() 메서드를 노출합니다. 그게 단서이지만, 질의 가능한 것은 차례대로 사용의 어떤 것도 드러내지 않는다 - get 또는 contains, 예를 들면. – Martin

8

당신이 IList<T>를 구현하는 자신의 일반적인 ConfigurationElementCollection 기본 클래스가 원하는 것은. 그런 다음 모든 구성 모음에 대해이를 상속하고 구성 모음을 만들 때 수행해야하는 작업량을 줄일 수 있습니다.

public abstract class BaseConfigurationElementCollection<TConfigurationElementType> : ConfigurationElementCollection, IList<TConfigurationElementType> where TConfigurationElementType : ConfigurationElement, IConfigurationElementCollectionElement, new() 
{ 
    protected override ConfigurationElement CreateNewElement() 
    { 
     return new TConfigurationElementType(); 
    } 

    protected override object GetElementKey(ConfigurationElement element) 
    { 
     return ((TConfigurationElementType)element).ElementKey; 
    } 

    public IEnumerator<TConfigurationElement> GetEnumerator() 
    { 
     foreach (TConfigurationElement type in this) 
     { 
      yield return type; 
     } 
    } 

    public void Add(TConfigurationElementType configurationElement) 
    { 
     BaseAdd(configurationElement, true); 
    } 

    public void Clear() 
    { 
     BaseClear(); 
    } 

    public bool Contains(TConfigurationElementType configurationElement) 
    { 
     return !(IndexOf(configurationElement) < 0); 
    } 

    public void CopyTo(TConfigurationElementType[] array, int arrayIndex) 
    { 
     base.CopyTo(array, arrayIndex); 
    } 

    public bool Remove(TConfigurationElementType configurationElement) 
    { 
     BaseRemove(GetElementKey(configurationElement)); 

     return true; 
    } 

    bool ICollection<TConfigurationElementType>.IsReadOnly 
    { 
     get { return IsReadOnly(); } 
    } 

    public int IndexOf(TConfigurationElementType configurationElement) 
    { 
     return BaseIndexOf(configurationElement); 
    } 

    public void Insert(int index, TConfigurationElementType configurationElement) 
    { 
     BaseAdd(index, configurationElement); 
    } 

    public void RemoveAt(int index) 
    { 
     BaseRemoveAt(index); 
    } 

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

조금 더 많은 작업을하면 사전 컬렉션도 가질 수 있습니다.