您好,欢迎访问一九零五行业门户网

CodeSmith实用技巧(八):生成的代码输出到文件中

在 codesmith 中,要把生成的代码文件输出到文件中,你需要在自己的模版中继承 outputfilecodetemplate 类。 % @codetemplatelanguage = c# targetlanguage = c# inherits = outputfilecodetemplate description = buildcustomaccesscode. %
在codesmith中,要把生成的代码文件输出到文件中,你需要在自己的模版中继承outputfilecodetemplate类。
@ codetemplate language=c# targetlanguage=c# inherits=outputfilecodetemplate description=build custom access code. %>
@ assembly name=codesmith.basetemplates %>
outputfilecodetemplate主要做两件事情:
1.它添加一个名为outputfile的属性到你的模版中,该属性要求你必须选择一个文件;
2.模版重载了方法onpostrender(),在codesmith生成代码完成后把相应的内容写入到指定的文件中去。
如果想要自定义outputfile属性弹出的保存文件对话框,你需要在你的模版中重载outputfile属性。例如:你希望用户选择一个.cs文件来保存生成的代码,需要在你的模版中添加如下代码:
script runat=template>
// override the outputfile property and assign our specific settings to it.
[filedialog(filedialogtype.save, title=select output file, filter=c# files (*.cs)|*.cs, defaultextension=.cs)]
public override string outputfile
{
    get {return base.outputfile;}
    set {base.outputfile = value;}
}
script>
其它类似信息

推荐信息