惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

C
Cisco Blogs
Cyberwarzone
Cyberwarzone
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
SecWiki News
SecWiki News
Martin Fowler
Martin Fowler
T
Tor Project blog
N
Netflix TechBlog - Medium
C
Cybersecurity and Infrastructure Security Agency CISA
V
Vulnerabilities – Threatpost
V
Visual Studio Blog
GbyAI
GbyAI
PCI Perspectives
PCI Perspectives
D
DataBreaches.Net
Jina AI
Jina AI
H
Heimdal Security Blog
云风的 BLOG
云风的 BLOG
P
Privacy International News Feed
A
About on SuperTechFans
J
Java Code Geeks
美团技术团队
H
Hackread – Cybersecurity News, Data Breaches, AI and More
N
News | PayPal Newsroom
有赞技术团队
有赞技术团队
MyScale Blog
MyScale Blog
博客园 - 司徒正美
C
Check Point Blog
T
Threat Research - Cisco Blogs
Attack and Defense Labs
Attack and Defense Labs
宝玉的分享
宝玉的分享
AI
AI
Simon Willison's Weblog
Simon Willison's Weblog
C
Cyber Attacks, Cyber Crime and Cyber Security
I
Intezer
P
Proofpoint News Feed
Blog — PlanetScale
Blog — PlanetScale
Apple Machine Learning Research
Apple Machine Learning Research
Hugging Face - Blog
Hugging Face - Blog
The Last Watchdog
The Last Watchdog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Vercel News
Vercel News
I
InfoQ
阮一峰的网络日志
阮一峰的网络日志
Cisco Talos Blog
Cisco Talos Blog
W
WeLiveSecurity
Hacker News: Ask HN
Hacker News: Ask HN
Recent Commits to openclaw:main
Recent Commits to openclaw:main
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
D
Docker
博客园 - Franky
Security Archives - TechRepublic
Security Archives - TechRepublic

博客园 - HelloSnoopy

写了个用一个文件定义样式的DataGrid 一个最基本的WebService+Flash调用的实例 [转载]如何奔向程序员打工的出头之日? 一定要讲给孩子们的20个小故事 人脉对职业生涯最重要 (转) 爱情的10种样子,送给懂爱的朋友们 装了Enterprise Library.NET 200501release C#判断是否为数字的最好速度! 开源的 BugTracker.net 错误跟踪管理系统 Enjoy new year of 2005! Flash AS2 用EventDispatcher广播事件 设计模式随笔-让众口不再难调 [专题]MVC构架模式 使用Microsoft Application Block之Cache Application 用一条SQL完成数据表的行统计 如何使IFrame的长宽与内容自动适应大小 为Project提供一个统一风格的DataGrid 用于读取树形任一节点下所有级别子节点的SqlServer UDF asp.net forums中定时器的应用
CodeSmith使用心得
HelloSnoopy · 2004-12-27 · via 博客园 - HelloSnoopy

  come from:http://www.cnblogs.com/elevenwolf/archive/2004/12/01/71391.aspx 

  代码生成器大家都用过吧,CodeSmith我想大家也不陌生。最近就用CodeSmith生成了自己的实体类。当然你也可以用它来生成HTML页面以及文档。

下面我就来说一说我生成实体类的步骤:
一、首先介绍一下CodeSmith 2.6CodeSmith 2.6安装后,会有3exe:
1.       CodeSmith.exe即为CodeSmith Explorer,可视化生成代码工具,免费,没有时间限制
2.       CodeSmithConsole.exe Command模式下运行,免费,没有时间限制
3.       CodeSmithStudio.exe编辑模板的工具的工具,可以校验模板语法是否正确。试用30天。
CodeSmith模板支持C#VB.Net语法,可以调用.net类库,语法和.net基本上是一样的。  

二、制作模板
1.在这里我选择了C#作为模板的开发语言。
<%@ CodeTemplate Language="C#" TargetLanguage="Text" Description="Template description here." %> 
2.要生成数据库的实体类,数据连接和表名不可缺少的。在CodeSmith中通过SchemaExplorer.dll来帮你和数据库打交道。
<%@ Assembly Name="SchemaExplorer" %>
<%@ Import Namespace="SchemaExplorer" %>
在模板中导入SchemaExplorer
然后设置一系列的需要传入的参数:
<%@ Property Name="DataBase" Type="SchemaExplorer.DatabaseSchema" Category="Context" Description="数据库连接." %>
<%@ Property Name="TableName" Type="System.String" Default="" Optional="False" Category="" Description="表名" %> 

3.CodeSmith模板脚本格式:
<script runat="template"> 
       CodeTemplateRule rule=new CodeTemplateRule();
</script>  

或者:
/// <summary> 
    /// 作用:<%= Description %> 
    /// 作者:<%= Author %> 
    /// 日期:<%= DateTime.Now.ToString() %>
///</summary>  

4.我自己写了一个dll来存放自己的函数 CodeTemplateRule.dll ,其中引用到了SchemaExplorer,举个例子:

这段代码的含义就是取数据库中某张表所有列的集合。

Demo文件:
模板文件:entity.cst
自己写的.net程序集:CodeTemplateRule.cs
生成后的代码效果:AccountBookEntity.cs  

三、运行
1.       CodeSmith.exe运行模板,CodeSmith会弹出对话框来你来填写你的参数。
2.       CodeSmithConsole.exe运行模板,参数可以放在xml文件中。例如:
<?xml version="1.0" encoding="utf-8" ?>

<codeSmith> 

       <propertySets>

              <propertySet>

                     <property name="SampleStringProperty1">string111111111</property>

                     <property name="SampleBooleanProperty1">false</property>         

              </propertySet>         

       </propertySets>

</codeSmith>

然后用命令执行:

3.       CodeSmithStudio.exe运行模板  

总结:

代码生成器给我们编程工作带来了很大的便利,不需要做很多重复性的工作。