private void btn_NewParam_Click(object sender, EventArgs e) { try { Parm_Type _new_pt = (Parm_Type)Enum.Parse(typeof(Parm_Type), cmb_ParamType.Text); string _new_pn = txt_ParamName.Text; if (string.IsNullOrEmpty(_new_pn)) { MessageBox.Show("請填入參數名稱", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } IEnumerable <Param> _exist_Param = List_Params.Where(p => p.ParamName.ToLower() == _new_pn.ToLower()); if (_exist_Param.Any()) { MessageBox.Show("參數已存在", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } List_Params.Add(new Param() { ParamType = _new_pt, ParamName = _new_pn }); txt_ParamName.Text = string.Empty; } catch (Exception ex) { Class_Log.Write_Log(Log_Type.Error, "btn_NewParam_Click", ex.ToString()); } }
private void tsmi_dgv_Param_Delete_Click(object sender, EventArgs e) { try { if (dgv_Param.SelectedRows.Count > 0) { DataGridViewRow row = dgv_Param.SelectedRows[0]; // 目前設定是單選狀態 Param _param = List_Params.FirstOrDefault(p => p.ParamType.ToString() == row.Cells["ParamType"].Value.ToString() && p.ParamName == row.Cells["ParamName"].Value.ToString()); if (_param != null) { List_Params.Remove(_param); dgv_Param.CurrentCell = null; } } } catch (Exception ex) { Class_Log.Write_Log(Log_Type.Error, "tsmi_dgv_Param_Delete_Click", ex.ToString()); } }
private void btn_Convert_Click(object sender, EventArgs e) { try { rtb_Result.Clear(); if (string.IsNullOrEmpty(txt_ClassName.Text)) { MessageBox.Show("請填入類別名稱", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (List_Params.Count == 0) { MessageBox.Show("請填入參數", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } string str_head = "[Serializable]\n"; str_head = str_head + string.Format("public class {0} : INotifyPropertyChanged, ICloneable", txt_ClassName.Text) + "\n{\n"; str_head = str_head + " public event PropertyChangedEventHandler PropertyChanged;\n"; str_head = str_head + " private void NotifyPropertyChanged(string name)\n {\n"; str_head = str_head + " if (PropertyChanged != null)\n"; str_head = str_head + " PropertyChanged(this, new PropertyChangedEventArgs(name));\n }\n\n"; string str_body = string.Empty; #region 參數部分 foreach (Param p in List_Params) { str_body = str_body + string.Format(" private {0} _{1} = default({2});\n", p.ParamType.ToString().ToLower(), p.ParamName, p.ParamType.ToString().ToLower()); str_body = str_body + string.Format(@" [Display(Name = ""{0}"", Description = ""中文"")]", p.ParamName.ToUpper()) + "\n"; str_body = str_body + string.Format(@" [System.ComponentModel.DisplayName(""中文"")]") + "\n"; str_body = str_body + string.Format(" public {0} {1}", p.ParamType.ToString().ToLower(), p.ParamName.ToUpper()) + "\n {\n"; str_body = str_body + " get { return _" + p.ParamName + "; }\n"; str_body = str_body + " set \n {\n"; str_body = str_body + string.Format(" _{0} = value;\n", p.ParamName); str_body = str_body + string.Format(@" this.NotifyPropertyChanged(""{0}"");", p.ParamName.ToUpper()); str_body = str_body + " \n }\n"; str_body = str_body + " }\n\n"; } #endregion #region 建構子部分 str_body = str_body + " public " + txt_ClassName.Text + "()\n {\n"; foreach (Param p in List_Params) { str_body = str_body + " _" + p.ParamName + " = default(" + p.ParamType.ToString().ToLower() + ");\n"; } str_body = str_body + " }\n\n"; #endregion #region Get_Prop_Display函式 str_body = str_body + " public static Dictionary<string, string> Get_Prop_Display()\n {\n"; str_body = str_body + " Dictionary<string, string> _Infos = new Dictionary<string, string>();\n"; str_body = str_body + " try\n {\n"; str_body = str_body + " PropertyInfo[] propInfos = default(PropertyInfo[]);\n"; str_body = str_body + " propInfos = typeof(" + txt_ClassName.Text + ").GetProperties();\n"; str_body = str_body + " propInfos.ToList().ForEach(x =>\n {\n"; str_body = str_body + " IList<CustomAttributeData> cad = (x as System.Reflection.MemberInfo).GetCustomAttributesData();\n"; str_body = str_body + " _Infos.Add(x.Name, cad[0].NamedArguments[1].TypedValue.Value.ToString());\n"; str_body = str_body + " });\n"; str_body = str_body + " return _Infos;\n }\n"; str_body = str_body + " catch (Exception ex)\n {\n"; str_body = str_body + " return _Infos;\n }\n }\n\n"; #endregion #region Clone函式 str_body = str_body + " public object Clone()\n {\n"; str_body = str_body + " return this.MemberwiseClone();\n }\n\n"; str_body = str_body + " public object Clone_Deep()\n {\n"; str_body = str_body + " MemoryStream stream = new MemoryStream();\n"; str_body = str_body + " BinaryFormatter formatter = new BinaryFormatter();\n"; str_body = str_body + " formatter.Serialize(stream, this);\n"; str_body = str_body + " stream.Position = 0;\n"; str_body = str_body + " return formatter.Deserialize(stream);\n }\n\n"; #endregion #region ToArray函式 str_body = str_body + " public object[] ToArray()\n {\n"; str_body = str_body + " return new object[] { " + string.Join(",", List_Params.Select(p => "_" + p.ParamName).ToArray()) + " };\n }\n\n"; #endregion #region ToString函式 str_body = str_body + " public override string ToString()\n {\n"; string str_format = "|"; for (int i = 0; i < List_Params.Count; i++) { str_format = str_format + "{" + i + "}|"; } str_body = str_body + @" return string.Format(""" + str_format + @""", " + string.Join(",", List_Params.Select(p => "_" + p.ParamName).ToArray()) + ");\n }\n\n"; #endregion string str_end = "}\n"; rtb_Result.Text = str_head + str_body + str_end; } catch (Exception ex) { Class_Log.Write_Log(Log_Type.Error, "btn_Convert_Click", ex.ToString()); } }