Skip to content

Instantly share code, notes, and snippets.

@ChrisMcKee
Created January 4, 2013 12:13
Show Gist options
  • Select an option

  • Save ChrisMcKee/4452192 to your computer and use it in GitHub Desktop.

Select an option

Save ChrisMcKee/4452192 to your computer and use it in GitHub Desktop.
Random use of a custom config with nested keys.
namespace Core.Configuration
{
using System;
using System.Configuration;
public class MessageConfigurationSection : ConfigurationSection
{
[ConfigurationProperty("messages", IsDefaultCollection = false)]
[ConfigurationCollection(typeof (MessageCollection), AddItemName = "add", ClearItemsName = "clear", RemoveItemName = "remove")]
public MessageCollection Messages
{
get { return base["messages"] as MessageCollection; }
}
}
public class MessageCollection : ConfigurationElementCollection
{
public MessageCollection()
{
var url = (MessageElement) CreateNewElement();
Add(url);
}
public override ConfigurationElementCollectionType CollectionType
{
get { return ConfigurationElementCollectionType.AddRemoveClearMap; }
}
public MessageElement this[int index]
{
get { return (MessageElement) BaseGet(index); }
set
{
if (BaseGet(index) != null)
{
BaseRemoveAt(index);
}
BaseAdd(index, value);
}
}
public new MessageElement this[string name]
{
get { return (MessageElement) BaseGet(name); }
}
protected override ConfigurationElement CreateNewElement()
{
return new MessageElement();
}
protected override Object GetElementKey(ConfigurationElement element)
{
return ((MessageElement) element).Name;
}
public int IndexOf(MessageElement url)
{
return BaseIndexOf(url);
}
public void Add(MessageElement url)
{
BaseAdd(url);
}
protected override void BaseAdd(ConfigurationElement element)
{
BaseAdd(element, false);
}
public void Remove(MessageElement url)
{
if (BaseIndexOf(url) >= 0)
BaseRemove(url.Name);
}
public void RemoveAt(int index)
{
BaseRemoveAt(index);
}
public void Remove(string name)
{
BaseRemove(name);
}
public void Clear()
{
BaseClear();
}
}
public class MessageElement : ConfigurationElement
{
[ConfigurationProperty("name", IsRequired = true, IsKey = true)]
public string Name
{
get { return base["name"] as string; }
set { base["name"] = value; }
}
[ConfigurationProperty("title", IsRequired = true, IsKey = false)]
public string Title
{
get { return base["title"] as string; }
set { base["title"] = value; }
}
[ConfigurationProperty("copy", IsRequired = true, IsKey = false)]
public string Copy
{
get { return base["copy"] as string; }
set { base["copy"] = value; }
}
[ConfigurationProperty("startDate", IsRequired = false, IsKey = false)]
public DateTime StartDate
{
get { return Convert.ToDateTime(base["startDate"]); }
set { base["startDate"] = value; }
}
[ConfigurationProperty("endDate", IsRequired = false, IsKey = false)]
public DateTime EndDate
{
get { return Convert.ToDateTime(base["endDate"]); }
set { base["endDate"] = value; }
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment