首页 > 代码库 > t4-editor使用方法 Visual T4
t4-editor使用方法 Visual T4
原文发布时间为:2011-05-17 —— 来源于本人的百度文章 [由搬家工具导入]
http://visualstudiogallery.msdn.microsoft.com/40a887aa-f3be-40ec-a85d-37044b239591
可以这里下载,也可以在 vs的 工具——扩展管理器 里面搜索 安装即可。
引入一些常用的命名空间并使用的方法如下:
<#@ template debug="false" hostspecific="false" language="C#" #>
<#@ output extension=".txt" #>
<#@ 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" #>
<#
var a = "sdfsdf".Select(it => it);
foreach (var b in a)
{
#>
<#= b #>
<#
}
#>
载入自定义dll,如
<#@ Assembly Name="E:\CodeTest\TestT4\My\bin\Debug\My.dll" #>
<#@ Import Namespace="My" #>
一个模板生成多个文件,范例(详见:http://www.olegsych.com/2008/03/how-to-generate-multiple-outputs-from-single-t4-template/)
<#@ template debug="False" hostspecific="True" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ assembly name="System.Xml" #>
<#@ assembly name="System.Xml.Linq" #>
<#@ assembly name="System.Web" #>
<#@ Assembly Name="System.Web.Mvc" #>
<#@ import namespace="System" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.IO" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Xml.Linq" #>
<#
HelloWorld(12);
SaveOutput("test1.txt");
HelloWorld(13);
SaveOutput("test2.txt");
HelloWorld(14);
SaveOutput("test3.txt");
#>
<#+
void HelloWorld(int i)
{
Write("Hello World" + i);
#>
This is file <#= i #>
<#+
}
#>
<#+
void SaveOutput(string outputFileName)
{
string templateDirectory = Path.GetDirectoryName(Host.TemplateFile);
string outputFilePath = Path.Combine(templateDirectory, outputFileName);
File.WriteAllText(outputFilePath, this.GenerationEnvironment.ToString());
this.GenerationEnvironment.Remove(0, this.GenerationEnvironment.Length);
}
#>
http://www.olegsych.com/2008/03/how-to-generate-multiple-outputs-from-single-t4-template/
中文MSDN这里:http://msdn.microsoft.com/zh-cn/library/dd820620
Tutorials:Creating your first code generatorTroubleshooting code generation errorsDebugging code generation filesCreating reusable code generation templatesCreating complex code generatorsReusing code generators on multiple projects
Here's some of the links from Oleg's blog.
How to create a simple T4 templateHow to use T4 to generate .config filesHow to use T4 to generate Decorator classesHow to use T4 to generate CRUD stored proceduresHow to use T4 to generate strongly-typed navigation class in ASP.NET (by Kirill Chilingarashvili) How to use T4 to generate strongly-typed AzMan wrapperHow to generate multiple outputs from single T4 templateT4 template for generating ADO.NET Entity Framework Stored Procedures (by David DeWinter) T4 script for generating ADO.NET Entity Framework Views (by ADO.NET team) T4 template for generating LINQ to SQL Data Context (by Damien Guard) T4 template for generating WiX source files (by New Age Solutions) T4 template for generating SQL view from C# enumerationMSBuild task for transforming T4 templates (by Elton Stoneman) T4 template for generating state machines in C# (by Andrew Matthews)Oleg also has a CodePlex project called T4 Toolbox that is a library of T4 templates that get added to File | New Item.
Also, check out Damien Guard's T4 templates that are a wholesale replacement of code that LINQ to SQL generates.
===其他的学习,可以到这个hanselman的博客。
http://www.hanselman.com/blog/T4TextTemplateTransformationToolkitCodeGenerationBestKeptVisualStudioSecret.aspx
t4-editor使用方法 Visual T4