Esempio n. 1
0
        private void btn_work_Click(object sender, EventArgs e)
        {
            var savePlace = this.txt_savePlace.Text;

            if (string.IsNullOrWhiteSpace(savePlace))
            {
                MessageBox.Show("生成位置不能为空!", "Error Msg");
                return;
            }

            Func <string, string> generateTableNameFunc = null, generatePropNameFunc = null;

            try
            {
                var dynamicAssembly = DynamicClassTools.DynamicAssembly(this.rtb_dynamicCode.Text);

                var instance = DynamicClassTools.GetClass(dynamicAssembly, "Dynamicly.GenerateHelper");

                generateTableNameFunc = (str) => (string)DynamicClassTools.InvokeMethod(instance, "GetTableName", str);
                generatePropNameFunc  = (str) => (string)DynamicClassTools.InvokeMethod(instance, "GetPropName", str);
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message, "Error Msg");
                return;
            }

            var connStr = this.txt_connStr.Text;

            if (string.IsNullOrWhiteSpace(connStr))
            {
                MessageBox.Show("连接字符串不能为空!", "Error Msg");
                return;
            }

            try
            {
                Conn = DbTools.GetConnection(connStr, DbType);

                //测试连接
                Conn.Open();

                Conn.Close();
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message, "Error Msg");
                return;
            }

            Operation = DbTools.GetOperation(DbType);

            if (Operation == null)
            {
                MessageBox.Show($"暂不支持{DbType}", "Error Msg");
                return;
            }

            CommandGenerateCode commandGenerate = new CommandGenerateCode(Conn, Operation);

            var dictionary = commandGenerate.Work();

            if (dictionary.Count == 0)
            {
                MessageBox.Show("查询数据为空", "Confirm Msg");
                return;
            }

            var templateField = this.rtb_templateField.Text;
            var templateTable = this.rtb_templateTable.Text;

            try
            {
                //并行写入文件
                dictionary.AsParallel().ForAll((item =>
                {
                    var code = commandGenerate.GenerateCode(item.Key, item.Value, templateTable, templateField
                                                            , generateTableNameFunc, generatePropNameFunc);

                    FileTools.InFile(savePlace, $"{generateTableNameFunc.Invoke(item.Key.Name)}.class", code);
                }
                                                ));

                MessageBox.Show("Success!", "Confirm Msg");
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message, "Error Msg");
            }
        }