Created
March 7, 2019 09:39
-
-
Save Axemasta/a3b8eff07caf5014a5f0143b106a42d5 to your computer and use it in GitHub Desktop.
Custom App Config Sections
This file contains 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
<?xml version="1.0" encoding="utf-8"?> | |
<configuration> | |
<configSections> | |
<section name="excludeKeywords" type="YourNameSpace.SectionHandler, YourNamespace" /> | |
</configSections> | |
<startup> | |
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/> | |
</startup> | |
<appSettings> | |
<add key="Test" value="Test value!"/> | |
</appSettings> | |
<!--<includeRegex> | |
<regex pattern=""/> | |
</includeRegex>--> | |
<!--Objects can be stored & deserialized in the section handler--> | |
<yourSectionName> | |
<item value="4097821023436939"/> | |
<item value="4010864067044940"/> | |
</yourSectionName> | |
</configuration> |
This file contains 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.Collections.Generic; | |
using System.Configuration; | |
using System.Xml; | |
public class ExcludeKeywordSection : IConfigurationSectionHandler | |
{ | |
public object Create(object parent, object configContext, XmlNode section) | |
{ | |
List<string> myConfigObject = new List<string>(); | |
foreach (XmlNode childNode in section.ChildNodes) | |
{ | |
foreach (XmlAttribute attrib in childNode.Attributes) | |
{ | |
myConfigObject.Add(attrib.Value); | |
} | |
} | |
return myConfigObject; | |
} | |
} |
This file contains 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
#region Properties | |
#region - App Config Properties | |
public static List<string> ExampleList | |
{ | |
get | |
{ | |
try | |
{ | |
List<string> list = (List<string>)ConfigurationManager.GetSection("yourSectionName"); | |
return list; | |
} | |
catch (Exception) | |
{ | |
return null; | |
} | |
} | |
} | |
#endregion - App Config Properties | |
#endregion - Properties |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment