CodeDom六--实体类生成示例

简介:

      CodeDom这个东西个人觉得知识点不多,前几个续节写的已差不多了。在这节将演示一个CodeDom示例:

数据库实体类的生成。这里先声明在如今的CodeSmith或者是T4模板中实现这些都很简单,并且更实用,在这

里只是一个CodeDom示例,为了演示CodeDom。

      在代码中位了简单、简化数据库数据信息的提取,引用了CodeSimth的SchemaExplorer.dll和SchemaExplorer.

SqlSchemaProvider.dll。可以在Demo中的CodeSimth目录下找到。

      先贴上代码,需要讲解的东西没有什么:

using 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.
AppSettings["ConnectionString"].ToString();
if (string.IsNullOrEmpty(connectionString))
{
MessageBox.Show(this, "Connection String is requested,in app configuration.",
 "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
Application.Exit();
}
DatabaseSchema db = new DatabaseSchema(provider, connectionString);
lboxTables.Items.AddRange(db.Tables.ToArray());
}
        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;
            foreach (var col in item.Columns)
{
CodeMemberField field = new CodeMemberField(
new 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(
new CodeFieldReferenceExpression(new CodeThisReferenceExpression(),
field.Name), new CodePropertySetValueReferenceExpression()));
property.GetStatements.Add(new CodeMethodReturnStatement(
new 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();
}
        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;
}
}
}
}
}
运行示图:c#图
imagec#图

vb图:

imagevb图

               代码比较简单界面也做得太简洁了,呵呵导出文件都没有,我觉得没有什么必要讲解的,如果有什么不懂的或者是写的不对

       不好的,请留言,我会尽快回复。

CodeDom代码下载;



本文转自破狼博客园博客,原文链接:http://www.cnblogs.com/whitewolf/archive/2010/07/08/1773361.html,如需转载请自行联系原作者

目录
相关文章
|
5天前
|
存储 人工智能 安全
AI 越智能,数据越危险?
阿里云提供AI全栈安全能力,为客户构建全链路数据保护体系,让企业敢用、能用、放心用
|
8天前
|
域名解析 人工智能
【实操攻略】手把手教学,免费领取.CN域名
即日起至2025年12月31日,购买万小智AI建站或云·企业官网,每单可免费领1个.CN域名首年!跟我了解领取攻略吧~
|
7天前
|
数据采集 人工智能 自然语言处理
3分钟采集134篇AI文章!深度解析如何通过云无影AgentBay实现25倍并发 + LlamaIndex智能推荐
结合阿里云无影 AgentBay 云端并发采集与 LlamaIndex 智能分析,3分钟高效抓取134篇 AI Agent 文章,实现 AI 推荐、智能问答与知识沉淀,打造从数据获取到价值提炼的完整闭环。
448 93
|
1天前
|
开发者
「玩透ESA」ESA启用和加速-ER在加速场景中的应用
本文介绍三种配置方法:通过“A鉴权”模板创建函数并设置触发器路由;在ESA上配置回源302跟随;以及自定义响应头。每步均配有详细截图指引,帮助开发者快速完成相关功能设置,提升服务安全性与灵活性。
286 2
|
7天前
|
SQL 人工智能 自然语言处理
Geo优化SOP标准化:于磊老师的“人性化Geo”体系如何助力企业获客提效46%
随着生成式AI的普及,Geo优化(Generative Engine Optimization)已成为企业获客的新战场。然而,缺乏标准化流程(Geo优化sop)导致优化效果参差不齐。本文将深入探讨Geo专家于磊老师提出的“人性化Geo”优化体系,并展示Geo优化sop标准化如何帮助企业实现获客效率提升46%的惊人效果,为企业在AI时代构建稳定的流量护城河。
407 156
Geo优化SOP标准化:于磊老师的“人性化Geo”体系如何助力企业获客提效46%
|
7天前
|
数据采集 缓存 数据可视化
Android 无侵入式数据采集:从手动埋点到字节码插桩的演进之路
本文深入探讨Android无侵入式埋点技术,通过AOP与字节码插桩(如ASM)实现数据采集自动化,彻底解耦业务代码与埋点逻辑。涵盖页面浏览、点击事件自动追踪及注解驱动的半自动化方案,提升数据质量与研发效率,助力团队迈向高效、稳定的智能化埋点体系。(238字)
314 158