Skip to content

Instantly share code, notes, and snippets.

@ilkerhalil
Created November 5, 2015 19:41
Show Gist options
  • Save ilkerhalil/14e30dd6c3ae659acfb0 to your computer and use it in GitHub Desktop.
Save ilkerhalil/14e30dd6c3ae659acfb0 to your computer and use it in GitHub Desktop.
namespace CustomConfigSectionForT4.ConfigSection
{
using System;
using System.Configuration;
/*
Add the code your app.config or web.config
<configSections>
<section name="SmsSenderConfigSection" type="CustomConfigSectionForT4.ConfigSection.SmsSenderConfigSection,CustomConfigSectionForT4" />
</configSections>
<SmsSenderConfigSection UserName="example value" Password="example value" MsIsdn="example value">
<ForbiddenNumbers>
<add value="example value" />
</ForbiddenNumbers>
</SmsSenderConfigSection>
*/
public class SmsSenderConfigSection : ConfigurationSection
{
[ConfigurationProperty("UserName")]
public System.String UserName
{
get
{
return (System.String)this["UserName"];
}
set
{
this["UserName"] = value;
}
}
[ConfigurationProperty("Password")]
public System.String Password
{
get
{
return (System.String)this["Password"];
}
set
{
this["Password"] = value;
}
}
[ConfigurationProperty("MsIsdn")]
public System.String MsIsdn
{
get
{
return (System.String)this["MsIsdn"];
}
set
{
this["MsIsdn"] = value;
}
}
[ConfigurationProperty("ForbiddenNumbers")]
public ValueConfigurationCollection<string> Names
{
get
{
return (ValueConfigurationCollection<System.String>)base["ForbiddenNumbers"];
}
}
}
public class ValueConfigurationElement<T> : ConfigurationElement
{
private const string ValueAttributeName = "value";
[ConfigurationProperty(ValueAttributeName, IsKey = true)]
public T Value
{
get { return (T)this[ValueAttributeName]; }
set { this[ValueAttributeName] = value; }
}
}
public class ValueConfigurationCollection<T> : ConfigurationElementCollection
{
protected override ConfigurationElement CreateNewElement()
{
return new ValueConfigurationElement<T>();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((ValueConfigurationElement<T>)element).Value.GetHashCode();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment