Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
T4 template for generating refactorable settings
<#@ template language="C#" hostspecific="True" #>
<#@ assembly name="System.Xml" #>
<#@ import namespace="System.IO" #>
<#@ import namespace="System.Xml" #>
<#
XmlDocument doc = new XmlDocument();
doc.Load(Path.Combine(Path.GetDirectoryName(Host.TemplateFile), "Settings.settings"));
string xmlns = doc.DocumentElement.Attributes["xmlns"].Value;
XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("n", xmlns);
XmlNode settingsFile = doc.SelectSingleNode("//n:SettingsFile", nsmgr);
String className = settingsFile.Attributes["GeneratedClassName"].Value;
#>
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:4.0.30319.18444
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
namespace <#= settingsFile.Attributes["GeneratedClassNamespace"].Value #> {
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Refactorable Settings Generator", "1.0.0")]
internal sealed partial class <#= className #> : global::System.Configuration.ApplicationSettingsBase {
private static <#= className #> defaultInstance = ((<#= className #>)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new <#= className #>())));
public static <#= className #> Default {
get {
return defaultInstance;
}
}
public static class SettingsNames
{
<#
var settingsNodes = doc.SelectNodes("//n:Setting", nsmgr);
// Generate the static setting names
if(settingsNodes != null)
{
foreach(XmlNode node in settingsNodes)
{
if(node.Attributes == null)
{
continue;
}
var settingName = node.Attributes["Name"].Value;
#>
public const string <#= settingName #> = "<#= settingName #>";
<#
}
}
#>
}
<#
// Generate the code for each setting
if(settingsNodes != null)
{
foreach(XmlNode node in settingsNodes)
{
if(node.Attributes == null)
{
continue;
}
var type = node.Attributes["Type"].Value;
var name = node.Attributes["Name"].Value;
var scope = node.Attributes["Scope"].Value;
var value = node.SelectSingleNode("n:Value", nsmgr);
#>
<#
if(scope == "User")
{
#>
[global::System.Configuration.UserScopedSettingAttribute()]
<#
}
if(scope == "Application")
{
#>
[global::System.Configuration.ApplicationScopedSetting()]
<#
}
#>
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
<#
if(value != null)
{
#>
[global::System.Configuration.DefaultSettingValueAttribute("<#= value.InnerText #>")]
<#
}
#>
public <#= type #> <#= name #> {
get {
return ((<#= type #>)(this["<#= name #>"]));
}
set {
this["<#= name #>"] = value;
}
}
<#
}
}
#>
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.