From 29f382fed53655fe7f023828d019d897c0e6f031 Mon Sep 17 00:00:00 2001 From: Yevhen Cherkes Date: Wed, 24 Jan 2024 23:17:18 +0200 Subject: [PATCH] Fix the non-public collection serialization. (#34) * Fix the non-public collection serialization. Enumerable.Range(0, 10) was wrongly dumped in NET80 as new RangeIterator { ... } * update reference to vardump nuget package --- README.md | 2 +- .../VarDump.Extensions.csproj | 8 +++---- src/VarDump/VarDump.csproj | 6 +++--- .../Visitor/KnownTypes/CollectionVisitor.cs | 3 ++- test/VarDump.UnitTests/CollectionSpec.cs | 21 +++++++++++++++++++ 5 files changed, 31 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 637185f..61902dd 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ VarDump is a utility for serialization of runtime objects to C# or Visual Basic Developed as a free alternative of [ObjectDumper.NET](https://github.com/thomasgalliker/ObjectDumper), which is not free for commercial use. -[![nuget version](https://img.shields.io/badge/Nuget-v0.2.14-blue)](https://www.nuget.org/packages/VarDump) +[![nuget version](https://img.shields.io/badge/Nuget-v0.2.15-blue)](https://www.nuget.org/packages/VarDump) [![nuget downloads](https://img.shields.io/nuget/dt/VarDump?label=Downloads)](https://www.nuget.org/packages/VarDump) ## C# & VB Dumper: diff --git a/src/VarDump.Extensions/VarDump.Extensions.csproj b/src/VarDump.Extensions/VarDump.Extensions.csproj index 2dbc021..34c5fa8 100644 --- a/src/VarDump.Extensions/VarDump.Extensions.csproj +++ b/src/VarDump.Extensions/VarDump.Extensions.csproj @@ -6,13 +6,13 @@ enable latest VarDump.Extensions - 0.2.14 - 0.2.14 + 0.2.15 + 0.2.15 Apache-2.0 Copyright $([System.DateTime]::Now.Year) Yevhen Cherkes True VarDump.Extensions - 0.2.14 + 0.2.15 Extension methods that simplify the usage of the VarDump library. https://github.com/ycherkes/VarDump README.md @@ -30,7 +30,7 @@ - + diff --git a/src/VarDump/VarDump.csproj b/src/VarDump/VarDump.csproj index 701acdd..2af9619 100644 --- a/src/VarDump/VarDump.csproj +++ b/src/VarDump/VarDump.csproj @@ -4,13 +4,13 @@ net45;netstandard2.0 latest VarDump - 0.2.14 - 0.2.14 + 0.2.15 + 0.2.15 Apache-2.0 Copyright $([System.DateTime]::Now.Year) Yevhen Cherkes True VarDump - 0.2.14 + 0.2.15 VarDump is a utility for serialization runtime objects to C# and Visual Basic string. https://github.com/ycherkes/VarDump README.md diff --git a/src/VarDump/Visitor/KnownTypes/CollectionVisitor.cs b/src/VarDump/Visitor/KnownTypes/CollectionVisitor.cs index 4c33adc..f96bf53 100644 --- a/src/VarDump/Visitor/KnownTypes/CollectionVisitor.cs +++ b/src/VarDump/Visitor/KnownTypes/CollectionVisitor.cs @@ -120,8 +120,9 @@ private CodeExpression VisitSimpleCollection(IEnumerable enumerable, Type elemen var type = enumerable.GetType(); var isImmutableOrFrozen = type.IsPublicImmutableOrFrozenCollection(); + var isCollection = IsCollection(enumerable); - if (type.IsArray || isImmutableOrFrozen || !IsCollection(enumerable)) + if (type.IsArray || isImmutableOrFrozen || !type.IsPublic || !isCollection) { if (type.IsArray && ((Array)enumerable).Rank > 1) { diff --git a/test/VarDump.UnitTests/CollectionSpec.cs b/test/VarDump.UnitTests/CollectionSpec.cs index 605cfc9..fa36d6c 100644 --- a/test/VarDump.UnitTests/CollectionSpec.cs +++ b/test/VarDump.UnitTests/CollectionSpec.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using System.Linq; using Xunit; namespace VarDump.UnitTests; @@ -23,6 +24,26 @@ public void DumpReadOnlyCollectionCSharp() """, result); } + [Fact] + public void DumpEnumerableRangeCSharp() + { + var range = Enumerable.Range(0, 3); + + var dumper = new CSharpDumper(); + + var result = dumper.Dump(range); + + Assert.Equal(""" + var rangeIteratorOfInt = new int[] + { + 0, + 1, + 2 + }; + + """, result); + } + [Fact] public void DumpReadOnlyCollectionVisualBasic() {