.NET实体类生成器

来源:互联网 发布:货运软件有哪些 编辑:程序博客网 时间:2024/05/07 16:51

牛腩的视频中,提到实体类生成器,从网上下载下来的,一点儿也不精简。

下面自己编写一个.NET实体类生成器。

(C#)



在窗体上拖入一些控件:

最后如图:

Form1.cs[设计]:




Form1.cs代码:

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.IO;using System.Text.RegularExpressions ;namespace WindowsFormsApplication1{    public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();        }        private void button1_Click(object sender, EventArgs e)        {                         string ClassName1 = textBox1.Text.Trim();            string classexp1 = txtexp.Text.Trim();            string NameSpace1 = txtNameSapace.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 (radyou.Checked && txtNameSapace.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();                        //替换propname前一个或多个下划线,中间下划线不替换                        sw.WriteLine("        private " + type + " " + propname + ";");                        string propname1 = Regex.Replace(propname, "^_+", "");                        //把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 (radyou.Checked && txtNameSapace.Text.Trim() != null)                {                    sw.WriteLine("}");                }                sw.Close();                fs.Close();                MessageBox.Show("实体类创建成功!");            }            }        private void radwu_CheckedChanged(object sender, EventArgs e)        {             txtNameSapace.Visible = false;        }        private void radyou_CheckedChanged(object sender, EventArgs e)        {            txtNameSapace.Visible  = true;        }        }    }


Class1.cs代码:

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace WindowsFormsApplication1{    class Class1    {        private int id;        public int Id        {            get { return id; }            set { id = value; }        }        private string name;        public string Name        {            get { return name; }            set { name = value; }        }    }}

Program.cs代码:

using System;using System.Collections.Generic;using System.Linq;using System.Windows.Forms;namespace WindowsFormsApplication1{    static class Program    {        /// <summary>        /// 应用程序的主入口点。        /// </summary>        [STAThread]        static void Main()        {            Application.EnableVisualStyles();            Application.SetCompatibleTextRenderingDefault(false);            Application.Run(new Form1());            Class1 u = new Class1();            u.Id = 1;            u.Name = "物业";        }    }}

打包好的下载:点击打开链接

http://download.csdn.net/detail/wulingmin21/3947512