Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save dontpaniclabsgists/8d794b9dcf2f72f04823ab523bceab38 to your computer and use it in GitHub Desktop.
Save dontpaniclabsgists/8d794b9dcf2f72f04823ab523bceab38 to your computer and use it in GitHub Desktop.
Custom Configuration Sections 7
using System.Configuration;
namespace LearnConfigCollection
{
public class ServerSection : ConfigurationSection
{
[ConfigurationProperty("", IsRequired = true, IsDefaultCollection = true)]
public ServerCollection Instances
{
get { return (ServerCollection)this[""]; }
set { this[""] = value; }
}
}
public class ServerCollection : ConfigurationElementCollection
{
protected override ConfigurationElement CreateNewElement()
{
return new ServerElement();
}
protected override object GetElementKey(ConfigurationElement element)
{
//set to whatever Element Property you want to use for a key
return ((ServerElement)element).Name;
}
}
public class ServerElement : ConfigurationElement
{
//Make sure to set IsKey=true for property exposed as the GetElementKey above
[ConfigurationProperty("key", IsKey = true, IsRequired = true)]
public string Key
{
get { return (string)base["key"]; }
set { base["key"] = value; }
}
[ConfigurationProperty("ipAddress", IsRequired = true)]
public string IpAddress
{
get { return (string)base["ipAddress"]; }
set { base["ipAddress"] = value; }
}
[ConfigurationProperty("ownerEmail", IsRequired = false)]
public string OwnerEmail
{
get { return (string)base["ownerEmail"]; }
set { base["ownerEmail"] = value; }
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment