MENU service case
 Website construction website design Beijing website construction high-end website production company Shangpin China
We create by embracing change
360 ° brand value__
simplified Chinese character
Simplified Chinese English

Shangpin China Joins Hands with Beisheng Internet to Create a New Chapter in Website Construction

Type: Shangpin Dynamic Learn more

[Beijing website production] NET programmers should not miss such a powerful T4 template

Source: Shangpin China | Type: website encyclopedia | Time: October 20, 2011

Powerful T4 text template

I hope you can at least realize that even if there is an automatic code generator, changes in database structure and other factors will bring us a lot of trouble. The benefits of putting the. tt file directly into the project need not be mentioned. There are special business requirements, which are implemented with the partial class in. to avoid overwriting the handwritten code when the template file automatically updates the code. ( Beijing website production )

What's the use of T4 text template? When will it be used?

Take a look at the following example and you will understand:

Create a new console application, delete all the files in it, create a new Program.tt file, and enter the following:

  1. < #@ template  language ="C#" debug ="true"# >  
  2. < #@ output  extension =".cs" encoding ="utf-8"# >  
  3. < #  
  4. stringClassName  =  "Program" ;  
  5. # > stringClassName  =  "Program" ;  
  6. # >  
  7. namespaceHelloAngel  
  8. {  
  9. usingSystem;  
  10. class < #=ClassName # >  
  11. {  
  12. staticvoidMain(string[] args)  
  13.  {  
  14. Console. WriteLine("Hello Angel");  
  15. }  
  16. }  

After saving, we will see this style in Solution Explorer:

The Program.cs file is generated by our Program.tt file, and its contents are as follows:

  1. namespaceHelloAngel  
  2. {  
  3. usingSystem;  
  4. classProgram  
  5. {  
  6. staticvoidMain( string [] args)  
  7. {  
  8. Console. WriteLine( "Hello Angel" );  
  9. }  
  10. }  

Look at the following two commands:

  1. < #@ template  language ="C#" debug ="true"# >  
  2. < #@ output  extension =".cs" encoding ="utf-8"# >  

The language in the<# @ template #>instruction specifies the language used by our template to run, which can be VB or C #. Debug declares whether to use debugging. If debugging is not available, it can be set to false

The extension in the<# @ output #>directive specifies the extension of the output file, and encoding specifies the encoding scheme of the output file.

That is to say, we can use C # language to write the logic in this template file. The template will output a file with the same name as the template file, the extension is. cs, and the code is utf-8.

Now let's take a look at the syntax of the instructions:

The syntax of the directive is as follows:

  1. < #@ DirectiveName [ AttributeName  =  "AttributeValue" ] ...  # >  

All attribute values must be enclosed in double quotation marks. If the value itself contains quotation marks, they must be escaped with characters.

A directive is usually the first element in a template file or included file. They should not be placed in code blocks<#... #> Should not be placed in the class function block<#+... #> After.

T4 template instruction

<#@ template [language="VB"] [hostspecific="true"] [debug="true"] [inherits="templateBaseClass"] [culture="code"] [compilerOptions="options"] #>

T4 parameter command

<#@ parameter type="Full.TypeName"name="ParameterName"#>

T4 output command

<#@ output extension=".fileNameExtension"[encoding="encoding"] #>

T4 Assembly Instruction

<#@ assembly name="[assembly strong name|assembly file name]"#>

T4 import instruction

<#@ import namespace="namespace"#>

T4 contains instructions

<#@ include file="filePath"#>

After reading the above instructions, try to write a template file to generate some classes for practice:

  1. < #@ template  language ="C#" debug ="true"# >  
  2. < #@ output  extension =".cs" encoding ="utf-8"# >  
  3. < #@ import  namespace = "System.Collections.Generic" # >  
  4. < #  
  5. List < List < string > classNames  =  
  6. new List < string > ()  
  7. {  
  8. "Hello",  
  9. "Test"  
  10. };  
  11. List < string > callMethods  =  new  List < string > ();  
  12.  # >  
  13. namespace HelloAngel  
  14.  {  
  15. using System;  
  16. < #  
  17. foreach (string className in classNames)  
  18. {  
  19. callMethods. Add(string.Format("{0}.Show();", className));  
  20. # > foreach (string className in classNames)  
  21. {  
  22. callMethods. Add(string.Format("{0}.Show();", className));  
  23. # >  
  24. class  < #=className # >  
  25. {  
  26. ///  < summary >  
  27. ///  < #=className # > Notes for
  28. ///  summary >  
  29. public static void Show()  
  30. {  
  31. Console. WriteLine("ClassName: < #=className # > ");  
  32. }  
  33. }  
  34. < #  
  35. # > }  
  36. # >  
  37. class Program  
  38. {  
  39. ///  < summary >  
  40. ///Comments for the entry method
  41. ///  summary >  
  42. static void Main(string[] args)  
  43. {  
  44. < #  
  45. foreach (string callMethod in callMethods)  
  46. {  
  47. # > foreach (string callMethod in callMethods)  
  48. {  
  49. # >  
  50. //  < #=callMethod # > Call
  51. < #=callMethod # >  
  52.   < #  
  53. }  
  54. # > }  
  55. # >  
  56. }  
  57. }  

Our general projects are divided into three simple layers. With some experience above, you should think. If only three class. tt files were written, it would be exciting to automatically maintain the entire three layers for us! Indeed, I did a project to share with you:

Look at the whole project now:

There are more than three projects here. I added a UnitTest project and an Adapters project. It's not hard to see the project name. The UnitTest project is a test project. The Adapters project is an entity adapter project. Leave them alone.

With just such a few things, the erection of the third floor has been completed, but it is still in Paramter mode. There is no support for stored procedures. If no one accompanies me, I will add support for stored procedures.

When the project was built, it was built on. net4. Here I changed it to. net2.0. However, I only install visual studio 2010. If you use Visual Studio 2008, please correct the version number in the project file by yourself. There are many methods on the Internet. I won't talk too much.

label: Beijing website production High end website construction

Source Statement: This article is original or edited by Shangpin China's editors. If it needs to be reproduced, please indicate that it is from Shangpin China. The above contents (including pictures and words) are from the Internet. If there is any infringement, please contact us in time (010-60259772).
TAG label:

What if your website can increase the number of conversions and improve customer satisfaction?

Make an appointment with a professional consultant to communicate!

* Shangpin professional consultant will contact you as soon as possible

Disclaimer

Thank you very much for visiting our website. Please read all the terms of this statement carefully before you use this website.

1. Part of the content of this site comes from the network, and the copyright of some articles and pictures involved belongs to the original author. The reprint of this site is for everyone to learn and exchange, and should not be used for any commercial activities.

2. This website does not assume any form of loss or injury caused by users to themselves and others due to the use of these resources.

3. For issues not covered in this statement, please refer to relevant national laws and regulations. In case of conflict between this statement and national laws and regulations, the national laws and regulations shall prevail.

4. If it infringes your legitimate rights and interests, please contact us in time, and we will delete the relevant content at the first time!

Contact: 010-60259772
E-mail: [email protected]

Communicate with professional consultants now!

  • National Service Hotline

    400-700-4979

  • Beijing Service Hotline

    010-60259772

Please be assured to fill in the information protection
Online consultation

Disclaimer

Thank you very much for visiting our website. Please read all the terms of this statement carefully before you use this website.

1. Part of the content of this site comes from the network, and the copyright of some articles and pictures involved belongs to the original author. The reprint of this site is for everyone to learn and exchange, and should not be used for any commercial activities.

2. This website does not assume any form of loss or injury caused by users to themselves and others due to the use of these resources.

3. For issues not covered in this statement, please refer to relevant national laws and regulations. In case of conflict between this statement and national laws and regulations, the national laws and regulations shall prevail.

4. If it infringes your legitimate rights and interests, please contact us in time, and we will delete the relevant content at the first time!

Contact: 010-60259772
E-mail: [email protected]