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

本文主要是介绍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资源。 他有一些很棒的教程:

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

这是Oleg博客的一些链接。

  • How to create a simple T4 template

    如何创建一个简单的T4模板

  • How to use T4 to generate .config files

    如何使用T4生成.config文件

  • How to use T4 to generate Decorator classes

    如何使用T4生成装饰器类

  • How to use T4 to generate CRUD stored procedures

    如何使用T4生成CRUD存储过程

  • How to use T4 to generate strongly-typed navigation class in ASP.NET (by Kirill Chilingarashvili)

    如何在ASP.NET中使用T4生成强类型导航类(作者Kirill Chilingarashvili)

  • How to use T4 to generate strongly-typed AzMan wrapper

    如何使用T4生成强类型的AzMan包装器

  • How to generate multiple outputs from single T4 template

    如何从单个T4模板生成多个输出

  • T4 template for generating ADO.NET Entity Framework Stored Procedures  (by David DeWinter)

    用于生成ADO.NET实体框架存储过程的T4模板(作者David DeWinter)

  • T4 script for generating ADO.NET Entity Framework Views (by ADO.NET team)

    用于生成ADO.NET实体框架视图的T4脚本(由ADO.NET团队提供)

  • T4 template for generating LINQ to SQL Data Context (by Damien Guard)

    用于生成LINQ to SQL数据上下文的T4模板(由Damien Guard撰写)

  • T4 template for generating WiX source files (by New Age Solutions)

    用于生成WiX源文件的T4模板(由New Age Solutions提供)

  • T4 template for generating SQL view from C# enumeration

    T4模板,用于从C#枚举生成SQL视图

  • MSBuild task for transforming T4 templates (by Elton Stoneman)

    用于转换T4模板的MSBuild任务(作者Elton Stoneman)

  • T4 template for generating state machines in C# (by Andrew Matthews)

    用于在C#中生成状态机的T4模板(作者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.

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

这篇关于T4(文本模板转换工具包)代码生成-最佳保存的Visual Studio秘密的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!


原文地址:https://blog.csdn.net/cunfusq0176/article/details/109083242
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.chinasem.cn/article/597423

相关文章

Python实现AVIF图片与其他图片格式间的批量转换

《Python实现AVIF图片与其他图片格式间的批量转换》这篇文章主要为大家详细介绍了如何使用Pillow库实现AVIF与其他格式的相互转换,即将AVIF转换为常见的格式,比如JPG或PNG,需要的小... 目录环境配置1.将单个 AVIF 图片转换为 JPG 和 PNG2.批量转换目录下所有 AVIF 图

详解如何通过Python批量转换图片为PDF

《详解如何通过Python批量转换图片为PDF》:本文主要介绍如何基于Python+Tkinter开发的图片批量转PDF工具,可以支持批量添加图片,拖拽等操作,感兴趣的小伙伴可以参考一下... 目录1. 概述2. 功能亮点2.1 主要功能2.2 界面设计3. 使用指南3.1 运行环境3.2 使用步骤4. 核

Spring Boot 配置文件之类型、加载顺序与最佳实践记录

《SpringBoot配置文件之类型、加载顺序与最佳实践记录》SpringBoot的配置文件是灵活且强大的工具,通过合理的配置管理,可以让应用开发和部署更加高效,无论是简单的属性配置,还是复杂... 目录Spring Boot 配置文件详解一、Spring Boot 配置文件类型1.1 applicatio

Java实现时间与字符串互相转换详解

《Java实现时间与字符串互相转换详解》这篇文章主要为大家详细介绍了Java中实现时间与字符串互相转换的相关方法,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录一、日期格式化为字符串(一)使用预定义格式(二)自定义格式二、字符串解析为日期(一)解析ISO格式字符串(二)解析自定义

在java中如何将inputStream对象转换为File对象(不生成本地文件)

《在java中如何将inputStream对象转换为File对象(不生成本地文件)》:本文主要介绍在java中如何将inputStream对象转换为File对象(不生成本地文件),具有很好的参考价... 目录需求说明问题解决总结需求说明在后端中通过POI生成Excel文件流,将输出流(outputStre

python+opencv处理颜色之将目标颜色转换实例代码

《python+opencv处理颜色之将目标颜色转换实例代码》OpenCV是一个的跨平台计算机视觉库,可以运行在Linux、Windows和MacOS操作系统上,:本文主要介绍python+ope... 目录下面是代码+ 效果 + 解释转HSV: 关于颜色总是要转HSV的掩膜再标注总结 目标:将红色的部分滤

利用Python开发Markdown表格结构转换为Excel工具

《利用Python开发Markdown表格结构转换为Excel工具》在数据管理和文档编写过程中,我们经常使用Markdown来记录表格数据,但它没有Excel使用方便,所以本文将使用Python编写一... 目录1.完整代码2. 项目概述3. 代码解析3.1 依赖库3.2 GUI 设计3.3 解析 Mark

使用Python实现文本转语音(TTS)并播放音频

《使用Python实现文本转语音(TTS)并播放音频》在开发涉及语音交互或需要语音提示的应用时,文本转语音(TTS)技术是一个非常实用的工具,下面我们来看看如何使用gTTS和playsound库将文本... 目录什么是 gTTS 和 playsound安装依赖库实现步骤 1. 导入库2. 定义文本和语言 3

C语言中的数据类型强制转换

《C语言中的数据类型强制转换》:本文主要介绍C语言中的数据类型强制转换方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录C语言数据类型强制转换自动转换强制转换类型总结C语言数据类型强制转换强制类型转换:是通过类型转换运算来实现的,主要的数据类型转换分为自动转换

Python实现常用文本内容提取

《Python实现常用文本内容提取》在日常工作和学习中,我们经常需要从PDF、Word文档中提取文本,本文将介绍如何使用Python编写一个文本内容提取工具,有需要的小伙伴可以参考下... 目录一、引言二、文本内容提取的原理三、文本内容提取的设计四、文本内容提取的实现五、完整代码示例一、引言在日常工作和学