CodeDom六--实体类生成示例
2010-07-08 10:15 破狼 阅读(4196) 评论(25) 编辑 收藏 举报CodeDom这个东西个人觉得知识点不多,前几个续节写的已差不多了。在这节将演示一个CodeDom示例:
数据库实体类的生成。这里先声明在如今的CodeSmith或者是T4模板中实现这些都很简单,并且更实用,在这
里只是一个CodeDom示例,为了演示CodeDom。
在代码中位了简单、简化数据库数据信息的提取,引用了CodeSimth的SchemaExplorer.dll和SchemaExplorer.
SqlSchemaProvider.dll。可以在Demo中的CodeSimth目录下找到。
先贴上代码,需要讲解的东西没有什么:
456789101112131415161718192021using
System;
using
System.Text;
using
System.Windows.Forms;
using
SchemaExplorer;
using
System.CodeDom;
using
System.CodeDom.Compiler;
namespace
CodeDomDemo
{
public
partial
class
Form1 : Form
{
public
Form1()
{
InitializeComponent();
}
private
void
Form1_Load(
object
sender, EventArgs e)
{
IDbSchemaProvider provider =
new
SqlSchemaProvider();
string
connectionString = System.Configuration.ConfigurationManager.
4567AppSettings[
"ConnectionString"
].ToString();
if
(
string
.IsNullOrEmpty(connectionString))
{
MessageBox.Show(
this
,
"Connection String is requested,in app configuration."
,
456789"Error"
, MessageBoxButtons.OK, MessageBoxIcon.Error);
Application.Exit();
}
DatabaseSchema db =
new
DatabaseSchema(provider, connectionString);
lboxTables.Items.AddRange(db.Tables.ToArray());
}
45678910
public
CodeCompileUnit GenTableCompilerUnit(TableSchema item)
{
if
(item ==
null
)
throw
new
ArgumentNullException(
"item"
);
CodeTypeDeclaration tableClass =
new
CodeTypeDeclaration();
tableClass.Attributes = MemberAttributes.Public | MemberAttributes.Final;
tableClass.Name = item.Name;
456
foreach
(
var
col
in
item.Columns)
{
CodeMemberField field =
new
CodeMemberField(
456789new
CodeTypeReference(col.SystemType),
"_"
+ col.Name);
CodeMemberProperty property =
new
CodeMemberProperty();
property.Name = col.Name;
property.Attributes = MemberAttributes.Public | MemberAttributes.Final;
property.Type =
new
CodeTypeReference(col.SystemType);
property.SetStatements.Add(
new
CodeAssignStatement(
456new
CodeFieldReferenceExpression(
new
CodeThisReferenceExpression(),
field.Name),
new
CodePropertySetValueReferenceExpression()));
property.GetStatements.Add(
new
CodeMethodReturnStatement(
456789101112131415161718192021222324252627new
CodeFieldReferenceExpression(
new
CodeThisReferenceExpression(), field.Name)));
tableClass.Members.Add(field);
tableClass.Members.Add(property);
}
CodeNamespace nspace =
new
CodeNamespace(item.Database.Name);
nspace.Imports.Add(
new
CodeNamespaceImport(
"System"
));
nspace.Types.Add(tableClass);
CodeCompileUnit unit=
new
CodeCompileUnit();
unit.Namespaces.Add(nspace);
return
unit;
}
public
string
GenTanleCodeFromCompilerUnit(CodeCompileUnit unit,
string
language)
{
CodeGeneratorOptions option=
new
CodeGeneratorOptions();
option.BlankLinesBetweenMembers =
true
;
option.BracingStyle =
"c"
;
option.ElseOnClosing=
true
;
option.IndentString=
" "
;
StringBuilder sb=
new
StringBuilder();
System.IO.StringWriter sw=
new
System.IO.StringWriter(sb);
CodeDomProvider.CreateProvider(language).GenerateCodeFromCompileUnit(unit, sw, option);
sw.Close();
return
sb.ToString();
}
45678910111213141516171819202122
private
void
lboxTables_SelectedIndexChanged(
object
sender, EventArgs e)
{
int
index = lboxTables.SelectedIndex;
if
(index > -1 && index < lboxTables.Items.Count)
{
object
obj = lboxTables.Items[index];
if
(obj
is
TableSchema)
{
CodeCompileUnit unit= GenTableCompilerUnit(obj
as
TableSchema);
string
language=
"c#"
;
if
(!
string
.IsNullOrEmpty(
this
.toolStripComboBox1.Text))
language=
this
.toolStripComboBox1.Text;
string
code = GenTanleCodeFromCompilerUnit(unit,language);
this
.rtbCode.Text = code;
}
}
}
}
}
4运行示图:c#图
![]()
vb图:
代码比较简单界面也做得太简洁了,呵呵导出文件都没有,我觉得没有什么必要讲解的,如果有什么不懂的或者是写的不对
不好的,请留言,我会尽快回复。
作者:破 狼
出处:http://www.cnblogs.com/whitewolf/
本文版权归作者,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。该文章也同时发布在我的独立博客中-个人独立博客、博客园--破狼和51CTO--破狼。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架