/// <summary> /// 显示数据 /// </summary> /// <param name="_proModel"></param> private void ShowProtoInfo(ProtocolModel _proModel) { if (_proModel == null) { return; } groupProtoInfo.Enabled = true; //协议基本信息 this.txtProtoCode.Text = _proModel.Code.ToString(); this.txtProtoEnName.Text = _proModel.EName; this.txtProtoCnName.Text = _proModel.CName; this.txtProtoDesc.Text = _proModel.Desc; var attrsList = ProtocolDB.GetAttributes(_proModel.PID); dvGrid.Rows.Clear(); if (attrsList != null && attrsList.Count > 0) { for (int i = 0; i < attrsList.Count; i++) { DataGridViewRow dataGridViewRow = dvGrid.Rows[0].Clone() as DataGridViewRow; dataGridViewRow.Cells[0].Value = attrsList[i].AType; dataGridViewRow.Cells[1].Value = attrsList[i].EName; dataGridViewRow.Cells[2].Value = attrsList[i].CName; dataGridViewRow.Cells[3].Value = attrsList[i].IsLoop; dataGridViewRow.Cells[4].Value = attrsList[i].A2LoopEName; dataGridViewRow.Cells[5].Value = attrsList[i].A2BoolEName; dataGridViewRow.Cells[6].Value = attrsList[i].A2Bool; dataGridViewRow.Cells[7].Value = attrsList[i].A2StrEName; dataGridViewRow.Cells[8].Value = attrsList[i].A2Str; dataGridViewRow.Cells[9].Value = attrsList[i].PAID; dataGridViewRow.Cells[10].Value = attrsList[i].PID; dvGrid.Rows.Add(dataGridViewRow); } } }
private async void ExportAsync(ProtocolModel _model) { //获取协议的所有属性 List <ProtocolAttributeModel> attributes = ProtocolDB.GetAttributes(_model.PID); StringBuilder sbr = await Task.Run(() => { //代码结果集 StringBuilder sb = new StringBuilder(); sb.Append("//===================================================\r\n"); sb.Append($"//创建时间:{ DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}\r\n"); sb.Append("//备 注:\r\n"); sb.Append("//===================================================\r\n"); sb.Append("using System.Collections;\r\n"); sb.Append("using System.Collections.Generic;\r\n"); sb.Append("using System;\r\n"); sb.Append("\r\n"); sb.Append("/// <summary>\r\n"); sb.Append($"/// {_model.CName}\r\n"); sb.Append("/// </summary>\r\n"); if (ProtocolDB.settingModel.UseNamespace) { sb.AppendFormat($"namespace {ProtocolDB.settingModel.NamespaceContent}\r\n"); sb.AppendFormat("{{\r\n");//namespace Start } sb.Append($"public struct {_model.EName}Protocol\r\n"); sb.AppendFormat("{{\r\n");//class Start sb.Append($" public ushort Code {{ get {{ return {_model.Code}; }} }}\r\n"); sb.Append("\r\n"); #region 字段 for (int i = 0; i < attributes.Count; i++) { var att = attributes[i]; if (!string.IsNullOrEmpty(att.A2LoopEName)) { //如果是从属某项循环的 不理会 continue; } //不从属循环的 if (att.IsLoop) { //如果当前是循环项 var loopAtts = attributes.Where(c => c.A2LoopEName == att.EName).OrderBy(c => c.Index).ToList(); for (int l = 0; l < loopAtts.Count; l++) { sb.Append($" public List<{loopAtts[l].AType}> {loopAtts[l].EName}List; //{loopAtts[l].CName}\r\n"); } } else { //正常 sb.Append($" public {att.AType} {att.EName}; //{att.CName}\r\n"); } } sb.Append("\r\n"); #endregion #region ToArray sb.AppendFormat(" public byte[] ToArray()\r\n"); sb.AppendFormat(" {{\r\n");//ToArray Start sb.AppendFormat(" using (JZ_MemoryStream ms = new JZ_MemoryStream())\r\n"); sb.AppendFormat(" {{\r\n");//JZ_MemoryStream Start sb.AppendFormat(" ms.WriteUShort(Code);\r\n"); for (int i = 0; i < attributes.Count; i++) { var att = attributes[i]; if (!string.IsNullOrEmpty(att.A2LoopEName) || !string.IsNullOrEmpty(att.A2BoolEName) || !string.IsNullOrEmpty(att.A2StrEName)) { //附属于其它属性的值 不处理 后面一起处理 continue; } if (att.IsLoop) { //本身是循环项 //查找依附于当前循环项的子项 var loopAtts = attributes.Where(c => c.A2LoopEName == att.EName).OrderBy(c => c.Index).ToList(); //输出子项数量 sb.AppendFormat($" ms.WriteInt({loopAtts.Count});\r\n"); if (loopAtts != null && loopAtts.Count > 0) { for (int l = 0; l < loopAtts.Count; l++) { sb.Append($"for(int i=0;i<{loopAtts[l].EName}List.Count;i++)"); sb.AppendFormat(" {{\r\n"); sb.Append($" ms.Write{ChangeTypeName(loopAtts[l].AType).Replace("()", "")} {loopAtts[l].EName}List[i]; //{loopAtts[l].CName}\r\n"); sb.AppendFormat(" }}\r\n"); } } } else if (att.AType.ToLower() == "bool") { //正常 sb.AppendFormat($" ms.Write{ChangeTypeName(att.AType).Replace("()", "")}({att.EName});\r\n"); //查看是否有关联项 var boolAtts = attributes.Where(c => c.A2BoolEName == att.EName).OrderBy(c => c.Index).ToList(); if (boolAtts != null && boolAtts.Count > 0) { //有bool关联 for (int b = 0; b < boolAtts.Count; b++) { string torf = att.A2Bool ? "" : "!"; sb.Append($"if({torf}{att.EName})"); sb.AppendFormat(" {{\r\n"); sb.Append($" ms.Write{ChangeTypeName(boolAtts[b].AType).Replace("()", "")}({boolAtts[b].EName}); //{boolAtts[b].CName}\r\n"); sb.AppendFormat(" }}\r\n"); } } } else if (att.AType.ToLower() == "string") { //本身是string项 //正常输出string内容 sb.AppendFormat($" ms.Write{ChangeTypeName(att.AType).Replace("()", "")}({att.EName});\r\n"); //查找string附属 var stringAtts = attributes.Where(c => c.A2StrEName == att.EName).OrderBy(c => c.Index).ToList(); if (stringAtts != null && stringAtts.Count > 0) { for (int s = 0; s < stringAtts.Count; s++) { sb.Append($"if({att.EName}=={stringAtts[s].A2Str})"); sb.AppendFormat(" {{\r\n"); sb.Append($" ms.Write{ChangeTypeName(stringAtts[s].AType).Replace("()", "")}({stringAtts[s].EName}); //{stringAtts[s].CName}\r\n"); sb.AppendFormat(" }}\r\n"); } } } else { //其它项 正常处理 sb.AppendFormat(" ms.Write{0}({1});\r\n", ChangeTypeName(att.AType).Replace("()", ""), att.EName); } } sb.AppendFormat(" }}\r\n"); //JZ_MemoryStream End sb.AppendFormat(" return ms.ToArray();\r\n"); sb.AppendFormat(" }}\r\n"); //ToArray End #endregion #region ToProtocol sb.AppendFormat($" public {_model.EName}Protocol ToProtocol(byte[] _buffer)\r\n"); sb.AppendFormat(" {{\r\n");//ToProtocol Start sb.AppendFormat($" {_model.EName}Protocol pro = new {_model.EName}Protocol();\r\n"); sb.AppendFormat(" using (JZ_MemoryStream ms = new JZ_MemoryStream(_buffer))\r\n"); sb.AppendFormat(" {{\r\n");//JZ_MemoryStream Start for (int i = 0; i < attributes.Count; i++) { var att = attributes[i]; if (!string.IsNullOrEmpty(att.A2LoopEName) || !string.IsNullOrEmpty(att.A2BoolEName) || !string.IsNullOrEmpty(att.A2StrEName)) { //附属于其它属性的值 不处理 后面一起处理 continue; } if (att.IsLoop) { //本身是循环项 //查找依附于当前循环项的子项 var loopAtts = attributes.Where(c => c.A2LoopEName == att.EName).OrderBy(c => c.Index).ToList(); //输出子项数量 sb.AppendFormat($" int count= ms.ReadInt();\r\n"); if (loopAtts != null && loopAtts.Count > 0) { //字段初始化 for (int l = 0; l < loopAtts.Count; l++) { sb.Append($" pro.{loopAtts[l].EName}List=new List<{loopAtts[l].AType}>();//{loopAtts[l].CName}\r\n"); } //字段赋值 for (int l = 0; l < loopAtts.Count; l++) { sb.Append($"for(int i=0;i<count;i++)"); sb.AppendFormat(" {{\r\n"); sb.Append($" pro.{loopAtts[l].EName}List.Add(ms.Read{ChangeTypeName(loopAtts[l].AType).Replace("()", "")}()); //{loopAtts[l].CName}\r\n"); sb.AppendFormat(" }}\r\n"); } } } else if (att.AType.ToLower() == "bool") { //正常 sb.AppendFormat($" pro.{att.EName}= ms.Read{ChangeTypeName(att.AType).Replace("()", "")}();\r\n"); //查看是否有关联项 var boolAtts = attributes.Where(c => c.A2BoolEName == att.EName).OrderBy(c => c.Index).ToList(); if (boolAtts != null && boolAtts.Count > 0) { //有bool关联 for (int b = 0; b < boolAtts.Count; b++) { string torf = att.A2Bool ? "" : "!"; sb.Append($"if({torf}{att.EName})"); sb.AppendFormat(" {{\r\n"); sb.Append($" pro.{boolAtts[b].EName}= ms.Read{ChangeTypeName(boolAtts[b].AType).Replace("()", "")}(); //{boolAtts[b].CName}\r\n"); sb.AppendFormat(" }}\r\n"); } } } else if (att.AType.ToLower() == "string") { //本身是string项 //正常输出string内容 sb.AppendFormat($" pro.{att.EName}= ms.Read{ChangeTypeName(att.AType).Replace("()", "")}();\r\n"); //查找string附属 var stringAtts = attributes.Where(c => c.A2StrEName == att.EName).OrderBy(c => c.Index).ToList(); if (stringAtts != null && stringAtts.Count > 0) { for (int s = 0; s < stringAtts.Count; s++) { sb.Append($"if({att.EName}=={stringAtts[s].A2Str})"); sb.AppendFormat(" {{\r\n"); sb.Append($" pro.{stringAtts[s].EName}= ms.Read{ChangeTypeName(stringAtts[s].AType).Replace("()", "")}(); //{stringAtts[s].CName}\r\n"); sb.AppendFormat(" }}\r\n"); } } } else { //其它项 正常处理 sb.AppendFormat(" pro.{1}= ms.Read{0}();\r\n", ChangeTypeName(att.AType).Replace("()", ""), att.EName); } } sb.AppendFormat(" return pro;\r\n"); sb.AppendFormat(" }}\r\n"); //JZ_MemoryStream End sb.AppendFormat(" }}\r\n"); //ToProtocol End #endregion sb.AppendFormat(" }}\r\n");//class End if (ProtocolDB.settingModel.UseNamespace) { sb.AppendFormat("}}\r\n");//namespace End } return(sb); }); using (FileStream fs = new FileStream(string.Format("{0}/{1}Protocol.cs", saveFolder, _model.EName), FileMode.Create)) { using (StreamWriter sw = new StreamWriter(fs)) { sw.Write(sbr.ToString()); } } }