CodeSmith is a good. Net development auxiliary tool that can greatly improve work efficiency. Recently, I used it for a project, where multiple files must be automatically saved. I checked it online. All I can find is implemented by inheriting Inherits = "OutputFileCodeTemplate". I personally think this method is too troublesome. Later, I checked the online help of CodeSmith 4.1. It is very clear that I can use the RenderToFile OF THE subtemplate. The original Article is as follows:
|
This technique is useful for generating
Multiple identical copies of the same file. When you need to generate multiple
Different files as part of a single
Code-generation process, you should use one sub-template for each file. Call
Sub-templates from a master template and use the rendertofile method to output
Each sub-template. |
The Demo I wrote is as follows: 1 public void RenderMyAspxCs (TableSchema table, string strTableName, string nameSpaceStr)
2 {
3 CodeTemplate subTemplate = null;
4 subTemplate = CreateSubTemplate (@ ".. \ Web. UI \ MyAspx. cs. cst"); // create a template
5 if (subTemplate! = Null)
6 {
7 subTemplate. SetProperty ("CurTable", table );
8 subtemplate. setproperty ("namespaces", "communityoa. Web. Module. OA." + namespacestr );
9 subtemplate. setproperty ("entityname", strtablename );
10 subtemplate. setproperty ("codeentity", strtablename );
11
13 subtemplate. rendertofile (foldname + "\" + namespacestr + "\ My. aspx. cs", true); // output to the file
14 // response. writeline (foldname + "\" + namespacestr + "\ manager. aspx. cs"); // obtain information
15}
16}
17
18 public codetemplate createsubtemplate (string subtempname)
19 {
20 CodeTemplate _ SubTemplate = null; // define a submodule
21 CodeTemplateCompiler compiler = new CodeTemplateCompiler (this. CodeTemplateInfo. DirectoryName + SubTempName );
22 compiler. Compile ();
23
24 if (compiler. Errors. Count = 0)
25 {
26 _ SubTemplate = compiler. CreateInstance ();
27}
28 else
29 {
30 for (int I = 0; I <compiler. Errors. Count; I ++)
31 {
32 Response. WriteLine (compiler. Errors [I]. ToString ());
33}
34}
35
36 return _ SubTemplate;
37}
38