实体类生成器教程:从设计到实现

发布时间:2024-12-01 14:46

从想象到实现的创新乐趣:从概念到产品的实践过程 #生活乐趣# #创新乐趣# #创新实践#

C#实体类生成器

壳壳的后宫 于 2015-05-31 19:50:56 发布

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

     看到牛腩视频的老师讲解了实体类的生成器,可是可惜的牛腩视频里并没有这集的讲解,所以只能上网去寻找一些资料,看了师姐的博客最后还是完成了这个高效的实体类的生成器。

      首先新建一个窗体,在上面脱出我们所需要的一些控件。如图所示:

                      

      然后就是窗体的代码段了:

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;

using System.IO;

using System.Text.RegularExpressions;

namespace DesignEntity

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

private void btnOK_Click(object sender, EventArgs e)

{

string ClassName1 = txtName.Text.Trim();

string ClassExp1 = txtExp.Text.Trim();

string NameSpace1 = txtNameSpace.Text.Trim();

if (ClassName1 .Length ==0)

{

MessageBox.Show("类名不能为空");

return;

}

sfdFile.FileName = ClassName1;

if (sfdFile.ShowDialog ()==DialogResult .OK )

{

FileStream fs = new FileStream(sfdFile.FileName, FileMode.Create, FileAccess.Write);

StreamWriter sw = new StreamWriter(fs, Encoding.Default);

if (radYes.Checked && txtNameSpace.Text.Trim() != null)

{

sw.WriteLine("namespace" + NameSpace1);

sw.WriteLine("{");

}

sw.WriteLine(" public class " + ClassName1);

sw.WriteLine(" {");

foreach (DataGridViewRow Row in fdContent.Rows)

{

if (Row.Cells[0].Value !=null && Row.Cells[0].Value !=null)

{

string propname = Row.Cells[0].Value.ToString();

string type = Row.Cells[1].Value.ToString();

sw.WriteLine(" private " + type + " " + propname + ";");

string propname1 = Regex.Replace(propname, "^_+", "");

string functionName = propname1.Substring(0, 1).ToUpper() + propname1.Substring(1);

sw.WriteLine(" public " + type + " " + functionName);

sw.WriteLine(" {");

sw.WriteLine(" get { return " + propname + "; }");

sw.WriteLine(" set { " + propname + " = value; }");

sw.WriteLine(" }");

}

}

sw.WriteLine("}");

if (radYes .Checked && txtNameSpace .Text.Trim() !=null)

{

sw.WriteLine("}");

}

sw.Close();

fs.Close();

MessageBox.Show("实体类创建成功");

}

}

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)

{

}

private void radYes_CheckedChanged(object sender, EventArgs e)

{

txtNameSpace.Visible = true;

}

private void radNo_CheckedChanged(object sender, EventArgs e)

{

txtNameSpace.Visible = false;

}

}

}

        

      名称和ID的实体类:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

namespace DesignEntity

{

class Class1

{

private string name;

private int id;

public int ID

{

get { return id; }

set { id = value; }

}

public string Name

{

get { return name; }

set { name = value; }

}

}

}

  实体类生成后肯定要把它保存到一个自己找到的到的地方,这时候就需要一个新的控件,并对这个控件属性进行相应的设置:

                           

                           

     这样一个属于你的实体类生成器就诞生啦!!!!

网址:实体类生成器教程:从设计到实现 https://www.yuejiaxmz.com/news/view/335294

相关内容

Android家庭收纳APP设计与实现教程
简述电子产品设计流程,从理论到实践!
APP开发从创意到现实的旅程
从生活的体验到“人性化”环境设计实践
3D MAX全屋定制模型设计步骤详解:从构思到实现
AI人工智能机器人:从未来的助手到现实的伙伴
多传感器融合的人体健康监测系统设计与实现
现代家庭教育知识生产:从原理到实践的体系 王茜 吴重涵
Springboot生活垃圾分类回收系统设计与实现
java计算机毕业设计大学生运动健身系统的设计与实现(开题+程序+论文)

随便看看