private string CreateRows(object source, IRenderingEngine engine) { var props = source.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance); var rows = new StringBuilder(); foreach (var propertyInfo in props) { var childRenderer = engine.TryFindRenderer(propertyInfo.PropertyType); var childValue = propertyInfo.GetValue(source); var childRendering = childRenderer.Render(childValue, engine); var row = $@"<tr><td>{propertyInfo.Name}</td><td>{childRendering.Content}</td></tr>"; rows.AppendLine(row); } var fields = source.GetType().GetFields(BindingFlags.Public | BindingFlags.Instance); foreach (var fieldInfo in fields) { var childRenderer = engine.TryFindRenderer(fieldInfo.FieldType); var childValue = fieldInfo.GetValue(source); var childRendering = childRenderer.Render(childValue, engine); var row = $@"<tr><td>{fieldInfo.Name}</td><td>{childRendering.Content}</td></tr>"; rows.AppendLine(row); } return(rows.ToString()); }
private static void CreateTableRow(IEnumerable <MemberInfo> memberInfos, IRenderingEngine engine, object element, IRendering defaultRendering, StringBuilder rowsBuffer) { if (memberInfos?.Any() != false) { foreach (var memberInfo in memberInfos) { IRendering childRendering = null; switch (memberInfo) { case PropertyInfo propertyInfo: { var childRenderer = engine.TryFindRenderer(propertyInfo.PropertyType); var childValue = propertyInfo.GetValue(element); childRendering = childValue == null ? defaultRendering : childRenderer.Render(childValue, engine); } break; case FieldInfo fieldInfo: { var childRenderer = engine.TryFindRenderer(fieldInfo.FieldType); var childValue = fieldInfo.GetValue(element); childRendering = childValue == null ? defaultRendering : childRenderer.Render(childValue, engine); } break; } var row = $"\t\t<td>{childRendering?.Content ?? string.Empty}</td>"; rowsBuffer.AppendLine(row); } } else { var childRenderer = engine.TryFindRenderer(element.GetType()); var childRendering = childRenderer.Render(element, engine); rowsBuffer.AppendLine($"\t\t<td>{childRendering.Content}</td>"); } }
public static string CreateTableRowsFromValues(IEnumerable <MemberInfo> memberInfos, IEnumerable <KeyValuePair <object, object> > source, IRenderingEngine engine, bool emptyFirstCell = false) { var nullRendering = new PlainTextRendering("null"); var rowsBuffer = new StringBuilder(); foreach (var(key, element) in source.Select(e => (e.Key, e.Value))) { rowsBuffer.AppendLine("\t<tr>"); if (emptyFirstCell) { rowsBuffer.AppendLine("\t\t<td></td>"); } var keyRenderer = engine.TryFindRenderer(key.GetType()); var keyRendering = keyRenderer.Render(key, engine); rowsBuffer.AppendLine($"\t\t<td>{keyRendering?.Content ?? string.Empty}</td>"); CreateTableRow(memberInfos, engine, element, nullRendering, rowsBuffer); rowsBuffer.AppendLine("\t</tr>"); } return(rowsBuffer.ToString()); }