C # code generation tool: Hello world for the first time in a Text Template

Source: Internet
Author: User

Address: http://www.cnblogs.com/huangcong/archive/2011/01/08/1930553.html

 

 

C # Code Generation Tool: The Text Template first experiences the Text Template (also called T4) of Hello world in Vs, which gives me a bit of codesmith feeling, it is also a method for batch generation of code by template and logic code mixing, but there is not much information about T4, and there is no smart awareness of the code written in the T4 template, but I would like to introduce some friends who may use it. Here are some simple examples to try it out. Of course, we should first start with the simplest Hello, world! Start ~

 

1. Hello, world! 1) create a new project. Right-click-- New-- Text Template , Named Newtemplate. TT:

 

2) After the creation is successful, the following two files will be generated in the project folder:

 

3) double-click Newtemplate. TT File with the following content:

 

4) Add a string under these texts" Hello, world! ", And save:

 

5) in the hosts file, you can see the same content:

Haha, a simple hello, world! Well done. Of course, this function is not worth moving to the table. Let's take a look at how to use it to dynamically generate text ~

 

Ii. dynamically generate code 1) Add the following code based on the original text:
  <  #  For  (  Int  I  =    0  ; I  <  10  ; I  ++  ){#  >  
I am a number: < # = I # > , My square is: < # = I * I # >
< #}# >

 

2.) Now, check newtemplate.txt. You can see the following content. Haha, isn't it like codesmith?

 

Summary: Well, the above two examples can help you get started. I will continue to tell you about other functions of the Text Template. Please pay attention to this article. Article Yes. Please click "recommended ~

 

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.