-
Notifications
You must be signed in to change notification settings - Fork 214
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Яценко Ирина #198
base: master
Are you sure you want to change the base?
Яценко Ирина #198
Changes from 1 commit
871837c
767afb5
6432b58
0aae76d
d14c11a
3a465c2
4a2ac35
59e3400
b64fd61
5a18f9c
230d3cb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using System; | ||
using System.Reflection; | ||
|
||
namespace ObjectPrinting | ||
{ | ||
public class DataMember | ||
{ | ||
public string Name { get; set; } | ||
public Type Type { get; set; } | ||
|
||
public Func<object, object> GetValue; | ||
public MemberInfo MemberInfo { get; set; } | ||
|
||
public DataMember(FieldInfo fieldInfo) | ||
{ | ||
Name = fieldInfo.Name; | ||
GetValue = fieldInfo.GetValue; | ||
Type = fieldInfo.FieldType; | ||
MemberInfo = fieldInfo; | ||
} | ||
|
||
public DataMember(PropertyInfo property) | ||
{ | ||
Name = property.Name; | ||
GetValue = property.GetValue; | ||
Type = property.PropertyType; | ||
MemberInfo = property; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,9 @@ public class PrintingConfig<TOwner> | |
{ | ||
private readonly HashSet<MemberInfo> excludedProperties = new HashSet<MemberInfo>(); | ||
private readonly HashSet<Type> excludedTypes = new HashSet<Type>(); | ||
|
||
private readonly HashSet<FieldInfo> excludedFields = new HashSet<FieldInfo>(); | ||
|
||
private readonly Dictionary<object, int> complexObjectLinks = new Dictionary<object, int>(); | ||
private int maxRecursion = 2; | ||
|
||
|
@@ -61,7 +64,8 @@ public PrintingConfig<TOwner> SerializeWith<TPropType>(Func<TPropType, string> s | |
{ | ||
Func<object, string> func = p => serialize((TPropType)p); | ||
|
||
typeSerializesInfos[typeof(TPropType)] = func; | ||
if (!typeSerializesInfos.ContainsKey(typeof(TPropType))) | ||
typeSerializesInfos[typeof(TPropType)] = func; | ||
|
||
return this; | ||
} | ||
|
@@ -96,7 +100,7 @@ public PrintingConfig<TOwner> Trim(Expression<Func<TOwner, string>> property, in | |
return SerializeWith(property, func); | ||
} | ||
|
||
public PrintingConfig<TOwner> Trim(int length) // | ||
public PrintingConfig<TOwner> Trim(int length) | ||
{ | ||
Func<string, string> func = value => value.Length <= length | ||
? value | ||
|
@@ -128,32 +132,62 @@ private string PrintToString(object obj, int nestingLevel) | |
var type = obj.GetType(); | ||
var sb = new StringBuilder().AppendLine(type.Name); | ||
|
||
|
||
HandleMembers(type, sb, indentation, obj, nestingLevel); | ||
|
||
|
||
|
||
|
||
return sb.ToString(); | ||
} | ||
|
||
private void HandleMembers(Type type, StringBuilder sb, string indentation, object obj, int nestingLevel) | ||
{ | ||
foreach (var propertyInfo in type.GetProperties()) | ||
{ | ||
if (excludedProperties.Any(memberInfo => memberInfo.Name == propertyInfo.Name) || | ||
excludedTypes.Contains(propertyInfo.PropertyType)) | ||
continue; | ||
|
||
if (membersSerializesInfos.TryGetValue(propertyInfo, out var serializeMember)) | ||
{ | ||
sb.Append(GetSerializedString(obj, propertyInfo, indentation, serializeMember)); | ||
continue; | ||
} | ||
|
||
if (typeSerializesInfos.TryGetValue(propertyInfo.PropertyType, out var serializeType)) | ||
{ | ||
sb.Append(GetSerializedString(obj, propertyInfo, indentation, serializeType)); | ||
continue; | ||
} | ||
|
||
sb.Append($"{indentation}{propertyInfo.Name} = " + | ||
PrintToString(propertyInfo.GetValue(obj), | ||
nestingLevel + 1)); | ||
DataMember data = new DataMember(propertyInfo); | ||
This comment was marked as resolved.
Sorry, something went wrong. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. у тебя просто propertyInfo заворачивается в DataMember - зачем? Почему бы не просто передавать MemberInfo в метод А еще можно сделать финт ушами, чтобы по разному не обрабатывать поля и свойства:
|
||
|
||
HandleMember(sb, data, obj, indentation, nestingLevel); | ||
} | ||
|
||
return sb.ToString(); | ||
foreach (var fieldInfo in type.GetFields()) | ||
{ | ||
DataMember data = new DataMember(fieldInfo); | ||
|
||
HandleMember(sb, data, obj, indentation, nestingLevel); | ||
} | ||
} | ||
|
||
private void HandleMember(StringBuilder sb, DataMember member, object obj, string indentation, int nestingLevel) | ||
This comment was marked as resolved.
Sorry, something went wrong. |
||
{ | ||
if (excludedProperties.Any(memberInfo => memberInfo.Name == member.Name) || | ||
excludedTypes.Contains(member.Type)) | ||
return; | ||
|
||
if (membersSerializesInfos.TryGetValue(member.MemberInfo, out var serializeMember)) | ||
{ | ||
sb.Append(GetSerializedString(obj, member, indentation, serializeMember)); | ||
return; | ||
} | ||
|
||
if (typeSerializesInfos.TryGetValue(member.Type, out var serializeType)) | ||
{ | ||
sb.Append(GetSerializedString(obj, member, indentation, serializeType)); | ||
return; | ||
} | ||
|
||
sb.Append( | ||
GetSerializedString( | ||
obj, | ||
member, | ||
indentation, | ||
(value) => PrintToString( | ||
value, | ||
nestingLevel + 1), | ||
false)); | ||
} | ||
|
||
|
||
private bool MaxRecursionHasBeenReached(object obj) | ||
{ | ||
complexObjectLinks.TryAdd(obj, 0); | ||
|
@@ -162,12 +196,17 @@ private bool MaxRecursionHasBeenReached(object obj) | |
return complexObjectLinks[obj] == maxRecursion; | ||
} | ||
|
||
private string GetSerializedString(object obj, PropertyInfo propertyInfo, string indentation, Func<object, string> serializeMember) | ||
private string GetSerializedString( | ||
object obj, | ||
DataMember memberInfo, | ||
string indentation, | ||
Func<object, string> serializeMember, | ||
bool needNewLine = true) | ||
{ | ||
var serializedString = serializeMember(propertyInfo.GetValue(obj)); | ||
var serializedString = serializeMember(memberInfo.GetValue(obj)); | ||
var stringEnd = needNewLine ? Environment.NewLine : string.Empty; | ||
|
||
return $"{indentation}{propertyInfo.Name} = {serializedString}{Environment.NewLine}"; | ||
return $"{indentation}{memberInfo.Name} = {serializedString}{stringEnd}"; | ||
} | ||
|
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
using System; | ||
|
||
namespace ObjectPrinting.Tests | ||
namespace ObjectPrintingTests | ||
{ | ||
public class Person | ||
{ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,4 @@ | ||
using System; | ||
|
||
namespace ObjectPrinting.Tests | ||
namespace ObjectPrintingTests | ||
{ | ||
public class SubPerson | ||
{ | ||
|
This comment was marked as resolved.
Sorry, something went wrong.