We are not unfamiliar with code generators, which is one of the more used tools in everyday coding. The main function of the general code generator is to generate public or basic code to reduce the workload of the coder, and a good code generator in addition to the production of code, but also the ability to build the project architecture and basic modules, so that developers focus on the core of the business logic, improve coding efficiency and reduce workload.
Now the most used code generation technology in the market is template generation technology, here is recommended a relatively good template generation engine T4 (Text template transformation Toolkit), because VisualStudio itself is also used it. As a very good IDE, presumably T4 can get its favor is enough to explain T4 excellent.
To create a new class library project with VS, right-click Add New item to add a text template
The file suffix is generally. tt is an executable file in VS, and the code in the file is automatically executed when you save it.
The TT template and the script in aspx, the executable code is included in <##>. Text that is not included in <##> is output directly to the destination file as static text.
We directly enter "HELLO,T4" in the space below the file to save a target file with the same name suffix. txt, open the file, you can see the following:
The static text in the TT template is output directly to the file. The use of T4 templates is described in detail in the next article.
So where does the VS use the T4? In fact, most of the files created by the template in VS are using T4. We randomly create a new class, which generates some basic code, which is T4 is the role, but T4 has been working in the background, we tend to ignore its role.
(Note: The default new TT template, VS is not support syntax highlighting and smart hints, for ease of writing and presentation, I installed the syntax highlighting plugin, I am using the T4 Editor, in the VS online template can be downloaded to)
First, the initial knowledge T4 engine