-
Notifications
You must be signed in to change notification settings - Fork 213
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 all commits
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,29 @@ | ||
using System; | ||
using System.Reflection; | ||
|
||
namespace ObjectPrinting | ||
{ | ||
public class DataMember | ||
{ | ||
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; | ||
} | ||
|
||
public string Name { get; } | ||
public Type Type { get; } | ||
public Func<object, object> GetValue { get; } | ||
public MemberInfo MemberInfo { get; } | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
namespace ObjectPrinting | ||
{ | ||
public static class PrinterExtensions | ||
{ | ||
public static PrintingConfig<T> CreatePrinter<T>(this T instance) | ||
{ | ||
return ObjectPrinter.For<T>(); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,98 @@ | ||
using System; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Collections.Generic; | ||
using System.Linq.Expressions; | ||
using System.Reflection; | ||
using ObjectPrinting.Serialization; | ||
|
||
namespace ObjectPrinting | ||
{ | ||
public class PrintingConfig<TOwner> | ||
This comment was marked as resolved.
Sorry, something went wrong.
This comment was marked as resolved.
Sorry, something went wrong. |
||
{ | ||
private readonly HashSet<MemberInfo> excludedMembers = new HashSet<MemberInfo>(); | ||
private readonly HashSet<Type> excludedTypes = new HashSet<Type>(); | ||
|
||
private readonly Dictionary<MemberInfo, IHasSerializationFunc> membersSerializesInfos = | ||
new Dictionary<MemberInfo, IHasSerializationFunc>(); | ||
|
||
private readonly Dictionary<Type, IHasSerializationFunc> typeSerializesInfos = | ||
new Dictionary<Type, IHasSerializationFunc>(); | ||
|
||
private Func<object, string> handleMaxRecursion; | ||
|
||
private readonly int maxRecursion = 1; | ||
|
||
public string PrintToString(TOwner obj) | ||
This comment was marked as resolved.
Sorry, something went wrong. |
||
{ | ||
return PrintToString(obj, 0); | ||
} | ||
|
||
public PrintingConfig<TOwner> OnMaxRecursion(Func<object,string> func) | ||
{ | ||
handleMaxRecursion = func; | ||
|
||
return this; | ||
} | ||
|
||
public PrintingConfig<TOwner> Exclude<TPropType>(Expression<Func<TOwner, TPropType>> exclude) | ||
{ | ||
if (exclude == null) | ||
throw new ArgumentNullException(); | ||
|
||
var memberInfo = GetMemberInfo(exclude); | ||
excludedMembers.Add(memberInfo); | ||
|
||
return this; | ||
} | ||
|
||
private static MemberInfo GetMemberInfo<TPropType>(Expression<Func<TOwner, TPropType>> expression) | ||
{ | ||
var memberExpression = expression.Body is UnaryExpression unaryExpression | ||
? (MemberExpression)unaryExpression.Operand | ||
: (MemberExpression)expression.Body; | ||
|
||
return memberExpression.Member; | ||
} | ||
|
||
|
||
public PrintingConfig<TOwner> Exclude<TPropType>() | ||
{ | ||
excludedTypes.Add(typeof(TPropType)); | ||
|
||
return this; | ||
} | ||
|
||
private string PrintToString(object obj, int nestingLevel) | ||
This comment was marked as resolved.
Sorry, something went wrong.
This comment was marked as resolved.
Sorry, something went wrong. |
||
{ | ||
//TODO apply configurations | ||
if (obj == null) | ||
return "null" + Environment.NewLine; | ||
|
||
var finalTypes = new[] | ||
{ | ||
typeof(int), typeof(double), typeof(float), typeof(string), | ||
typeof(DateTime), typeof(TimeSpan) | ||
}; | ||
if (finalTypes.Contains(obj.GetType())) | ||
return obj + Environment.NewLine; | ||
|
||
var identation = new string('\t', nestingLevel + 1); | ||
var sb = new StringBuilder(); | ||
var type = obj.GetType(); | ||
sb.AppendLine(type.Name); | ||
foreach (var propertyInfo in type.GetProperties()) | ||
{ | ||
sb.Append(identation + propertyInfo.Name + " = " + | ||
PrintToString(propertyInfo.GetValue(obj), | ||
nestingLevel + 1)); | ||
} | ||
return sb.ToString(); | ||
var serializer = new Serializer( | ||
excludedMembers, | ||
excludedTypes, | ||
membersSerializesInfos, | ||
typeSerializesInfos, | ||
maxRecursion, | ||
handleMaxRecursion); | ||
|
||
return serializer.Serialize(obj, nestingLevel); | ||
} | ||
|
||
public IUsing<TOwner, T> Printing<T>() | ||
{ | ||
var config = new SerializationConfig<TOwner, T>(this); | ||
typeSerializesInfos[typeof(T)] = config; | ||
|
||
return config; | ||
} | ||
|
||
public IUsing<TOwner, T> Printing<T>(Expression<Func<TOwner, T>> property) | ||
{ | ||
if (property == null) | ||
throw new ArgumentNullException(); | ||
|
||
var memberInfo = GetMemberInfo(property); | ||
|
||
var config = new SerializationConfig<TOwner, T>(this); | ||
membersSerializesInfos[memberInfo] = config; | ||
|
||
return config; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
using System; | ||
|
||
namespace ObjectPrinting.Serialization | ||
{ | ||
public interface IHasSerializationFunc | ||
{ | ||
Func<object, string> SerializationFunc { get; } | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
namespace ObjectPrinting.Serialization | ||
{ | ||
public interface ISerializationConfig<TOwner, TSerialization> : | ||
IWrap<TOwner>, | ||
IUsing<TOwner, TSerialization>, | ||
IHasSerializationFunc | ||
{ | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
using System; | ||
|
||
namespace ObjectPrinting.Serialization | ||
{ | ||
public interface IUsing<TOwner, TSerialization> | ||
{ | ||
public IWrap<TOwner> Using(Func<TSerialization, string> serialize); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using System; | ||
|
||
namespace ObjectPrinting.Serialization | ||
{ | ||
public interface IWrap<TOwner> | ||
{ | ||
PrintingConfig<TOwner> And { get; } | ||
|
||
IWrap<TOwner> Wrap(Func<string, string> modify); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
using System; | ||
|
||
namespace ObjectPrinting.Serialization | ||
{ | ||
public class SerializationConfig<TOwner, TSerialization> : ISerializationConfig<TOwner, TSerialization> | ||
{ | ||
private Func<TSerialization, string> serialize; | ||
|
||
public SerializationConfig(PrintingConfig<TOwner> printingConfig) | ||
{ | ||
And = printingConfig; | ||
} | ||
|
||
public PrintingConfig<TOwner> And { get; } | ||
|
||
public IWrap<TOwner> Using(Func<TSerialization, string> serialize) | ||
{ | ||
if (serialize == null) | ||
throw new ArgumentNullException(); | ||
|
||
this.serialize = serialize; | ||
return this; | ||
} | ||
|
||
public IWrap<TOwner> Wrap(Func<string, string> modify) | ||
{ | ||
if (modify == null) | ||
throw new ArgumentNullException(); | ||
|
||
var currentFunc = serialize; | ||
serialize = value => modify(currentFunc(value)); | ||
return this; | ||
} | ||
|
||
public Func<object, string> SerializationFunc => | ||
p => serialize((TSerialization)p); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Все комменты обсуждаемы - можем обсуждать или в тредах, или в личке в телеге
За количество комментов не парься - это нормально, процесс обучения, повышения скилла, ревью именно для этого и нужно
На пофикшенные комментарии можно ставить реакции или писать пометочки, что и почему