Generating Files with the TextTransform Utility
\Program Files\Common Files\Microsoft Shared\TextTemplating\11.0
C:\Program Files (x86)\Common Files\microsoft shared\TextTemplating\11.0
T4 (Text Template Transformation Toolkit) Code Generation - Best Kept Visual Studio Secret
十月 14, '08 Comments [30] Posted in ASP.NET | Programming | Tools Sponsored ByRob beat me to it. Blogging about T4 (the Text Template Transformation Toolkit) had been on my list literally for a year. He and I were singing its praises last night. Hopefully I can add something small to the conversation.
What's the story? Well, T4 is a code generator built right into Visual Studio. To be clear, you HAVE THIS NOW on your system…go play. Now's the time to introduce code generation to your company. If you're doing something twice or more, manually, in your company, generate it.
However, it's not deep-deep built in, because there's no item templates in File | New Item and there's no intellisense or syntax highlighting.
You don't need this, but if you want really get the most out of T4, first, head over to Clarius Consulting and get their "T4 Editor Community Edition." That'll get you some basic coloring. They have a pay version that gets you more if you want.
Now, go into Visual Studio and make a Console App (or any app) and add a Text File, but name it something with a .tt extension. You'll get a warning since it's a generator, that someone could generate evil. Click OK if you are cool with potential evil. ;)
Now, look in Solution Explorer at the .tt file. If you're using C#, you'll have a sub .cs file, or if you're using VB, a sub .vb file. That's the file that will hold the result of the generation. This is the same visual metaphor used to the express the template/generated file relationship with .designer files you've seen elsewhere in Visual Studio.
If you look in the Properties for the .tt file, you'll see it's using a CustomTool listed as the "TextTemplatingFileGenerator." This custom tool extensibility point is how things like XSD and WSDL code generators hook in to generate their artifacts.
T4 can generate any kind of text file, not just code. Whatever you like. You can use it in your projects, as above, or you can call T4 from the command-line.
Aside from Kzu and the folks at Clarius, Rob notes that Oleg Sych has a great collection of T4 resources. He's got some great Tutorials:
- Creating your first code generator
- Troubleshooting code generation errors
- Debugging code generation files
- Creating reusable code generation templates
- Creating complex code generators
- Reusing code generators on multiple projects
Here's some of the links from Oleg's blog.
- How to create a simple T4 template
- How to use T4 to generate .config files
- How to use T4 to generate Decorator classes
- How to use T4 to generate CRUD stored procedures
- How to use T4 to generate strongly-typed navigation class in ASP.NET (by Kirill Chilingarashvili)
- How to use T4 to generate strongly-typed AzMan wrapper
- How to generate multiple outputs from single T4 template
- T4 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# enumeration
- MSBuild 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. Here's an example, where I use Damien's T4 templates against the sample Chinook Database.
Notice that I've named the .tt file the same as the .dbml, so Damian's code can find it. I also continue to let original LINQ to SQL generate it's .designer.cs file, but make that file's Build Action "None" so it's not ever compiled. That effectively puts Damian's code in charge.
Here's a screenshot showing a bit of Damian's T4 template using the syntax highlighting from the Clairus T4 Visual Studio free download. If I'd pay them, I'd get intellisense and syntax highlighting inside the code blocks also. It looks like a lot like ASP.NET Web Forms, or Velocity, or any templating language really. The code blocks are where your logic is and outside the codeblocks is the template for whatever you want to generate. Notice how Damien gets input and sets output. You have full control, you can read files off the file system, from with your project, etc. He sets the output extension also. I like to use .g.cs or .g.vb, myself. In this example his generated file is Chinook.generated.cs.
I particularly like Damien's example because he's swapping out parts of LINQ to SQL that he didn't like (the generated code) while keeping the part he did (the general mode, the designer, the dbml file.) If you don't like something, fix it.
Plus, it all works in Visual Studio without installing anything.
If you're doing Code Gen, or thinking about it, check out T4 as it's a great place to start. Also, search my blog for "Code Generation" as I was livign and breathing it with CodeSmith for the many years I worked at Corillian. Have fun!
C:\Program Files (x86)\Common Files\microsoft shared\TextTemplating\11.0的更多相关文章
-
C:\Program Files (x86)\Common Files\microsoft shared\DevServer\10.0
C:\Program Files (x86)\Common Files\microsoft shared\DevServer\10.0
-
删除C:\Program Files (x86)\Common Files\baidu 等误装软件且正常模式删不掉的文件夹
---------方法一-------- C:\Program Files\Common Files\Baidu\BaiduProtect\1.1.0.26打开以上路径找到反注册程序uninst.ex ...
-
warning MSB3162: 所选的“Microsoft Report Viewer 2012 Runtime”项需要“Microsoft.SqlServer.SQLSysClrTypes.11.0”。在“系统必备”对话框中选择缺少的系统必备组件,或者为缺少的系统必备组件创建引导程序包。
warning MSB3162: 所选的“Microsoft Report Viewer 2012 Runtime”项需要“Microsoft.SqlServer.SQLSysClrTypes.11. ...
-
微软 microsoft calendar control 11.0 控件下载
微软 microsoft calendar control 11.0 控件下载 https://files.cnblogs.com/files/mqingqing123/csccal2.rar
-
Failed to execute command: ";";C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\ResGen.exe"; 的一个解决办法
最近在做wpf项目,期间下了一些源码参考,但是在build时经常遇到下面这种bug: Error 2 Failed to execute command: ""C:\Program ...
-
Local IIS 7.0 - CS0016: Could not write to output file / Microsoft.Net >; Framework >; v4.0.30319 >; Temporary ASP.NET Files
This week I went nuts over my local IIS. I have never swore to a machine that much in my whole life. ...
-
未能加载文件或程序集 Microsoft.ReportViewer.Common, Version=11.0.0.0
原文:未能加载文件或程序集 Microsoft.ReportViewer.Common, Version=11.0.0.0 System.IO.FileNotFoundException: 未能加载文 ...
-
RDLC报表 在WinForm里运行出现 未能加载文件或程序集 Microsoft.ReportViewer.WinForms, Version=11.0.0.0 System.IO.FileNotFoundException
原文:RDLC报表 在WinForm里运行出现 未能加载文件或程序集microsoft.reportviewer.winforms 推荐以下方案二 做一下记录顺便帮助一下遇到问题的朋友. 做RDLC报 ...
-
C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V120\Microsoft.CppBuild.targets(388,5): warning MSB8028: The intermediate directory (Debug) contains files shared from another project (GU.vcxproj). T
1>C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V120\Microsoft.CppBuild.targets(388,5): warni ...
随机推荐
-
android 图像处理系列合集
为了便于大家对滤镜算法的学习,以后发布的图像处理滤镜系列帖子会在这里汇总,本人第一次写合集,写得不好的地方大家请见谅,手头上虽然有一些滤镜的算法,但是大多不是android版的,教程里的代码大多是我借 ...
-
h264检测是I帧还是P帧,解决录像第一帧有马赛克问题
最近使用h264码流数据进行录像,但是录出来的第一帧有马赛克,究其原因是录像的第一帧不是关键帧,所以需要录像是需要判断第一帧是否是关键帧,方法有两种,第一种是原码流的基础上查找,第二种是将原码流传递给 ...
-
java编译环境
系统环境变量: path D:\Program Files\Java\jdk1.7.0_45\bin (若里面已经有内容,前面加一个分号) 在c盘当前目录,能编译d盘的文件吗----能 javac D ...
-
Using Qt to build an Omi App for iOS (and Android)
JUNE 6, 2014 / HHARTZ Working on projects where the technology is pre-determined, it's often difficu ...
-
Accounting Flexfield Setup and Usage (Doc ID 124333.1)
APPLIES TO:Oracle General Ledger - Version 11.5.10.2 to 12.1.3 [Release 11.5.10 to 12.1] Information ...
-
c#代码分析
代码分析是在一个IT行业计算机程序员必须要具有的基本专业技能,在了解一定的专业基础之上去看懂别人编写的代码,分析别人代码实现的功能,以及基本的维护和扩展测试.不同的人有不同的代码风格,要使自己的能要别 ...
-
Codeforces 219D - Choosing Capital for Treeland(树形dp)
http://codeforces.com/problemset/problem/219/D 题意 给一颗树但边是单向边,求至少旋转多少条单向边的方向,可以使得树上有一点可以到达树上任意一点,若有多个 ...
-
luogu P4289 [HAOI2008]移动玩具
传送门 这道题可以二进制记录状态搜索 也可以做以下考虑 若一个棋子要移动到另一个位置上去,则步数为两点的曼哈顿距离(横坐标差的绝对值+纵坐标差的绝对值),因为假设路径上有其他的棋子,可以通过移动其他棋 ...
-
Linux命令应用大词典-第29章 SELinux管理
29.1 sestaus:显示SElinux的状态 29.2 getenforce:显示当前SELinux的应用模式 29.3 setenforce:修改SELinux的应用模式 29.4 getfa ...
-
Java EE 学习(3):IDEA + maven 搭建 web(1)
摘要: 主要讲解使用 IDEA 开发 Spring MVC 的环境搭建,Maven的简单教学. 参考1:https://my.oschina.net/gaussik/blog/385697 参考2:h ...