public bool TryDeserialize(string text, out ClassFormat[] classFormats) { classFormats = new ClassFormat[0]; bool isSuccess = true; List <ClassFormat> formatList = new List <ClassFormat>(); try { foreach (var line in CsvReader.ReadFromText(text, _options)) { List <FieldFormat> fieldFormatList = new List <FieldFormat>(); ClassFormat classFormat = new ClassFormat(); TryGetLineValue(line, _columnNames[0], out string comment); classFormat.comment = comment; classFormat.className = line[_columnNames[1]]; int columnIndex = 1; while (true) { bool hasFieldType = TryGetLineValue(line, columnIndex, EColumnName.type, out string fieldType); bool hasFieldName = TryGetLineValue(line, columnIndex, EColumnName.name, out string fieldName); if (hasFieldType == false || hasFieldName == false) { break; } FieldFormat fieldFormat = new FieldFormat(); fieldFormat.fieldType = fieldType; fieldFormat.fieldName = fieldName; TryGetLineValue(line, columnIndex, EColumnName.comment, out string fieldComment); fieldFormat.comment = fieldComment; fieldFormatList.Add(fieldFormat); columnIndex++; } classFormat.fieldFormats = fieldFormatList.ToArray(); formatList.Add(classFormat); } } catch { isSuccess = false; } classFormats = formatList.ToArray(); return(isSuccess); }
public string Serialize(params ClassFormat[] classFormats) { List <string> columnNames = new List <string>(_columnNames); ClassFormat mostFieldClassFormat = classFormats .OrderByDescending(item => item.fieldFormats.Length) .FirstOrDefault(); if (mostFieldClassFormat != null) { FieldFormat[] fieldFormatArray = mostFieldClassFormat.fieldFormats; for (int i = 0; i < fieldFormatArray.Length; i++) { int index = i + 1; columnNames.Add(GetColumnName(index, EColumnName.comment)); columnNames.Add(GetColumnName(index, EColumnName.type)); columnNames.Add(GetColumnName(index, EColumnName.name)); } } List <string[]> rows = new List <string[]>(); foreach (ClassFormat classFormat in classFormats) { List <string> row = new List <string>(); row.Add(classFormat.comment); row.Add(classFormat.className); FieldFormat[] fieldFormatArray = classFormat.fieldFormats; for (int i = 0; i < fieldFormatArray.Length; i++) { row.Add(fieldFormatArray[i].comment); row.Add(fieldFormatArray[i].fieldType); row.Add(fieldFormatArray[i].fieldName); } rows.Add(row.ToArray()); } var csv = CsvWriter.WriteToText(columnNames.ToArray(), rows, ','); return(csv); }