CodeSmith实用技巧(八):生成的代码输出到文件中
生活随笔
收集整理的這篇文章主要介紹了
CodeSmith实用技巧(八):生成的代码输出到文件中
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
在CodeSmith中,要把生成的代碼文件輸出到文件中,你需要在自己的模版中繼承OutputFileCodeTemplate類。<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
<%@?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文件來保存生成的代碼,需要在你的模版中添加如下代碼:
//?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>
轉載于:https://www.cnblogs.com/wayne-ivan/archive/2007/06/15/784412.html
總結
以上是生活随笔為你收集整理的CodeSmith实用技巧(八):生成的代码输出到文件中的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Zen4给力!锐龙7000内存超频真有惊
- 下一篇: 用代理技术实现简单的AOP框架