From 5b61d869754b2e30e32dbba7d3420cf2028343ee Mon Sep 17 00:00:00 2001
From: Kholstinin Yegor <99079920+Yrwlcm@users.noreply.github.com>
Date: Sun, 24 Dec 2023 00:18:19 +0500
Subject: [PATCH 01/13] Project refactoring
---
.../{Solved => }/ObjectExtensions.cs | 2 +-
ObjectPrinting/ObjectPrinting.csproj | 5 --
ObjectPrinting/PrintingConfig.cs | 23 ++++++-
.../{Solved => }/PropertyPrintingConfig.cs | 2 +-
.../PropertyPrintingConfigExtensions.cs | 2 +-
ObjectPrinting/Solved/ObjectPrinter.cs | 10 ---
ObjectPrinting/Solved/PrintingConfig.cs | 62 -------------------
ObjectPrinting/Solved/Tests/Person.cs | 12 ----
.../Tests/ObjectPrinterAcceptanceTests.cs | 27 --------
.../ObjectPrinterAcceptanceTests.cs | 7 ++-
.../ObjectPrinting_Should.csproj | 24 +++++++
.../Tests => ObjectPrinting_Should}/Person.cs | 2 +-
ObjectPrinting_Should/Usings.cs | 1 +
fluent-api.sln | 18 ++++--
14 files changed, 67 insertions(+), 130 deletions(-)
rename ObjectPrinting/{Solved => }/ObjectExtensions.cs (85%)
rename ObjectPrinting/{Solved => }/PropertyPrintingConfig.cs (96%)
rename ObjectPrinting/{Solved => }/PropertyPrintingConfigExtensions.cs (94%)
delete mode 100644 ObjectPrinting/Solved/ObjectPrinter.cs
delete mode 100644 ObjectPrinting/Solved/PrintingConfig.cs
delete mode 100644 ObjectPrinting/Solved/Tests/Person.cs
delete mode 100644 ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.cs
rename {ObjectPrinting/Solved/Tests => ObjectPrinting_Should}/ObjectPrinterAcceptanceTests.cs (96%)
create mode 100644 ObjectPrinting_Should/ObjectPrinting_Should.csproj
rename {ObjectPrinting/Tests => ObjectPrinting_Should}/Person.cs (87%)
create mode 100644 ObjectPrinting_Should/Usings.cs
diff --git a/ObjectPrinting/Solved/ObjectExtensions.cs b/ObjectPrinting/ObjectExtensions.cs
similarity index 85%
rename from ObjectPrinting/Solved/ObjectExtensions.cs
rename to ObjectPrinting/ObjectExtensions.cs
index b0c94553..87976203 100644
--- a/ObjectPrinting/Solved/ObjectExtensions.cs
+++ b/ObjectPrinting/ObjectExtensions.cs
@@ -1,4 +1,4 @@
-namespace ObjectPrinting.Solved
+namespace ObjectPrinting
{
public static class ObjectExtensions
{
diff --git a/ObjectPrinting/ObjectPrinting.csproj b/ObjectPrinting/ObjectPrinting.csproj
index 1c5eaf1c..d7f3a084 100644
--- a/ObjectPrinting/ObjectPrinting.csproj
+++ b/ObjectPrinting/ObjectPrinting.csproj
@@ -6,9 +6,4 @@
false
-
-
-
-
-
diff --git a/ObjectPrinting/PrintingConfig.cs b/ObjectPrinting/PrintingConfig.cs
index a9e08211..1433d519 100644
--- a/ObjectPrinting/PrintingConfig.cs
+++ b/ObjectPrinting/PrintingConfig.cs
@@ -1,11 +1,32 @@
-using System;
+using System;
using System.Linq;
+using System.Linq.Expressions;
using System.Text;
namespace ObjectPrinting
{
public class PrintingConfig
{
+ public PropertyPrintingConfig Printing()
+ {
+ return new PropertyPrintingConfig(this);
+ }
+
+ public PropertyPrintingConfig Printing(Expression> memberSelector)
+ {
+ return new PropertyPrintingConfig(this);
+ }
+
+ public PrintingConfig Excluding(Expression> memberSelector)
+ {
+ return this;
+ }
+
+ public PrintingConfig Excluding()
+ {
+ return this;
+ }
+
public string PrintToString(TOwner obj)
{
return PrintToString(obj, 0);
diff --git a/ObjectPrinting/Solved/PropertyPrintingConfig.cs b/ObjectPrinting/PropertyPrintingConfig.cs
similarity index 96%
rename from ObjectPrinting/Solved/PropertyPrintingConfig.cs
rename to ObjectPrinting/PropertyPrintingConfig.cs
index a509697d..84f537b8 100644
--- a/ObjectPrinting/Solved/PropertyPrintingConfig.cs
+++ b/ObjectPrinting/PropertyPrintingConfig.cs
@@ -1,7 +1,7 @@
using System;
using System.Globalization;
-namespace ObjectPrinting.Solved
+namespace ObjectPrinting
{
public class PropertyPrintingConfig : IPropertyPrintingConfig
{
diff --git a/ObjectPrinting/Solved/PropertyPrintingConfigExtensions.cs b/ObjectPrinting/PropertyPrintingConfigExtensions.cs
similarity index 94%
rename from ObjectPrinting/Solved/PropertyPrintingConfigExtensions.cs
rename to ObjectPrinting/PropertyPrintingConfigExtensions.cs
index dd392239..1e192203 100644
--- a/ObjectPrinting/Solved/PropertyPrintingConfigExtensions.cs
+++ b/ObjectPrinting/PropertyPrintingConfigExtensions.cs
@@ -1,6 +1,6 @@
using System;
-namespace ObjectPrinting.Solved
+namespace ObjectPrinting
{
public static class PropertyPrintingConfigExtensions
{
diff --git a/ObjectPrinting/Solved/ObjectPrinter.cs b/ObjectPrinting/Solved/ObjectPrinter.cs
deleted file mode 100644
index 540ee769..00000000
--- a/ObjectPrinting/Solved/ObjectPrinter.cs
+++ /dev/null
@@ -1,10 +0,0 @@
-namespace ObjectPrinting.Solved
-{
- public class ObjectPrinter
- {
- public static PrintingConfig For()
- {
- return new PrintingConfig();
- }
- }
-}
\ No newline at end of file
diff --git a/ObjectPrinting/Solved/PrintingConfig.cs b/ObjectPrinting/Solved/PrintingConfig.cs
deleted file mode 100644
index 0ec5aeb2..00000000
--- a/ObjectPrinting/Solved/PrintingConfig.cs
+++ /dev/null
@@ -1,62 +0,0 @@
-using System;
-using System.Linq;
-using System.Linq.Expressions;
-using System.Text;
-
-namespace ObjectPrinting.Solved
-{
- public class PrintingConfig
- {
- public PropertyPrintingConfig Printing()
- {
- return new PropertyPrintingConfig(this);
- }
-
- public PropertyPrintingConfig Printing(Expression> memberSelector)
- {
- return new PropertyPrintingConfig(this);
- }
-
- public PrintingConfig Excluding(Expression> memberSelector)
- {
- return this;
- }
-
- internal PrintingConfig Excluding()
- {
- return this;
- }
-
- public string PrintToString(TOwner obj)
- {
- return PrintToString(obj, 0);
- }
-
- private string PrintToString(object obj, int nestingLevel)
- {
- //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();
- }
- }
-}
\ No newline at end of file
diff --git a/ObjectPrinting/Solved/Tests/Person.cs b/ObjectPrinting/Solved/Tests/Person.cs
deleted file mode 100644
index 858ebbf8..00000000
--- a/ObjectPrinting/Solved/Tests/Person.cs
+++ /dev/null
@@ -1,12 +0,0 @@
-using System;
-
-namespace ObjectPrinting.Solved.Tests
-{
- public class Person
- {
- public Guid Id { get; set; }
- public string Name { get; set; }
- public double Height { get; set; }
- public int Age { get; set; }
- }
-}
\ No newline at end of file
diff --git a/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.cs b/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.cs
deleted file mode 100644
index 4c8b2445..00000000
--- a/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.cs
+++ /dev/null
@@ -1,27 +0,0 @@
-using NUnit.Framework;
-
-namespace ObjectPrinting.Tests
-{
- [TestFixture]
- public class ObjectPrinterAcceptanceTests
- {
- [Test]
- public void Demo()
- {
- var person = new Person { Name = "Alex", Age = 19 };
-
- var printer = ObjectPrinter.For();
- //1. Исключить из сериализации свойства определенного типа
- //2. Указать альтернативный способ сериализации для определенного типа
- //3. Для числовых типов указать культуру
- //4. Настроить сериализацию конкретного свойства
- //5. Настроить обрезание строковых свойств (метод должен быть виден только для строковых свойств)
- //6. Исключить из сериализации конкретного свойства
-
- string s1 = printer.PrintToString(person);
-
- //7. Синтаксический сахар в виде метода расширения, сериализующего по-умолчанию
- //8. ...с конфигурированием
- }
- }
-}
\ No newline at end of file
diff --git a/ObjectPrinting/Solved/Tests/ObjectPrinterAcceptanceTests.cs b/ObjectPrinting_Should/ObjectPrinterAcceptanceTests.cs
similarity index 96%
rename from ObjectPrinting/Solved/Tests/ObjectPrinterAcceptanceTests.cs
rename to ObjectPrinting_Should/ObjectPrinterAcceptanceTests.cs
index ac52d5ee..7e20d7b3 100644
--- a/ObjectPrinting/Solved/Tests/ObjectPrinterAcceptanceTests.cs
+++ b/ObjectPrinting_Should/ObjectPrinterAcceptanceTests.cs
@@ -1,8 +1,9 @@
using System;
using System.Globalization;
using NUnit.Framework;
+using ObjectPrinting;
-namespace ObjectPrinting.Solved.Tests
+namespace ObjectPrinting_Should
{
[TestFixture]
public class ObjectPrinterAcceptanceTests
@@ -26,10 +27,10 @@ public void Demo()
.Excluding(p => p.Age);
string s1 = printer.PrintToString(person);
-
+
//7. Синтаксический сахар в виде метода расширения, сериализующего по-умолчанию
string s2 = person.PrintToString();
-
+
//8. ...с конфигурированием
string s3 = person.PrintToString(s => s.Excluding(p => p.Age));
Console.WriteLine(s1);
diff --git a/ObjectPrinting_Should/ObjectPrinting_Should.csproj b/ObjectPrinting_Should/ObjectPrinting_Should.csproj
new file mode 100644
index 00000000..fb63b262
--- /dev/null
+++ b/ObjectPrinting_Should/ObjectPrinting_Should.csproj
@@ -0,0 +1,24 @@
+
+
+
+ net6.0
+ enable
+ enable
+
+ false
+ true
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/ObjectPrinting/Tests/Person.cs b/ObjectPrinting_Should/Person.cs
similarity index 87%
rename from ObjectPrinting/Tests/Person.cs
rename to ObjectPrinting_Should/Person.cs
index f9555955..3eac91f3 100644
--- a/ObjectPrinting/Tests/Person.cs
+++ b/ObjectPrinting_Should/Person.cs
@@ -1,6 +1,6 @@
using System;
-namespace ObjectPrinting.Tests
+namespace ObjectPrinting_Should
{
public class Person
{
diff --git a/ObjectPrinting_Should/Usings.cs b/ObjectPrinting_Should/Usings.cs
new file mode 100644
index 00000000..cefced49
--- /dev/null
+++ b/ObjectPrinting_Should/Usings.cs
@@ -0,0 +1 @@
+global using NUnit.Framework;
\ No newline at end of file
diff --git a/fluent-api.sln b/fluent-api.sln
index 69c8db9e..2de370c9 100644
--- a/fluent-api.sln
+++ b/fluent-api.sln
@@ -1,17 +1,19 @@
Microsoft Visual Studio Solution File, Format Version 12.00
-# Visual Studio 14
-VisualStudioVersion = 14.0.25123.0
+# Visual Studio Version 17
+VisualStudioVersion = 17.6.33815.320
MinimumVisualStudioVersion = 10.0.40219.1
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ObjectPrinting", "ObjectPrinting\ObjectPrinting.csproj", "{07B8C9B7-8289-46CB-9875-048A57758EEE}"
+Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ObjectPrinting", "ObjectPrinting\ObjectPrinting.csproj", "{07B8C9B7-8289-46CB-9875-048A57758EEE}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Samples", "Samples", "{6D308E4A-CEC7-4536-9B87-81CD337A87AD}"
EndProject
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FluentMapping", "Samples\FluentMapper\FluentMapping.csproj", "{FEEA5AFE-459A-4D13-81D0-252E1A2E6F4E}"
+Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FluentMapping", "Samples\FluentMapper\FluentMapping.csproj", "{FEEA5AFE-459A-4D13-81D0-252E1A2E6F4E}"
EndProject
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FluentMapping.Tests", "Samples\FluentMapper.Tests\FluentMapping.Tests.csproj", "{8A7BB3EA-3E6A-4D04-A801-D5CD1620DA0D}"
+Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FluentMapping.Tests", "Samples\FluentMapper.Tests\FluentMapping.Tests.csproj", "{8A7BB3EA-3E6A-4D04-A801-D5CD1620DA0D}"
EndProject
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Spectacle", "Samples\Spectacle\Spectacle.csproj", "{EFA9335C-411B-4597-B0B6-5438D1AE04C3}"
+Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Spectacle", "Samples\Spectacle\Spectacle.csproj", "{EFA9335C-411B-4597-B0B6-5438D1AE04C3}"
+EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ObjectPrinting_Should", "ObjectPrinting_Should\ObjectPrinting_Should.csproj", "{46DA74C4-460B-4AA0-8647-E1204FF8125D}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -35,6 +37,10 @@ Global
{EFA9335C-411B-4597-B0B6-5438D1AE04C3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EFA9335C-411B-4597-B0B6-5438D1AE04C3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EFA9335C-411B-4597-B0B6-5438D1AE04C3}.Release|Any CPU.Build.0 = Release|Any CPU
+ {46DA74C4-460B-4AA0-8647-E1204FF8125D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {46DA74C4-460B-4AA0-8647-E1204FF8125D}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {46DA74C4-460B-4AA0-8647-E1204FF8125D}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {46DA74C4-460B-4AA0-8647-E1204FF8125D}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
From 6e7a1aaf1c190cad3079330b1451e787b0a12b65 Mon Sep 17 00:00:00 2001
From: Kholstinin Yegor <99079920+Yrwlcm@users.noreply.github.com>
Date: Sun, 24 Dec 2023 02:48:10 +0500
Subject: [PATCH 02/13] Tests and excluded realisation
---
ObjectPrinting/PrintingConfig.cs | 16 ++++
.../ObjectPrinterAcceptanceTests.cs | 79 ++++++++++++++++++-
.../ObjectPrinting_Should.csproj | 1 +
3 files changed, 94 insertions(+), 2 deletions(-)
diff --git a/ObjectPrinting/PrintingConfig.cs b/ObjectPrinting/PrintingConfig.cs
index 1433d519..4cfff79a 100644
--- a/ObjectPrinting/PrintingConfig.cs
+++ b/ObjectPrinting/PrintingConfig.cs
@@ -1,12 +1,17 @@
using System;
+using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
+using System.Reflection;
using System.Text;
namespace ObjectPrinting
{
public class PrintingConfig
{
+ private HashSet excludedTypes = new HashSet();
+ private HashSet excludedProperites = new HashSet();
+
public PropertyPrintingConfig Printing()
{
return new PropertyPrintingConfig(this);
@@ -19,11 +24,14 @@ public PropertyPrintingConfig Printing(Expression<
public PrintingConfig Excluding(Expression> memberSelector)
{
+ var propInfo = ((MemberExpression)memberSelector.Body).Member as PropertyInfo;
+ excludedProperites.Add(propInfo);
return this;
}
public PrintingConfig Excluding()
{
+ excludedTypes.Add(typeof(TPropType));
return this;
}
@@ -52,11 +60,19 @@ private string PrintToString(object obj, int nestingLevel)
sb.AppendLine(type.Name);
foreach (var propertyInfo in type.GetProperties())
{
+ if (Excluded(propertyInfo))
+ continue;
+
sb.Append(identation + propertyInfo.Name + " = " +
PrintToString(propertyInfo.GetValue(obj),
nestingLevel + 1));
}
return sb.ToString();
}
+
+ private bool Excluded(PropertyInfo propertyInfo)
+ {
+ return excludedTypes.Contains(propertyInfo.PropertyType) || excludedProperites.Contains(propertyInfo);
+ }
}
}
\ No newline at end of file
diff --git a/ObjectPrinting_Should/ObjectPrinterAcceptanceTests.cs b/ObjectPrinting_Should/ObjectPrinterAcceptanceTests.cs
index 7e20d7b3..ee3c68fc 100644
--- a/ObjectPrinting_Should/ObjectPrinterAcceptanceTests.cs
+++ b/ObjectPrinting_Should/ObjectPrinterAcceptanceTests.cs
@@ -1,5 +1,6 @@
using System;
using System.Globalization;
+using FluentAssertions;
using NUnit.Framework;
using ObjectPrinting;
@@ -8,11 +9,17 @@ namespace ObjectPrinting_Should
[TestFixture]
public class ObjectPrinterAcceptanceTests
{
+ private Person person = null!;
+
+ [SetUp]
+ public void Setup()
+ {
+ person = new Person { Name = "Alex", Age = 19, Height = 179.5, Id = new Guid() };
+ }
+
[Test]
public void Demo()
{
- var person = new Person { Name = "Alex", Age = 19 };
-
var printer = ObjectPrinter.For()
//1. Исключить из сериализации свойства определенного типа
.Excluding()
@@ -21,6 +28,7 @@ public void Demo()
//3. Для числовых типов указать культуру
.Printing().Using(CultureInfo.InvariantCulture)
//4. Настроить сериализацию конкретного свойства
+ .Printing(p => p.Age).Using(i => i.ToString("X"))
//5. Настроить обрезание строковых свойств (метод должен быть виден только для строковых свойств)
.Printing(p => p.Name).TrimmedToLength(10)
//6. Исключить из сериализации конкретного свойства
@@ -36,6 +44,73 @@ public void Demo()
Console.WriteLine(s1);
Console.WriteLine(s2);
Console.WriteLine(s3);
+ var expectedString = string.Join(Environment.NewLine, "Person", "\tId = Guid", "\tName = Alex", "\tHeight = 179,5", "\tAge = 19", "");
+ }
+
+ [Test]
+ public void PrintToString_SkipsExcludedTypes()
+ {
+ var printer = ObjectPrinter.For().Excluding();
+
+ var expectedString = string.Join(Environment.NewLine, "Person", "\tName = Alex", "\tHeight = 179,5", "\tAge = 19", "");
+ var outputString = printer.PrintToString(person);
+ outputString.Should().Be(expectedString);
+ }
+
+ [Test]
+ public void PrintToString_SkipsExcludedProperty()
+ {
+ var printer = ObjectPrinter.For().Excluding(p => p.Id);
+
+ var expectedString = string.Join(Environment.NewLine, "Person", "\tName = Alex", "\tHeight = 179,5", "\tAge = 19", "");
+ var outputString = printer.PrintToString(person);
+ outputString.Should().Be(expectedString);
+ }
+
+ [Test]
+ public void PrintToString_UsesCustomSerializator_WhenGivenToType()
+ {
+ var printer = ObjectPrinter.For().Printing().Using(i => i.ToString("X"));
+
+ //var change = 19.ToString("X"); //13
+
+ var expectedString = string.Join(Environment.NewLine, "Person", "\tId = Guid", "\tName = Alex", "\tHeight = 179,5", "\tAge = 13", "");
+ var outputString = printer.PrintToString(person);
+ outputString.Should().Be(expectedString);
+ }
+
+ [Test]
+ public void PrintToString_UsesCustomSerialization_WhenGivenToProperty()
+ {
+ var printer = ObjectPrinter.For().Printing(p => p.Age).Using(i => i.ToString("X"));
+
+ //var change = 19.ToString("X"); //13
+
+ var expectedString = string.Join(Environment.NewLine, "Person", "\tId = Guid", "\tName = Alex", "\tHeight = 179.5", "\tAge = 13", "");
+ var outputString = printer.PrintToString(person);
+ outputString.Should().Be(expectedString);
+ }
+
+ [Test]
+ public void PrintToString_TrimsStringProperties_WhenTrimmingIsSet()
+ {
+ var printer = ObjectPrinter.For().Printing(p => p.Name).TrimmedToLength(1);
+
+ var expectedString = string.Join(Environment.NewLine, "Person", "\tId = Guid", "\tName = A", "\tHeight = 179.5", "\tAge = 19", "");
+ var outputString = printer.PrintToString(person);
+ outputString.Should().Be(expectedString);
+ }
+
+ [Test]
+ public void PrintToString_UsesCustomCulture_WhenGivenToNumericType()
+ {
+ var printer = ObjectPrinter.For().Printing().Using(CultureInfo.InvariantCulture);
+
+ //var change = 179.5.ToString(CultureInfo.InvariantCulture); 179.5
+
+ var expectedString = string.Join(Environment.NewLine, "Person", "\tId = Guid", "\tName = Alex", "\tHeight = 179.5", "\tAge = 19", "");
+ var outputString = printer.PrintToString(person);
+ outputString.Should().Be(expectedString);
}
}
}
\ No newline at end of file
diff --git a/ObjectPrinting_Should/ObjectPrinting_Should.csproj b/ObjectPrinting_Should/ObjectPrinting_Should.csproj
index fb63b262..23e8a9b0 100644
--- a/ObjectPrinting_Should/ObjectPrinting_Should.csproj
+++ b/ObjectPrinting_Should/ObjectPrinting_Should.csproj
@@ -10,6 +10,7 @@
+
From 91872a6dc2eafa381eae949eca9b0235b0dcb489 Mon Sep 17 00:00:00 2001
From: Kholstinin Yegor <99079920+Yrwlcm@users.noreply.github.com>
Date: Sun, 24 Dec 2023 17:03:19 +0500
Subject: [PATCH 03/13] Basic functionality
---
ObjectPrinting/PrintingConfig.cs | 34 ++++++++++----
ObjectPrinting/PropertyPrintingConfig.cs | 20 ++++-----
.../PropertyPrintingConfigExtensions.cs | 8 +++-
ObjectPrinting/TypePrintingConfig.cs | 24 ++++++++++
.../TypePrintingConfigExtensions.cs | 44 +++++++++++++++++++
.../ObjectPrinterAcceptanceTests.cs | 4 +-
6 files changed, 112 insertions(+), 22 deletions(-)
create mode 100644 ObjectPrinting/TypePrintingConfig.cs
create mode 100644 ObjectPrinting/TypePrintingConfigExtensions.cs
diff --git a/ObjectPrinting/PrintingConfig.cs b/ObjectPrinting/PrintingConfig.cs
index 4cfff79a..9ccee46f 100644
--- a/ObjectPrinting/PrintingConfig.cs
+++ b/ObjectPrinting/PrintingConfig.cs
@@ -9,17 +9,23 @@ namespace ObjectPrinting
{
public class PrintingConfig
{
- private HashSet excludedTypes = new HashSet();
- private HashSet excludedProperites = new HashSet();
+ private readonly HashSet excludedTypes = new HashSet();
+ private readonly HashSet excludedProperites = new HashSet();
+ internal readonly Dictionary> typeSerializers =
+ new Dictionary>();
- public PropertyPrintingConfig Printing()
+ internal readonly Dictionary> propertySerializers =
+ new Dictionary>();
+
+ public TypePrintingConfig Printing()
{
- return new PropertyPrintingConfig(this);
+ return new TypePrintingConfig(this);
}
public PropertyPrintingConfig Printing(Expression> memberSelector)
{
- return new PropertyPrintingConfig(this);
+ var propInfo = ((MemberExpression)memberSelector.Body).Member as PropertyInfo;
+ return new PropertyPrintingConfig(this, propInfo);
}
public PrintingConfig Excluding(Expression> memberSelector)
@@ -51,18 +57,28 @@ private string PrintToString(object obj, int nestingLevel)
typeof(int), typeof(double), typeof(float), typeof(string),
typeof(DateTime), typeof(TimeSpan)
};
- if (finalTypes.Contains(obj.GetType()))
+ var objType = obj.GetType();
+ if (finalTypes.Contains(objType))
+ {
+ if (typeSerializers.TryGetValue(objType, out var serializer))
+ return serializer(obj) + Environment.NewLine;
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.AppendLine(objType.Name);
+ foreach (var propertyInfo in objType.GetProperties())
{
if (Excluded(propertyInfo))
continue;
+ if (propertySerializers.TryGetValue(propertyInfo, out var serializer))
+ {
+ sb.Append(identation + propertyInfo.Name + " = " + serializer(propertyInfo.GetValue(obj)) + Environment.NewLine);
+ continue;
+ }
+
sb.Append(identation + propertyInfo.Name + " = " +
PrintToString(propertyInfo.GetValue(obj),
nestingLevel + 1));
diff --git a/ObjectPrinting/PropertyPrintingConfig.cs b/ObjectPrinting/PropertyPrintingConfig.cs
index 84f537b8..47df3d6d 100644
--- a/ObjectPrinting/PropertyPrintingConfig.cs
+++ b/ObjectPrinting/PropertyPrintingConfig.cs
@@ -1,28 +1,28 @@
using System;
using System.Globalization;
+using System.Reflection;
namespace ObjectPrinting
{
public class PropertyPrintingConfig : IPropertyPrintingConfig
{
- private readonly PrintingConfig printingConfig;
+ public PrintingConfig PrintingConfig { get; }
+ public PropertyInfo PropertyInfo { get; }
- public PropertyPrintingConfig(PrintingConfig printingConfig)
+ public PropertyPrintingConfig(PrintingConfig printingConfig, PropertyInfo propertyInfo)
{
- this.printingConfig = printingConfig;
+ this.PrintingConfig = printingConfig;
+ this.PropertyInfo = propertyInfo;
}
public PrintingConfig Using(Func print)
{
- return printingConfig;
+ var castedFunc = new Func