Created
August 15, 2014 23:36
-
-
Save hartez/699a6ca20c017b8a14e5 to your computer and use it in GitHub Desktop.
T4 template for generating refactorable settings
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
<#@ 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