Skip to content

Commit

Permalink
Исправил, часть связанную с свойствами
Browse files Browse the repository at this point in the history
  • Loading branch information
Pasha0666 committed Nov 29, 2019
1 parent a25b938 commit 1d8ae48
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 25 deletions.
5 changes: 3 additions & 2 deletions ObjectPrinting/IPrintingConfig.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Reflection;

namespace ObjectPrinting
{
internal interface IPrintingConfig
{
Dictionary<Type, Delegate> TypePrintingFunctions { get; }
Dictionary<string, Delegate> PropertyPrintingFunctions { get; }
Dictionary<string, int> PropertyStringsLength { get; }
Dictionary<MemberInfo, Delegate> PropertyPrintingFunctions { get; }
Dictionary<MemberInfo, int> PropertyStringsLength { get; }
Dictionary<Type, CultureInfo> TypeCultures { get; }
}
}
25 changes: 13 additions & 12 deletions ObjectPrinting/PrintingConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,24 @@ public class PrintingConfig<TOwner> : IPrintingConfig
public PrintingConfig()
{
typeExclusions = new HashSet<Type>();
propertyExclusions = new HashSet<string>();
propertyExclusions = new HashSet<MemberInfo>();
typePrintingFunctions = new Dictionary<Type, Delegate>();
typeCultures = new Dictionary<Type, CultureInfo>();
propertyPrintingFunctions = new Dictionary<string, Delegate>();
propertyStringsLength = new Dictionary<string, int>();
propertyPrintingFunctions = new Dictionary<MemberInfo, Delegate>();
propertyStringsLength = new Dictionary<MemberInfo, int>();
}

private readonly HashSet<Type> typeExclusions;
private readonly HashSet<string> propertyExclusions;
private readonly HashSet<MemberInfo> propertyExclusions;
private readonly Dictionary<Type, Delegate> typePrintingFunctions;
private readonly Dictionary<Type, CultureInfo> typeCultures;
private readonly Dictionary<string, Delegate> propertyPrintingFunctions;
private readonly Dictionary<string, int> propertyStringsLength;
private readonly Dictionary<MemberInfo, Delegate> propertyPrintingFunctions;
private readonly Dictionary<MemberInfo, int> propertyStringsLength;

Dictionary<Type, Delegate> IPrintingConfig.TypePrintingFunctions => typePrintingFunctions;
Dictionary<Type, CultureInfo> IPrintingConfig.TypeCultures => typeCultures;
Dictionary<string, Delegate> IPrintingConfig.PropertyPrintingFunctions => propertyPrintingFunctions;
Dictionary<string, int> IPrintingConfig.PropertyStringsLength => propertyStringsLength;
Dictionary<MemberInfo, Delegate> IPrintingConfig.PropertyPrintingFunctions => propertyPrintingFunctions;
Dictionary<MemberInfo, int> IPrintingConfig.PropertyStringsLength => propertyStringsLength;

private readonly Type[] finalTypes = new[]
{
Expand All @@ -54,7 +54,7 @@ public PropertyPrintingConfig<TOwner, TPropType> Printing<TPropType>(Expression<
public PrintingConfig<TOwner> Excluding<TPropType>(Expression<Func<TOwner, TPropType>> memberSelector)
{
if (memberSelector.Body is MemberExpression memberExpression)
propertyExclusions.Add(memberExpression.Member.Name);
propertyExclusions.Add(memberExpression.Member);
return this;
}

Expand All @@ -71,12 +71,13 @@ public string PrintToString(TOwner obj)

private string MemberToString(object value, MemberInfo memberInfo, int nestingLevel, List<object> serializeObjects)
{
var test = memberInfo.Module;
string valueString;
if (propertyPrintingFunctions.TryGetValue(memberInfo.Name, out var printingFunc))
if (propertyPrintingFunctions.TryGetValue(memberInfo, out var printingFunc))
valueString = (string) printingFunc.DynamicInvoke(value);
else
valueString = PrintToString(value, nestingLevel + 1, serializeObjects);
if (propertyStringsLength.TryGetValue(memberInfo.Name, out var length))
if (propertyStringsLength.TryGetValue(memberInfo, out var length))
valueString = valueString.Substring(0, length);
return new string('\t', nestingLevel + 1) + memberInfo.Name + " = " + valueString;
}
Expand Down Expand Up @@ -113,7 +114,7 @@ private string PrintToString(object obj, int nestingLevel, List<object> serializ
}

foreach (var propertyInfo in type.GetProperties())
if (!typeExclusions.Contains(propertyInfo.PropertyType) && !propertyExclusions.Contains(propertyInfo.Name))
if (!typeExclusions.Contains(propertyInfo.PropertyType) && !propertyExclusions.Contains(propertyInfo))
sb.AppendLine(MemberToString(propertyInfo.GetValue(obj), propertyInfo, nestingLevel, serializeObjects));
foreach (var fieldInfo in type.GetFields())
if ((obj.GetType().IsClass || !fieldInfo.IsStatic) && !typeExclusions.Contains(fieldInfo.FieldType) &&
Expand Down
2 changes: 1 addition & 1 deletion ObjectPrinting/PropertyPrintingConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public PrintingConfig<TOwner> Using(Func<TPropType, string> print)
if (memberSelector == null)
((IPrintingConfig) printingConfig).TypePrintingFunctions[typeof(TPropType)] = print;
else if (memberSelector.Body is MemberExpression memberExpression)
((IPrintingConfig) printingConfig).PropertyPrintingFunctions[memberExpression.Member.Name] = print;
((IPrintingConfig) printingConfig).PropertyPrintingFunctions[memberExpression.Member] = print;
return printingConfig;
}

Expand Down
2 changes: 1 addition & 1 deletion ObjectPrinting/PropertyPrintingConfigExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public static PrintingConfig<TOwner> TrimmedToLength<TOwner>(this PropertyPrinti
{
var parentConfig = ((IPropertyPrintingConfig<TOwner, string>)propConfig).ParentConfig;
if (((IPropertyPrintingConfig<TOwner, string>) propConfig).MemberSelector.Body is MemberExpression memberExpression)
((IPrintingConfig) parentConfig).PropertyStringsLength[memberExpression.Member.Name] = maxLen;
((IPrintingConfig) parentConfig).PropertyStringsLength[memberExpression.Member] = maxLen;
return ((IPropertyPrintingConfig<TOwner, string>)propConfig).ParentConfig;
}
}
Expand Down
1 change: 1 addition & 0 deletions ObjectPrinting/Tests/A.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
public class A
{
public string X { get; set; }
public string Y { get; set; }
public B B { get; set; }
}
}
24 changes: 15 additions & 9 deletions ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ public void OneTimeSetUp()
person.Dictionary.Add(new Person{Name = "Misha"}, "456");
person.Dictionary.Add(new Person {Name = "Dasha"}, "789");
person.Dictionary.Add(person, "09876");
a = new A {X = "xxx"};
var b = new B {Y = "yyy", A = a};
a = new A {X = "xxx", Y = "ay"};
var b = new B {Y = "by", A = a};
a.B = b;
}

Expand All @@ -32,8 +32,8 @@ public void TuneSubobjectExcluding()
.Excluding(a => a.B.Y);

var str = config.PrintToString(a);
str.Should().NotContain("yyy");
str.Should().NotContain("Y");
str.Should().Contain("ay");
str.Should().NotContain("by");
}

[Test]
Expand All @@ -46,12 +46,18 @@ public void PrintingIEnumerable()
str.Should().Contain("4");
str.Should().Contain("5");
}





[Test]
public void PrintingDictionary()
{
var str = person.PrintToString();
str.Should().Contain("Pasha");
str.Should().Contain("Misha");
str.Should().Contain("Dasha");
}

[Test]
public void Demo()
public void CompilingMethods()
{
var printer = ObjectPrinter.For<Person>()
//1. Исключить из сериализации свойства определенного типа +
Expand Down

0 comments on commit 1d8ae48

Please sign in to comment.