我已经做了T4模板非常相似的东西秒。技巧是知道需要哪些dll,找到它们等(例如,在Visual Studio 2013中,EnvDET被引用为“import namespace =”EnvDTE“而不是EvnDTE.dll)
基本上你需要和他们一起玩尝试并得到你想要的结果在你的解决方案中创建一个T4项目,然后开始填充它。
我使用命令对象(并且使用下面的代码使用MySQL库),但它应该给你一个想法怎么我跑了。
<#@ template debug="true" hostSpecific="true" #>
<#@ output extension=".generated.cs" #>
<#@ Assembly Name="EnvDTE" #>
<#@ Assembly Name="System.Data" #>
<#@ Assembly Name="$(SolutionDir)Services.Entities\bin\DevTest\Libraries.DB.dll" #> **// * custom location for custom libraries**
<#@ Assembly Name="$(SolutionDir)Services.Entities\bin\DevTest\MySql.Data.dll" #> **// custom location for custom libraries**
<#@ import namespace="EnvDTE" #>
<#@ import namespace="System.Data" #>
<#@ import namespace="System.Data.SqlClient" #>
<#@ import namespace="System.IO" #>
<#@ import namespace="System.Text.RegularExpressions" #>
<#
string tableName = "";
//string path = Path.GetDirectoryName(Host.TemplateFile);
string connectionString = "mysqlConnectionString"; **// replaced with regular value, could pull from web.config but it's only updated rarely**
// Get containing project
IServiceProvider serviceProvider = (IServiceProvider)Host;
DTE dte = (DTE)serviceProvider.GetService(typeof(DTE));
//Project project = dte.Solution.FindProjectItem(Host.TemplateFile).ContainingProject;
#>
using System;
using System.CodeDom.Compiler;
namespace Services.Entities
{
[GeneratedCode("TextTemplatingFileGenerator", "10")]
public enum Store
{
<#
using (var cmd = new Libraries.DB.Mysql.Command(connectionString)) **//Custom libraries to open a disposable command object**
{
cmd.Clear();
cmd.AppendCmd("select id, storecode, StoreName \n");
cmd.AppendCmd("from stores \n");
var reader = cmd.ExecuteReader();
while(reader.Read())
{
int storeId = Convert.ToInt32(reader["id"]);
string storecode = reader["storecode"].ToString();
string description = reader["StoreName"].ToString();
#> //We now have the data, let's use it! The code in <#= gets populated #>**
[Libraries.Utils.Enums.Info(Code = "<#= storecode #>", Name = "<#= description #>")] <#= Sanitize(description) #> = <#= storeId #>,
<#
}
}
#>
}
}
//This is a method which takes a name like "Some Descripton" and Sanitizes it to Some_Description so it can be usable as an enum**
<#+
static string Sanitize(string token)
{
// Replace all invalid chars by underscores
token = Regex.Replace(token, @"[\W\b]", "_", RegexOptions.IgnoreCase);
// If it starts with a digit, prefix it with an underscore
token = Regex.Replace(token, @"^\d", @"_$0");
// Check for reserved words
// TODO: Clean this up and add other reserved words (keywords, etc)
if (token == "Url") token = "_Url";
return token;
}
#>
需要注意这些文件的关键问题是,之间< ##家居>是代码即运行,并且您可以在该代码中创建变量。显示,很像ASP,<#=变量#>。
我不确定这些文件是否在您每次构建解决方案时自动构建,但似乎这将是一个简单的构建脚本。要编辑它们,请在保存时进行编辑。您可以右键单击该文件并从上下文菜单中单击“运行自定义工具”。您将通过文件和相关的生成文件来查看错误,以检查它。
顺便说一句,上面可能会产生这样的事情:
using System;
using System.CodeDom.Compiler;
namespace Services.Entities
{
[GeneratedCode("TextTemplatingFileGenerator", "10")]
public enum Store
{
[Libraries.Utils.Enums.Info(Code = "ST1", Name = "Store 1")] Store1 = 1,
[Libraries.Utils.Enums.Info(Code = "ST2", Name = "Store 2")] Store2 = 2,
}
}
顺便说一句,这是一个标签(<#,<#=,<#+)之外的一切都将被发送作为班级的输出。您需要确保您的模板在运行时正确设置,否则您可能因缺少括号而导致编译错误等。