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.

 
   
  1. ?  
  2. AppSettings["ConnectionString"].ToString();   
  3. if (string.IsNullOrEmpty(connectionString))   
  4. {   
  5. MessageBox.Show(this, "Connection String is requested,in app configuration.",   
  6.  
  7. ?  
  8. "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);   
  9. Application.Exit();   
  10. }   
  11. DatabaseSchema db = new DatabaseSchema(provider, connectionString);   
  12. lboxTables.Items.AddRange(db.Tables.ToArray());   
  13. }   
  14.  
  15. ?  
  16.         public CodeCompileUnit GenTableCompilerUnit(TableSchema item)   
  17. {   
  18. if (item == null)   
  19. throw new ArgumentNullException("item");   
  20. CodeTypeDeclaration tableClass = new CodeTypeDeclaration();   
  21. tableClass.Attributes = MemberAttributes.Public | MemberAttributes.Final;   
  22. tableClass.Name = item.Name;   
  23.  
  24. ?  
  25.             foreach (var col in item.Columns)   
  26. {   
  27. CodeMemberField field = new CodeMemberField(   
  28.  
  29. ?  
  30. new CodeTypeReference(col.SystemType), "_" + col.Name);   
  31. CodeMemberProperty property = new CodeMemberProperty();   
  32. property.Name = col.Name;   
  33. property.Attributes = MemberAttributes.Public | MemberAttributes.Final;   
  34. property.Type = new CodeTypeReference(col.SystemType);   
  35. property.SetStatements.Add(new CodeAssignStatement(   
  36.  
  37. ?  
  38. new CodeFieldReferenceExpression(new CodeThisReferenceExpression(),   
  39. field.Name), new CodePropertySetValueReferenceExpression()));   
  40. property.GetStatements.Add(new CodeMethodReturnStatement(   
  41.  
  42. ?  
  43. new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), field.Name)));   
  44. tableClass.Members.Add(field);   
  45. tableClass.Members.Add(property);   
  46. }   
  47. CodeNamespace nspace = new CodeNamespace(item.Database.Name);   
  48. nspace.Imports.Add(new CodeNamespaceImport("System"));   
  49. nspace.Types.Add(tableClass);   
  50. CodeCompileUnit unit=new CodeCompileUnit();   
  51. unit.Namespaces.Add(nspace);   
  52. return unit;   
  53. }   
  54. public string GenTanleCodeFromCompilerUnit(CodeCompileUnit unit, string language)   
  55. {   
  56. CodeGeneratorOptions option=new CodeGeneratorOptions();   
  57. option.BlankLinesBetweenMembers =true;   
  58. option.BracingStyle ="c";   
  59. option.ElseOnClosing=true;   
  60. option.IndentString="    ";   
  61. StringBuilder sb=new StringBuilder();   
  62. System.IO.StringWriter sw=new System.IO.StringWriter(sb);   
  63. CodeDomProvider.CreateProvider(language).GenerateCodeFromCompileUnit(unit, sw, option);   
  64. sw.Close();   
  65. return sb.ToString();   
  66. }   
  67.  
  68. ?  
        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;
}
}
}
}
}
 

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

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

CodeDom代码下载;






 本文转自 破狼 51CTO博客,原文链接:

http://blog.51cto.com/whitewolfblog/834669

,如需转载请自行联系原作者


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值