private void AddCurrentObjectWithPropertiesToPrinting(PrintingInformation printingInformation) { var indentation = printingInformation.CurrentIndentation; var nestingLevel = printingInformation.CurrentNestingLevel; var obj = printingInformation.CurrentObject; var objFullName = printingInformation.CurrentFullName; var type = obj.GetType(); printingInformation.AddPrintingNewLine(""); foreach (var propertyInfo in GetNonExcludedProperties(type)) { var propertyName = $"{objFullName}.{propertyInfo.Name}"; if (IsExcludedName(propertyName)) { continue; } printingInformation.AddPrinting($"{indentation}{propertyName} = "); AddObjectPrinting(printingInformation, propertyInfo.GetValue(obj), nestingLevel + 1, propertyName); } foreach (var fieldInfo in GetNonExcludedFields(type)) { var fieldName = $"{objFullName}.{fieldInfo.Name}"; if (IsExcludedName(fieldName)) { continue; } printingInformation.AddPrinting($"{indentation}{fieldName} = "); AddObjectPrinting(printingInformation, fieldInfo.GetValue(obj), nestingLevel + 1, fieldName); } }
private void AddObjectPrinting(PrintingInformation printingInformation, object addedObject, int nestingLevel, string nameObject = "") { Func <object, string> printingFunction; if (nestingLevel >= 10) { printingInformation.AddPrintingNewLine("... recursion is too deep"); } else if (addedObject == null) { printingInformation.AddPrintingNewLine("null"); } else if (TryGetSpecialPrinting(addedObject, nameObject, out printingFunction)) { printingInformation.AddPrintingNewLine(printingFunction(addedObject)); } else if (baseTypes.Contains(addedObject.GetType())) { printingInformation.AddPrintingNewLine(addedObject.ToString()); } else if (!printingInformation.AddObjectAndSetCurrent(addedObject, nestingLevel, nameObject)) { printingInformation.AddPrintingNewLine("... this item was added already"); } else { AddCurrentObjectToPrintingResult(printingInformation); } }
public string PrintToString(TOwner obj) { if (!IsAddedType(obj.GetType())) { return(string.Empty); } var printingResult = new PrintingInformation(); AddObjectPrinting(printingResult, obj, 0); return(printingResult.GetPrinting()); }
private void AddCurrentCollectionToPrinting(PrintingInformation printingInformation) { var indentation = printingInformation.CurrentIndentation; var nestingLevel = printingInformation.CurrentNestingLevel; var collection = printingInformation.CurrentObject as IEnumerable; var objFullName = printingInformation.CurrentFullName; printingInformation.AddPrintingNewLine(" {"); foreach (var obj in GetAddedObjects(collection)) { printingInformation.AddPrinting(indentation); AddObjectPrinting(printingInformation, obj, nestingLevel + 1, $"{objFullName}."); } printingInformation.AddPrintingNewLine($"{indentation}}}"); }
private void AddCurrentObjectToPrintingResult(PrintingInformation printingInformation) { var currentObject = printingInformation.CurrentObject; var type = currentObject.GetType(); printingInformation.AddPrinting(type.Name); if (currentObject is IEnumerable) { AddCurrentCollectionToPrinting(printingInformation); } else { AddCurrentObjectWithPropertiesToPrinting(printingInformation); } }