T4(文本模板转换工具包)代码生成-最佳保存的Visual Studio秘密

Rob 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.

罗伯击败我。 从字面上看,有关T4的博客(文本模板转换工具包)在我的名单上已经存在了一年。 我和他昨晚在赞美他。 希望我可以在对话中添加一些小东西。

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.

来龙去脉是什么? 好吧, T4是Visual Studio中内置的代码生成器。 需要明确的是,您现在已经在系统上……开始游戏。 现在是时候向您的公司介绍代码生成了。 如果您要执行两次或多次操作,请在公司中手动生成。

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.

但是,它不是内置的,因为File |中没有项目模板。 新项目,没有智能感知或语法突出显示。

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.

您不需要这个,但是如果您想真正从T4中获得最大收益,请首先前往Clarius Consulting,获取其“ T4编辑器社区版” 。 这将为您提供一些基本的着色。 他们提供付费版本,可让您获得更多收益。

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. ;)

现在,进入Visual Studio,制作一个控制台应用程序(或任何应用程序),并添加一个文本文件,但是将其命名为.tt扩展名。 您会收到警告,因为它是发电机,有人可能会制造邪恶。 如果您对潜在的邪恶感到很冷静,请单击“确定”。 ;)

Security Warning

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.

现在,在解决方案资源管理器中查看.tt文件。 如果使用的是C#,则将有一个.cs子文件;如果使用的是VB,则将有一个.vb子文件。 那就是保存生成结果的文件。 这与您在Visual Studio中其他地方看到的用于表示.designer文件的模板/生成文件关系的可视化隐喻相同。

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.

如果在.tt文件的“属性”中查找,您会看到它正在使用列为“ TextTemplatingFileGenerator”的CustomTool。 这个自定义工具的可扩展性点就是XSD和WSDL代码生成器之类的东西如何挂钩以生成其工件。

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.

T4可以生成任何类型的文本文件,而不仅仅是代码。 随你喜欢。 您可以如上所述在项目中使用它,也可以从命令行调用T4

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:

除了Kzu克拉里乌斯(Clarius)的亲朋好友之外,罗布(Rob)注意到奥列格塞奇(Oleg Sych)拥有大量的T4资源。 他有一些很棒的教程:

Here's some of the links from Oleg's blog.

这是Oleg博客一些链接

Oleg also has a CodePlex project called T4 Toolbox that is a library of T4 templates that get added to File | New Item.

Oleg还有一个名为T4 Toolbox的CodePlex项目,该项目是T4模板的库,这些库已添加到File | 新物品。

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.

另外,请查看Damien Guard的T4模板,这些模板是LINQ to SQL生成的代码的批发替代品。 这是一个示例,其中我将Damien的T4模板用于示例Chinook数据库

image

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.

注意,我已经将.tt文件命名为与.dbml相同,因此Damian的代码可以找到它。 我还继续让原始的LINQ to SQL生成它的.designer.cs文件,但使该文件的Build Action为“ None”,因此它永远不会被编译。 这有效地使达米安的代码负责。

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.

这是一个截图,显示了一些Damian的T4模板,该模板使用了从Clairus T4 Visual Studio免费下载的语法突出显示。 如果我付钱给他们,我也会在代码块获得智能感知和语法高亮显示。 它看起来很像ASP.NET Web窗体,Velocity或任何模板语言。 代码块是您的逻辑所在,代码块之外是您想要生成的任何内容的模板。 注意Damien如何获取输入并设置输出。 您拥有完全的控制权,可以从文件系统中读取文件,也可以从项目中读取文件,等等。他还设置了输出扩展名。 我自己喜欢使用.g.cs或.g.vb。 在此示例中,他生成的文件是Chinook.genic.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.

我特别喜欢Damien的示例,因为他将LINQ的某些部分(通用模式,设计器和dbml文件)替换为他不喜欢的LINQ(生成的代码),同时保留了他的部分(常规模式,设计器和dbml文件。)。修复它。

Plus, it all works in Visual Studio without installing anything.

另外,它们都可以在Visual Studio中运行,而无需安装任何程序

image

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!

如果您正在执行Code Gen或正在考虑它,请查看T4,因为它是一个不错的起点。 另外,在我在Corillian工作的许多年里,我经常浏览并在CodeSmith上呼吸它,以搜索“ Code Generation”。 玩得开心!

翻译自: https://www.hanselman.com/blog/t4-text-template-transformation-toolkit-code-generation-best-kept-visual-studio-secret

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值