Use T4 templates in Visual Studio to generate code

Source: Internet
Author: User
Before watching the video of Hilton giesenow (How do I: create and use T4 templates.), I realized how easy it was to use T4 in Visual Studio 2008. If you are interested in using T4 templates in Visual Studio, make sure that you have downloaded and installed the T4 editor. Code To quickly create a T4 template, create a console application in Visual Studio 2008, select "Add new project" and select "Text File ". When naming a file, make sure that you use the "TT" extension. As you can see below, I added a text file named mytemplate. TT. Visual Studio automatically creates the mytemplate. CS file for you. Write some T4 template tags, which is basically inline ASP, In the mytemplate. TT file as such: In mytemplate. compile the following mark in TT (similar to ASP) <# @ template Language = "C #" #> using system; public class testclass {public void writetoconsole () {<# For (INT I = 0; I <10; I ++) {#> console. writeline (<# = I. tostring () #>); <#}#>} when you save the file, the following C # code is automatically generated to mytemplate. CS file: using system; public class testclass {public void writ Etoconsole () {console. writeline (0); console. writeline (1); console. writeline (2); console. writeline (3); console. writeline (4); console. writeline (5); console. writeline (6); console. writeline (7); console. writeline (8); console. writeline (9);} in program. write the code in the CS file and run the generated code using system; namespace consoleapplication5 {internal class program {Private Static void main (string [] ARGs) {New testclass (). writetoc Onsole (); console. Readline () ;}} you can run it and use the code generated by T4 template in Visual Studio 2008. There is no strong reason to use T4 template to generate code in the case of generator and O/R mapper. However, at least he provides us with a choice. Hope to help Denny. Dong
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.