public static void GenerateClassCode(JClassInfo jClassInfo, DeclareOption declareOption, string formatString) { string fieldsString = ""; if ((declareOption & DeclareOption.CustomFormat) == DeclareOption.CustomFormat) { fieldsString = CreateMemberString(jClassInfo.Properties, formatString, declareOption); } else { StringBuilder optionalFormat = new StringBuilder(); optionalFormat.Append(SetModifider(declareOption)); optionalFormat.Append(" {TYPE}"); optionalFormat.Append(" {NAME} "); if ((declareOption & DeclareOption.Property) == DeclareOption.Property) { optionalFormat.Append(" {get; set;}"); } else if ((declareOption & DeclareOption.NormalField) == DeclareOption.NormalField) { optionalFormat.Append(";"); } fieldsString = CreateMemberString(jClassInfo.Properties, optionalFormat.ToString(), declareOption); } string classTemplate = TemplateString.ClassTemplate; classTemplate = classTemplate.Replace("{FD}", fieldsString); classTemplate = classTemplate.Replace("{CN}", jClassInfo.Type); jClassInfo.ClassCode = classTemplate; }
private void usc_Input_GenerateButtonClicked(object sender, Controls.GenerateButtonClickedEventArgs e) { JObject jObject; try { jObject = JObject.Parse(e.JsonString); } catch (Exception exception) { MessageBox.Show("json object 생성중에 예외 발생\n" + exception.Message, "알림"); return; }; JClassInfo temp = _jClassCreater.Create(jObject); ClassInfoTreeNode classInfoTreeNode = new ClassInfoTreeNode(temp); _declareOption = e.SelectedOption; _formatString = e.FormatString; SetNode(classInfoTreeNode); usc_CodeViewer.ClearTreeView(); usc_CodeViewer.AddNode(classInfoTreeNode); usc_CodeViewer.SetCodeText(classInfoTreeNode.ClassCode); }
public static string SetModifider(DeclareOption declareOption) { if ((declareOption & DeclareOption.Private) == DeclareOption.Private) { return("[JsonProperty]private"); } if ((declareOption & DeclareOption.Protected) == DeclareOption.Protected) { return("[JsonProperty]protected"); } if ((declareOption & DeclareOption.Public) == DeclareOption.Public) { return("public"); } return(""); }
private static string CreateMemberString(List <JInfo> members, string formatString, DeclareOption declareOption) { StringBuilder fieldString = new StringBuilder(); foreach (var item in members) { string tempString = formatString; if ((declareOption & DeclareOption.Nullable) == DeclareOption.Nullable) { if (item.Type == SharpType.Bool || item.Type == SharpType.Int || item.Type == SharpType.Double) { tempString = tempString.Replace("{TYPE}", "{TYPE}?"); } } tempString = tempString.Replace("{TYPE}", item.Type); int nameStart = tempString.IndexOf("{NAME}"); if ((declareOption & DeclareOption.UpperFirstWord) == DeclareOption.UpperFirstWord) { tempString = tempString.Replace("{NAME}", (char.ToUpper(item.Name[0]) + item.Name.Substring(1))); } else { tempString = tempString.Replace("{NAME}", item.Name); } item.UsetDefineName = ExtractName(tempString, nameStart); fieldString.AppendLine(); fieldString.Append("\t"); fieldString.Append(tempString); } return(fieldString.ToString()); }
protected virtual void OnGenerateButtonClicked(string jsonSting, string formatString, DeclareOption declareContent) { OnGenerateButtonClicked(new GenerateButtonClickedEventArgs(jsonSting, formatString, declareContent)); }
private void btn_Generate_Click(object sender, EventArgs e) { DeclareOption declareContent = 0; if (!cb_Custom.Checked) { if (rb_Default.Checked) { declareContent |= DeclareOption.Default; } else if (rb_Private.Checked) { declareContent |= DeclareOption.Private; } else if (rb_Protected.Checked) { declareContent |= DeclareOption.Protected; } else if (rb_Public.Checked) { declareContent |= DeclareOption.Public; } if (rb_NormalVariable.Checked) { declareContent |= DeclareOption.NormalField; } else if (rb_Property.Checked) { declareContent |= DeclareOption.Property; } if (cb_Nullable.Checked) { declareContent |= DeclareOption.Nullable; } if (cb_UpperFirst.Checked) { declareContent |= DeclareOption.UpperFirstWord; } OnGenerateButtonClicked(txt_JsonString.Text, txt_FormatString.Text, declareContent); } else { Debug.WriteLine("Generate 버튼 누름 - 커스텀"); declareContent |= DeclareOption.CustomFormat; if (cb_Nullable.Checked) { declareContent |= DeclareOption.Nullable; } if (cb_UpperFirst.Checked) { declareContent |= DeclareOption.UpperFirstWord; } OnGenerateButtonClicked(txt_JsonString.Text, txt_FormatString.Text, declareContent); } }
public GenerateButtonClickedEventArgs(string jsonString, string formatString, DeclareOption declareOption) { JsonString = jsonString; FormatString = formatString; SelectedOption = declareOption; }