Created
June 29, 2019 03:28
-
-
Save dontpaniclabsgists/8d794b9dcf2f72f04823ab523bceab38 to your computer and use it in GitHub Desktop.
Custom Configuration Sections 7
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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