당신이 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);
}
}
}
조금 더 많은 작업을하면 사전 컬렉션도 가질 수 있습니다.
답장을 보내 주셔서 감사합니다. 나를위한 어려운 부분은 컬렉션 유형입니다. 나는 # 1을 풀었지만 하나의 키를 찾는다면 더 예쁘고 빠른 방법이 있어야합니다. 나는 너의 연결을 검사 할 것이다. – Martin
나는 실제로 아주 즐겁다. 내가 추천 한 페이지는 처음 방문한 유사한 콘텐츠가 아닙니다. 이제는 컬렉션 형식을 정의하고 구성에 요소를 추가하는 데 능숙합니다. 실제로 모든 코드를 컬렉션에서 사용합니다. AsQueryable() 메서드를 노출합니다. 그게 단서이지만, 질의 가능한 것은 차례대로 사용의 어떤 것도 드러내지 않는다 - get 또는 contains, 예를 들면. – Martin