![]() |
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 the sub-templates from a master template and use the RenderToFile method to output each sub-template. |
1
public void RenderMyAspxCs(TableSchema table,string strTableName,string nameSpaceStr)
2
{
3
CodeTemplate subTemplate=null;
4
subTemplate=CreateSubTemplate(@"..\Web.UI\MyAspx.cs.cst"); //创建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); //输出到文件
14
// Response.WriteLine(FoldName+"\\"+nameSpaceStr+"\\Manager.Aspx.cs"); //得示信息
15
}
16
}
17
18
public CodeTemplate CreateSubTemplate(string SubTempName)
19
{
20
CodeTemplate _SubTemplate=null; //定义子模块
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

2

3

4

5

6

7

8

9

10

11

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38
