I had a similar idea. I wanted to have a static class with constant string values ββfor my user settings in order to use a strong type, not a string in a Windows Forms binding:
this.textBox1.DataBindings.Add("Text", Properties.Settings.Default, Properties.Settings.PropertyNames.SomeStringValue);
However, I found out that the t4 template cannot access the types of the current project. You must upload the file using
<#@ assembly name="$(TargetPath)" #>
However, if you do this and regenerate your template, you will not rebuild your project, since the file is used if you do not close the visual studio.
Shortly speaking,
I abandoned this approach, and now I myself read the app.config file to get the data I need. It should be fairly easy to modify to read the Resources.xml file.
<#@ template debug="true" hostspecific="true" language="C#" #> <#@ output extension=".cs" #> <#@ assembly name="System.Core" #> <#@ assembly name="System.Xml" #> <#@ assembly name="System.Xml.Linq" #> <#@ import namespace="System.IO" #> <#@ import namespace="System.Linq" #> <#@ import namespace="System.Xml.Linq" #> <# var xmlFile = Path.Combine(Path.GetDirectoryName(Host.TemplateFile), "..", "app.config"); var query = from x in XElement.Load(xmlFile).Element("userSettings").Element("Your.Namespace.Properties.Settings").Elements("setting") select x; #> namespace Your.Namespace.Properties { public sealed partial class Settings { public static class PropertyNames { <# foreach (var item in query) { #> public static readonly string <#=item.Attribute("name").Value#> = @"<#=item.Attribute("name").Value#>"; <# } #> } } }
source share