CodeSmith中,要把生成的代码文件输出到文件中,你需要在自己的模版中继承OutputFileCodeTemplate类。

CodeSmith实用技巧(八):生成的代码输出到文件中<%@ CodeTemplate Language="C#" TargetLanguage="C#" Inherits="OutputFileCodeTemplate" Description="Build custom access code." %>
CodeSmith实用技巧(八):生成的代码输出到文件中
<%@ Assembly Name="CodeSmith.BaseTemplates" %>

OutputFileCodeTemplate主要做两件事情:

1.它添加一个名为OutputFile的属性到你的模版中,该属性要求你必须选择一个文件;

2.模版重载了方法OnPostRender(),在CodeSmith生成代码完成后把相应的内容写入到指定的文件中去。

如果想要自定义OutputFile属性弹出的保存文件对话框,你需要在你的模版中重载OutputFile属性。例如:你希望用户选择一个.cs文件来保存生成的代码,需要在你的模版中添加如下代码:

CodeSmith实用技巧(八):生成的代码输出到文件中<script runat="template">
CodeSmith实用技巧(八):生成的代码输出到文件中
// Override the OutputFile property and assign our specific settings to it.
CodeSmith实用技巧(八):生成的代码输出到文件中
[FileDialog(FileDialogType.Save, Title="Select Output File", Filter="C# Files (*.cs)|*.cs", DefaultExtension=".cs")]
CodeSmith实用技巧(八):生成的代码输出到文件中
public override string OutputFile
>

相关文章:

  • 2022-03-04
  • 2022-12-23
  • 2021-11-20
  • 2022-12-23
  • 2022-12-23
  • 2021-08-11
  • 2022-01-16
  • 2021-12-30
猜你喜欢
  • 2022-01-18
  • 2021-07-09
  • 2022-02-17
  • 2021-10-15
  • 2021-07-25
相关资源
相似解决方案