为此,您还可以使用codesmith模板。优点是可以在模板文件属性中设置,以便在每次生成时重新生成(set buildAction=“complile”)
编辑
我也在寻找这样的解决方案。在谷歌搜索之后,我找到了生成此类的基本t4模板。
我重新设计了它,你可以在下面找到它。
模板正在从web.config/app.config文件生成appsetting节的包装类
假设在配置文件中有以下设置行
<appSettings>
<add key="PageSize" value="20" />
<add key="CurrentTheme" value="MyFavouriteTheme" />
<add key="IsShowSomething" value="True" />
</appSettings>
处理完模板后,您将得到以下类
namespace MyProject.Core
{
/// <remarks>
/// You can create partial class with the same name in another file to add custom properties
/// </remarks>
public static partial class SiteSettings
{
/// <summary>
/// Static constructor to initialize properties
/// </summary>
static SiteSettings()
{
var settings = System.Configuration.ConfigurationManager.AppSettings;
PageSize = Convert.ToInt32( settings["PageSize"] );
CurrentTheme = ( settings["CurrentTheme"] );
IsShowSomething = Convert.ToBoolean( settings["IsShowSomething"] );
}
/// <summary>
/// PageSize configuration value
/// </summary>
public static readonly int PageSize;
/// <summary>
/// CurrentTheme configuration value
/// </summary>
public static readonly string CurrentTheme;
/// <summary>
/// IsShowSomething configuration value
/// </summary>
public static readonly bool IsShowSomething;
}
}
将以下代码保存到*.tt文件
并将生成的文件包含到您的项目中。
在每个生成上重新生成类
see my answer here
模板从值中识别字符串、日期时间、int和bool类型
<#@ assembly name="System.Core" #>
<#@ assembly name="System.Xml" #>
<#@ assembly name="System.Xml.Linq" #>
<#@ import namespace="System" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.IO" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Xml.Linq" #>
<#@ import namespace="Microsoft.VisualBasic" #>
<#@ template language="VB" debug="True" hostspecific="True" #>
<#@ output extension=".Generated.cs" #>
<#
Dim projectNamespace as String = "MyProject.Core"
Dim className as String = "SiteSettings"
Dim fileName as String = "..\..\MyProject.Web\web.config"
Init(fileName)
#>
//------------------------------------------------------------------------------
// FileName = <#= path #>
// Generated at <#= Now.ToLocaltime() #>
//
// <auto-generated>
// This code was generated by a tool.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
//
// NOTE: Please use the Add a Reference to System.Configuration assembly if
// you get compile errors with ConfigurationManager
// </auto-generated>
//------------------------------------------------------------------------------
using System;
using System.Configuration;
namespace <#= projectNamespace #>
{
/// <remarks>
/// You can create partial class with the same name in another file to add custom properties
/// </remarks>
public static partial class <#= className #>
{
/// <summary>
/// Static constructor to initialize properties
/// </summary>
static <#= className #>()
{
var settings = System.Configuration.ConfigurationManager.AppSettings;
<#= AddToCostructor(path) #> }
<#= RenderApplicationSettings(path) #> }
}
<#+
Dim path as String = ""
Dim doc as XDocument = Nothing
Public Sub Init(fileName as String)
Try
path = Host.ResolvePath(fileName)
If File.Exists(path) Then
doc = XDocument.Load(path)
End If
Catch
path = "<< App.config or Web.config not found within the project >>"
End Try
End Sub
Public Function AddToCostructor(ByVal path as String) as String
If doc Is Nothing Then Return ""
Dim sb as New StringBuilder()
For Each result as XElement in doc...<appSettings>.<add>
sb.Append(vbTab).Append(vbTab).Append(vbTab)
sb.AppendFormat("{0} = {1}( settings[""{0}""] );", result.@key, GetConverter(result.@value))
sb.AppendLine()
Next
Return sb.ToString()
End Function
Public Function RenderApplicationSettings(ByVal path as String) as String
If doc Is Nothing Then Return ""
Dim sb as New StringBuilder()
For Each result as XElement in doc...<appSettings>.<add>
dim key = result.@key
sb.Append(vbTab).Append(vbTab)
sb.Append("/// <summary>").AppendLine()
sb.Append(vbTab).Append(vbTab)
sb.AppendFormat("/// {0} configuration value", key).AppendLine()
sb.Append(vbTab).Append(vbTab)
sb.Append("/// </summary>").AppendLine()
sb.Append(vbTab).Append(vbTab)
sb.AppendFormat("public static readonly {0} {1}; ", GetPropertyType(result.@value), key)
sb.AppendLine().AppendLine()
Next
Return sb.ToString()
End Function
Public Shared Function GetConverter(ByVal prop as String) as String
If IsNumeric(prop) Then Return "Convert.ToInt32"
If IsDate(prop) Then Return "Convert.ToDateTime"
dim b as Boolean
If Boolean.TryParse(prop, b) Then Return "Convert.ToBoolean"
Return ""
End Function
Public Shared Function GetPropertyType(ByVal prop as String) as String
If IsNumeric(prop) Then Return "int"
If IsDate(prop) Then Return "DateTime"
dim b as Boolean
If Boolean.TryParse(prop, b) Then Return "bool"
Return "string"
End Function
#>