public void CreateModel(string strJson) { lists.Clear(); subPath = subPathName + "\\" + ModelName.Text + ""; if (false == System.IO.Directory.Exists(subPath)) { //创建pic文件夹 System.IO.Directory.CreateDirectory(subPath); } JavaScriptSerializer serializer = new JavaScriptSerializer(); Dictionary <string, Object> json = (Dictionary <string, Object>)serializer.DeserializeObject(strJson); if (json != null) { List <string> keys = json.Keys.ToList(); List <string> fList = new List <string>(); lists.Add(fList); fList.Add("[Serializable]"); fList.Add("public class " + TableName); fList.Add("{"); foreach (var s in keys) { //findNode(json[s], s, "test", true); findNode2(s, json[s], fList); } DataRow[] Rowlist = IcreateType.GetColumns(tableName).Select("type <> 'jsonb' and type <>'json'"); for (int i = 0; i < Rowlist.Length; i++) { string classType = FileOperate.SqlTypeTope(Rowlist[i][1].ToString()); if (classType == "") { classType = "string"; } string className = Rowlist[i][0].ToString(); fList.Add("public " + classType + " " + className + " {get;set;}"); } fList.Add("}"); List <string> header = new List <string>(); header.Add("using System;"); header.Add("using System.Collections.Generic;"); header.Add("namespace " + ModelName.Text.Trim()); header.Add("{"); FileOperate.FileWrite(header, subPath + "\\" + TableName + ".cs"); foreach (List <string> clist in lists) { FileOperate.WriteAppendList(clist, subPath + "\\" + TableName + ".cs"); } FileOperate.WriteAppendStr("\r\n}", subPath + "\\" + TableName + ".cs"); } else { List <string> header = new List <string>(); header.Add("using System;"); header.Add("using System.Collections.Generic;"); header.Add("namespace " + ModelName.Text.Trim()); header.Add("{"); header.Add("[Serializable]"); header.Add("public class " + TableName); header.Add("{"); FileOperate.FileWrite(header, subPath + "\\" + TableName + ".cs"); DataRow[] Rowlist = IcreateType.GetColumns(tableName).Select("type <> 'jsonb' and type <>'json'"); for (int i = 0; i < Rowlist.Length; i++) { string classType = FileOperate.SqlTypeTope(Rowlist[i][1].ToString()); if (classType == "") { classType = "string"; } string className = Rowlist[i][0].ToString(); header.Add("public " + classType + " " + className + " {get;set;}"); } header.Add("}"); header.Add("}"); FileOperate.FileWrite(header, subPath + "\\" + TableName + ".cs"); } CreateManger(TableName); CreateComponent((DataTable)dgvColumns.DataSource); CreateControllers((DataTable)dgvColumns.DataSource); MessageBox.Show("生成成功,请查看!!", "提示"); }
public void findNode(object obj, string fileName, string fastName, bool IsF) { if (null == obj) { if (IsF) { FileOperate.WriteAppendStr("public string " + fileName + " {get;set;}\r\n", "" + subPath + "\\" + TableName + ".cs"); } else { FileOperate.WriteAppendStr("public string " + fileName + " {get;set;}\r\n", "" + subPath + "\\" + fastName + "Content.cs"); } return; } var type = obj.GetType(); if (type == typeof(int)) { if (IsF) { FileOperate.WriteAppendStr("public int " + fileName + " {get;set;}\r\n", "" + subPath + "\\" + TableName + ".cs"); } else { FileOperate.WriteAppendStr("public int " + fileName + " {get;set;}\r\n", "" + subPath + "\\" + fastName + "Content.cs"); } return; } else if (type == typeof(string)) { if (IsF) { FileOperate.WriteAppendStr("public string " + fileName + " {get;set;}\r\n", "" + subPath + "\\" + TableName + ".cs"); } else { FileOperate.WriteAppendStr("public string " + fileName + " {get;set;}\r\n", "" + subPath + "\\" + fastName + "Content.cs"); } return; } else if (type == typeof(bool)) { if (IsF) { FileOperate.WriteAppendStr("public bool " + fileName + " {get;set;}\r\n", "" + subPath + "\\" + TableName + ".cs"); } else { FileOperate.WriteAppendStr("public bool " + fileName + " {get;set;}\r\n", "" + subPath + "\\" + fastName + "Content.cs"); } return; } else if (type == typeof(Dictionary <string, Object>))//键值对 { Dictionary <string, Object> chi = obj as Dictionary <string, Object>; List <string> keys = chi.Keys.ToList(); if (IsF) { FileOperate.WriteAppendStr("public " + fileName + "Content " + fileName + " {get;set;}\r\n", "" + subPath + "\\" + TableName + ".cs"); } else { FileOperate.WriteAppendStr("public " + fileName + "Content " + fileName + " {get;set;}\r\n", "" + subPath + "\\" + fastName + "Content.cs"); } FileOperate.WriteAppendStr("using System;\r\nusing System.Collections.Generic;\r\nnamespace " + ModelName.Text.Trim() + "\r\n{\r\npublic class " + fileName + "Content\r\n{\r\n", "" + subPath + "\\" + fileName + "Content.cs"); int i = 0; foreach (var s in keys) { i++; findNode(chi[s], s, fileName, false); if (i == keys.Count) { FileOperate.WriteAppendStr("}\r\n}\r\n", "" + subPath + "\\" + fileName + "Content.cs"); } } } else //集合 { Object[] objArr = obj as Object[]; Dictionary <string, Object> chi = objArr[0] as Dictionary <string, Object>; List <string> keys = chi.Keys.ToList(); if (IsF) { FileOperate.WriteAppendStr("public List<" + fileName + "Content> " + fileName + " {get;set;}\r\n", "" + subPath + "\\" + TableName + ".cs"); } else { FileOperate.WriteAppendStr("public List<" + fileName + "Content> " + fileName + " {get;set;}\r\n", "" + subPath + "\\" + fastName + "Content.cs"); } FileOperate.WriteAppendStr("using System;\r\nusing System.Collections.Generic;\r\nnamespace " + ModelName.Text.Trim() + "\r\n{\r\npublic class " + fileName + "Content\r\n{\r\n", "" + subPath + "\\" + fileName + "Content.cs"); int i = 0; foreach (var s in keys) { i++; findNode(chi[s], s, fileName, false); if (i == keys.Count) { FileOperate.WriteAppendStr("}\r\n}\r\n", "" + subPath + "\\" + fileName + "Content.cs"); } } } }