-
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
Зубакин Ренат #196
base: master
Are you sure you want to change the base?
Зубакин Ренат #196
Changes from 5 commits
e390215
28ca9f5
5f22449
28e8262
c404994
216d295
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,17 @@ | ||
using System; | ||
|
||
namespace ObjectPrinting.Extensions | ||
{ | ||
public static class ObjectExtensions | ||
{ | ||
public static string PrintToString<T>(this T obj) | ||
{ | ||
return ObjectPrinter.For<T>().PrintToString(obj); | ||
} | ||
|
||
public static string PrintToString<T>(this T obj, Func<PrintingConfig<T>,PrintingConfig<T>> func) | ||
{ | ||
return func(ObjectPrinter.For<T>()).PrintToString(obj); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
namespace ObjectPrinting.Extensions | ||
{ | ||
public static class PropertyPrintingConfigExtension | ||
{ | ||
public static PrintingConfig<TOwner> TrimmedStringProperty<TOwner>( | ||
this PropertyPrintingConfig<TOwner, string> printingConfig, int length) | ||
{ | ||
return printingConfig.SetSerializer(str => str.Length <= length ? str : str.Substring(0, length)); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,189 @@ | ||
using System; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using System.Globalization; | ||
using System.Linq; | ||
using System.Linq.Expressions; | ||
using System.Reflection; | ||
using System.Text; | ||
|
||
namespace ObjectPrinting | ||
{ | ||
public class PrintingConfig<TOwner> | ||
{ | ||
private static readonly HashSet<Type> FinalTypes = new HashSet<Type>() | ||
{ | ||
typeof(int), | ||
typeof(double), | ||
typeof(float), | ||
typeof(string), | ||
typeof(DateTime), | ||
typeof(TimeSpan), | ||
typeof(Guid) | ||
}; | ||
|
||
private readonly HashSet<Type> excludedTypes = new HashSet<Type>(); | ||
private readonly HashSet<MemberInfo> excludedProperty = new HashSet<MemberInfo>(); | ||
|
||
private readonly Dictionary<MemberInfo, Delegate> customPropertySerialize = | ||
new Dictionary<MemberInfo, Delegate>(); | ||
|
||
private readonly Dictionary<Type, Delegate> customTypeSerializers = new Dictionary<Type, Delegate>(); | ||
private HashSet<object> visitedObjects = new HashSet<object>(); | ||
|
||
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. Этим множеством ты убиваешь одновременный запуск сериализаторого твоего |
||
|
||
public string PrintToString(TOwner obj) | ||
{ | ||
return PrintToString(obj, 0); | ||
} | ||
|
||
private string PrintToString(object obj, int nestingLevel) | ||
{ | ||
var builder = new StringBuilder(); | ||
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. Кадется лишнее поле |
||
//TODO apply configurations | ||
if (obj == null) | ||
return "null" + Environment.NewLine; | ||
return "null"; | ||
|
||
if (visitedObjects.Contains(obj)) | ||
{ | ||
return $"Cycling references"; | ||
} | ||
|
||
visitedObjects.Add(obj); | ||
|
||
if (FinalTypes.Contains(obj.GetType())) | ||
return obj.ToString(); | ||
|
||
if (obj is IDictionary) | ||
{ | ||
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. obj is IDictionary dictionary |
||
return PrintToStringIDictionary((IDictionary)obj, nestingLevel + 1); | ||
} | ||
|
||
if (obj is IEnumerable) | ||
{ | ||
return PrintToStringIEnumerable((IEnumerable)obj, nestingLevel + 1); | ||
} | ||
|
||
var finalTypes = new[] | ||
return PrintToStringProperties(obj, nestingLevel + 1); | ||
} | ||
|
||
private string PrintToStringProperties(object obj, int nestingLevel) | ||
{ | ||
var identation = new string('\t', nestingLevel); | ||
var builder = new StringBuilder(); | ||
var objType = obj.GetType(); | ||
builder.Append($"{objType.Name}"); | ||
foreach (var propertyInfo in GetIncludedProperties(objType)) | ||
{ | ||
typeof(int), typeof(double), typeof(float), typeof(string), | ||
typeof(DateTime), typeof(TimeSpan) | ||
}; | ||
if (finalTypes.Contains(obj.GetType())) | ||
return obj + Environment.NewLine; | ||
var value = propertyInfo.GetValue(obj); | ||
var customSerializeStr = TryUseCustomSerializer(value, propertyInfo, out var str) | ||
? str | ||
: PrintToString(value, nestingLevel + 1); | ||
builder.Append('\n' + identation + propertyInfo.Name + " = " + customSerializeStr); | ||
} | ||
|
||
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()) | ||
visitedObjects.Remove(obj); | ||
return builder.ToString(); | ||
} | ||
|
||
private string PrintToStringIEnumerable(IEnumerable collection, int nestingLevel) | ||
{ | ||
var identation = new string('\t', nestingLevel); | ||
var builder = new StringBuilder(); | ||
|
||
foreach (var item in collection) | ||
{ | ||
sb.Append(identation + propertyInfo.Name + " = " + | ||
PrintToString(propertyInfo.GetValue(obj), | ||
nestingLevel + 1)); | ||
builder.Append('\n' + identation + PrintToString(item, nestingLevel + 1)); | ||
} | ||
return sb.ToString(); | ||
|
||
visitedObjects.Remove(collection); | ||
return builder.ToString(); | ||
} | ||
|
||
private string PrintToStringIDictionary(IDictionary dictionary, int nestingLevel) | ||
{ | ||
var identation = new string('\t', nestingLevel); | ||
var builder = new StringBuilder(); | ||
|
||
foreach (var key in dictionary.Keys) | ||
{ | ||
builder.Append('\n' + identation + PrintToString(key, nestingLevel + 1) + " = " + | ||
PrintToString(dictionary[key], nestingLevel + 1)); | ||
} | ||
|
||
visitedObjects.Remove(dictionary); | ||
return builder.ToString(); | ||
} | ||
|
||
private bool TryUseCustomSerializer(object value, MemberInfo memberInfo, out string str) | ||
{ | ||
str = null; | ||
if (customPropertySerialize.ContainsKey(memberInfo)) | ||
{ | ||
str = (string)customPropertySerialize[memberInfo].DynamicInvoke(value); | ||
return true; | ||
} | ||
|
||
if (customTypeSerializers.ContainsKey(memberInfo.GetType())) | ||
{ | ||
str = (string)customTypeSerializers[memberInfo.GetType()].DynamicInvoke(value); | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
private IEnumerable<PropertyInfo> GetIncludedProperties(Type type) | ||
{ | ||
return type.GetRuntimeProperties().Where(p => !IsExclude(p)); | ||
} | ||
|
||
private bool IsExclude(MemberInfo member) | ||
{ | ||
return excludedTypes.Contains(member.GetType()) || excludedProperty.Contains(member); | ||
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 и member.PropertyType сработает все |
||
} | ||
|
||
public PrintingConfig<TOwner> Exclude<TType>() | ||
{ | ||
excludedTypes.Add(typeof(TType)); | ||
return this; | ||
} | ||
|
||
public PrintingConfig<TOwner> Exclude<TProperty>(Expression<Func<TOwner, TProperty>> func) | ||
{ | ||
var memberInfo = func.Body as MemberExpression; | ||
if (memberInfo == null) | ||
throw new ArgumentException(); | ||
excludedProperty.Add(memberInfo.Member); | ||
return this; | ||
} | ||
|
||
|
||
public PrintingConfig<TOwner> SetCustomTypeSerializer<TType>(Func<TType, string> serializer) | ||
{ | ||
customTypeSerializers[typeof(TType)] = serializer; | ||
return this; | ||
} | ||
|
||
public PropertyPrintingConfig<TOwner, TProperty> SerializeByProperty<TProperty>( | ||
Expression<Func<TOwner, TProperty>> func) | ||
{ | ||
var memberInfo = func.Body as MemberExpression; | ||
if (memberInfo == null) | ||
throw new ArgumentException(); | ||
return new PropertyPrintingConfig<TOwner, TProperty>(this, memberInfo.Member); | ||
} | ||
|
||
public PrintingConfig<TOwner> SetCustomPropertySerializer<TProperty>(MemberInfo property, | ||
Func<TProperty, string> serializer) | ||
{ | ||
customPropertySerialize[property] = serializer; | ||
return this; | ||
} | ||
|
||
public PrintingConfig<TOwner> SetCulture<TType>(string format, CultureInfo culture) where TType : IFormattable | ||
{ | ||
return SetCustomTypeSerializer((TType obj) => obj.ToString(format, culture)); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
using System; | ||
using System.Reflection; | ||
|
||
namespace ObjectPrinting | ||
{ | ||
public class PropertyPrintingConfig<TOwner, TProperty> | ||
{ | ||
private readonly PrintingConfig<TOwner> printingConfig; | ||
private readonly MemberInfo property; | ||
|
||
public PropertyPrintingConfig(PrintingConfig<TOwner> printingConfig, MemberInfo property) | ||
{ | ||
this.printingConfig = printingConfig; | ||
this.property = property; | ||
} | ||
|
||
public PrintingConfig<TOwner> SetSerializer(Func<TProperty, string> serializer) | ||
{ | ||
printingConfig.SetCustomPropertySerializer(property, serializer); | ||
return printingConfig; | ||
} | ||
} | ||
} |
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.
Тут же явно можно положить какая функция тут лежит