Skip to content

Instantly share code, notes, and snippets.

@AndrewBuntsev
Last active May 19, 2020 09:55
Show Gist options
  • Select an option

  • Save AndrewBuntsev/a3c230868e460b8c17c1d7d1f636f543 to your computer and use it in GitHub Desktop.

Select an option

Save AndrewBuntsev/a3c230868e460b8c17c1d7d1f636f543 to your computer and use it in GitHub Desktop.
///
/// ConfigurationElement with additional Id property
///
public abstract class IdentifiableConfigurationElement : ConfigurationElement
{
public virtual string Id { get; }
}
///
/// Generic ConfigurationElementCollection
///
public class ConfigurationElementCollection<T> : ConfigurationElementCollection where T : IdentifiableConfigurationElement, new()
{
public T this[int index]
{
get { return (T)BaseGet(index); }
set
{
if (BaseGet(index) != null)
{
BaseRemoveAt(index);
}
BaseAdd(index, value);
}
}
public void Add(T serviceConfig)
{
BaseAdd(serviceConfig);
}
public void Clear()
{
BaseClear();
}
protected override ConfigurationElement CreateNewElement()
{
return new T();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((T)element).Id;
}
public void Remove(T serviceConfig)
{
BaseRemove(serviceConfig.Id);
}
public void RemoveAt(int index)
{
BaseRemoveAt(index);
}
public void Remove(String name)
{
BaseRemove(name);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment