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秘密的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/597423

相关文章

Java调用DeepSeek API的最佳实践及详细代码示例

《Java调用DeepSeekAPI的最佳实践及详细代码示例》:本文主要介绍如何使用Java调用DeepSeekAPI,包括获取API密钥、添加HTTP客户端依赖、创建HTTP请求、处理响应、... 目录1. 获取API密钥2. 添加HTTP客户端依赖3. 创建HTTP请求4. 处理响应5. 错误处理6.

Java数字转换工具类NumberUtil的使用

《Java数字转换工具类NumberUtil的使用》NumberUtil是一个功能强大的Java工具类,用于处理数字的各种操作,包括数值运算、格式化、随机数生成和数值判断,下面就来介绍一下Number... 目录一、NumberUtil类概述二、主要功能介绍1. 数值运算2. 格式化3. 数值判断4. 随机

使用C++将处理后的信号保存为PNG和TIFF格式

《使用C++将处理后的信号保存为PNG和TIFF格式》在信号处理领域,我们常常需要将处理结果以图像的形式保存下来,方便后续分析和展示,C++提供了多种库来处理图像数据,本文将介绍如何使用stb_ima... 目录1. PNG格式保存使用stb_imagephp_write库1.1 安装和包含库1.2 代码解

C语言中自动与强制转换全解析

《C语言中自动与强制转换全解析》在编写C程序时,类型转换是确保数据正确性和一致性的关键环节,无论是隐式转换还是显式转换,都各有特点和应用场景,本文将详细探讨C语言中的类型转换机制,帮助您更好地理解并在... 目录类型转换的重要性自动类型转换(隐式转换)强制类型转换(显式转换)常见错误与注意事项总结与建议类型

Java中基于注解的代码生成工具MapStruct映射使用详解

《Java中基于注解的代码生成工具MapStruct映射使用详解》MapStruct作为一个基于注解的代码生成工具,为我们提供了一种更加优雅、高效的解决方案,本文主要为大家介绍了它的具体使用,感兴趣... 目录介绍优缺点优点缺点核心注解及详细使用语法说明@Mapper@Mapping@Mappings@Co

Python实现视频转换为音频的方法详解

《Python实现视频转换为音频的方法详解》这篇文章主要为大家详细Python如何将视频转换为音频并将音频文件保存到特定文件夹下,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1. python需求的任务2. Python代码的实现3. 代码修改的位置4. 运行结果5. 注意事项

使用Python实现图片和base64转换工具

《使用Python实现图片和base64转换工具》这篇文章主要为大家详细介绍了如何使用Python中的base64模块编写一个工具,可以实现图片和Base64编码之间的转换,感兴趣的小伙伴可以了解下... 简介使用python的base64模块来实现图片和Base64编码之间的转换。可以将图片转换为Bas

C#使用DeepSeek API实现自然语言处理,文本分类和情感分析

《C#使用DeepSeekAPI实现自然语言处理,文本分类和情感分析》在C#中使用DeepSeekAPI可以实现多种功能,例如自然语言处理、文本分类、情感分析等,本文主要为大家介绍了具体实现步骤,... 目录准备工作文本生成文本分类问答系统代码生成翻译功能文本摘要文本校对图像描述生成总结在C#中使用Deep

vscode保存代码时自动eslint格式化图文教程

《vscode保存代码时自动eslint格式化图文教程》:本文主要介绍vscode保存代码时自动eslint格式化的相关资料,包括打开设置文件并复制特定内容,文中通过代码介绍的非常详细,需要的朋友... 目录1、点击设置2、选择远程--->点击右上角打开设置3、会弹出settings.json文件,将以下内

SpringBoot项目中Maven剔除无用Jar引用的最佳实践

《SpringBoot项目中Maven剔除无用Jar引用的最佳实践》在SpringBoot项目开发中,Maven是最常用的构建工具之一,通过Maven,我们可以轻松地管理项目所需的依赖,而,... 目录1、引言2、Maven 依赖管理的基础概念2.1 什么是 Maven 依赖2.2 Maven 的依赖传递机