.NET实体类生成器 改

来源:互联网 发布:深圳gis数据下载 编辑:程序博客网 时间:2024/05/20 00:13

将前面的Form1.cs代码改成:

修改了前面的第44行(nameSpace-->namespace)和第53行

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 )                    {                        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;        }        }    }