VarDump.Extensions 0.3.6-alpha

This is a prerelease version of VarDump.Extensions.
There is a newer version of this package available.
See the version list below for details.
dotnet add package VarDump.Extensions --version 0.3.6-alpha                
NuGet\Install-Package VarDump.Extensions -Version 0.3.6-alpha                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="VarDump.Extensions" Version="0.3.6-alpha" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add VarDump.Extensions --version 0.3.6-alpha                
#r "nuget: VarDump.Extensions, 0.3.6-alpha"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install VarDump.Extensions as a Cake Addin
#addin nuget:?package=VarDump.Extensions&version=0.3.6-alpha&prerelease

// Install VarDump.Extensions as a Cake Tool
#tool nuget:?package=VarDump.Extensions&version=0.3.6-alpha&prerelease                

Made in Ukraine Stand with the people of Ukraine: How to Help

VarDump is a utility for serialization of runtime objects to C# or Visual Basic string.

Developed as a free alternative to ObjectDumper.NET, which is not free for commercial use.

nuget version nuget downloads

C# & VB Dumper:

<p align="right"><a href="https://dotnetfiddle.net/vI6Wq4">Run .NET fiddle</a></p>

using System;
using VarDump;

var anonymousObject = new { Name = "Name", Surname = "Surname" };
var cs = new CSharpDumper().Dump(anonymousObject);
Console.WriteLine(cs);
var vb = new VisualBasicDumper().Dump(anonymousObject);
Console.WriteLine(vb);

C# & VB Dumper, how to use DumpOptions:

<p align="right"><a href="https://dotnetfiddle.net/p4oIKX">Run .NET fiddle</a></p>

using System;
using System.ComponentModel;
using VarDump;
using VarDump.Visitor;

var person = new Person { Name = "Nick", Age = 23 };
var dumpOptions = new DumpOptions { SortDirection = ListSortDirection.Ascending };

var csDumper = new CSharpDumper(dumpOptions);
var cs = csDumper.Dump(person);

var vbDumper = new VisualBasicDumper(dumpOptions);
var vb = vbDumper.Dump(person);

// C# string
Console.WriteLine(cs);
// VB string
Console.WriteLine(vb);

class Person
{
    public string Name {get; set;}
    public int Age {get; set;}
}

Object Extension methods:

<p align="right"><a href="https://dotnetfiddle.net/n9kjiF">Run .NET fiddle</a></p>

using System;
using System.Linq;
using VarDump.Extensions;
using VarDump.Visitor;

var dictionary = new[]
{
    new
    {
        Name = "Name1",
        Surname = "Surname1"
    }
}.ToDictionary(x => x.Name, x => x);

Console.WriteLine(dictionary.Dump(new DumpOptions()));

Object Extension methods, how to switch default dumper to VB:

<p align="right"><a href="https://dotnetfiddle.net/OGCcrk">Run .NET fiddle</a></p>

using System;
using System.Linq;
using VarDump.Extensions;
using VarDump.Visitor;

VarDumpExtensions.VarDumpFactory = VarDumpFactories.VisualBasic;

var dictionary = new[]
{
    new
    {
        Name = "Name1",
        Surname = "Surname1"
    }
}.ToDictionary(x => x.Name, x => x);

Console.WriteLine(dictionary.Dump(new DumpOptions()));

Extensibility:

With middleware:

<p align="right"><a href="https://dotnetfiddle.net/hfrbo6">Run .NET fiddle</a></p>

using System;
using System.Linq;
using VarDump;
using VarDump.Visitor;
using VarDump.Visitor.Descriptors;

// For more examples see https://github.com/ycherkes/VarDump/blob/main/test/VarDump.UnitTests/ObjectDescriptorMiddlewareSpec.cs

var obj = new
{
    FullName = "BRUCE LEE",
    CardNumber = "4953089013607",
    OtherInfo = new 
    {
        CardNumber = "5201294442453002",
    }
};

var dumpOptions = new DumpOptions
{
    Descriptors = { new CardNumberMaskingMiddleware() }
};

var csDumper = new CSharpDumper(dumpOptions);
var cs = csDumper.Dump(obj);

var vbDumper = new VisualBasicDumper(dumpOptions);
var vb = vbDumper.Dump(obj);

// C# string
Console.WriteLine(cs);

// VB string
Console.WriteLine(vb);

class CardNumberMaskingMiddleware : IObjectDescriptorMiddleware
{
    public IObjectDescription GetObjectDescription(object @object, Type objectType, Func<IObjectDescription> prev)
    {
        var objectDescription = prev();

        return new ObjectDescription
        {
            Type = objectDescription.Type,
            ConstructorArguments = objectDescription.ConstructorArguments.Select(MaskCardNumber),
            Properties = objectDescription.Properties.Select(MaskCardNumber),
            Fields = objectDescription.Fields.Select(MaskCardNumber)
        };
    }

    private static bool IsCardNumber<T>(T description) where T : ReflectionDescription
    {
        return description.Type == typeof(string)
               && description.Name?.EndsWith("cardnumber", StringComparison.OrdinalIgnoreCase) == true;
    }

    private static T MaskCardNumber<T>(T description) where T : ReflectionDescription
    {
        if (!IsCardNumber(description) || string.IsNullOrWhiteSpace((string)description.Value))
        {
            return description;
        }

        var stringValue = (string)description.Value;

        var maskedValue = stringValue.Length - 4 > 0
                ? new string('*', stringValue.Length - 4) + stringValue.Substring(stringValue.Length - 4)
                : stringValue;

        return description with
        {
            Value = maskedValue
        };
    }
}

With KnownObjectVisitor:

<p align="right"><a href="https://dotnetfiddle.net/kScIyR">Run .NET fiddle</a></p>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using VarDump;
using VarDump.CodeDom.Compiler;
using VarDump.Visitor;
using VarDump.Visitor.KnownObjects;

// For more examples see https://github.com/ycherkes/VarDump/blob/main/test/VarDump.UnitTests/KnownObjectsSpec.cs

const string name = "World";
FormattableString str = $"Hello, {name}";

var dumpOptions = new DumpOptions
{
    ConfigureKnownObjects = (knownObjects, rootVisitor, _, codeWriter) =>
    {
        knownObjects.Add(new FormattableStringVisitor(rootVisitor, codeWriter));
    }
};

var dumper = new CSharpDumper(dumpOptions);
var result = dumper.Dump(str);
Console.WriteLine(result);

return;

class FormattableStringVisitor(IRootVisitor rootVisitor, ICodeWriter codeWriter) : IKnownObjectVisitor
{
    public bool IsSuitableFor(object obj, Type objectType)
    {
        return obj is FormattableString;
    }

    public void Visit(object obj, Type objectType, VisitContext context)
    {
        var formattableString = (FormattableString)obj;

        IEnumerable<Action> arguments =
        [
            () => codeWriter.WritePrimitive(formattableString.Format)
        ];

        arguments = arguments.Concat(formattableString.GetArguments().Select(a => (Action)(() => rootVisitor.Visit(a, context))));

        codeWriter.WriteMethodInvoke(() =>
            codeWriter.WriteMethodReference(
                () => codeWriter.WriteType(typeof(FormattableStringFactory)),
                nameof(FormattableStringFactory.Create)),
            arguments);
    }
}

For more examples see Unit Tests

Compare VarDump with ObjectDumper.NET - Run .NET fiddle

Powered By

Repository License
Heavily customized version of System.CodeDom MIT

Privacy Notice: No personal data is collected at all.

This tool has been working well for my personal needs, but outside that its future depends on your feedback. Feel free to open an issue.

🍪 Sponsor me on GitHub or PayPal.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net45 is compatible.  net451 was computed.  net452 was computed.  net46 was computed.  net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETFramework 4.5

  • .NETStandard 2.0

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.4.11 109 6/3/2024
1.0.4.10 107 5/25/2024
1.0.3 131 4/2/2024
1.0.2 108 3/31/2024
1.0.1 121 3/17/2024
1.0.0 112 3/17/2024
0.3.8-alpha 88 3/16/2024
0.3.7-alpha 88 3/15/2024
0.3.6-alpha 99 3/11/2024
0.3.5-alpha 98 3/10/2024
0.3.4-alpha 94 3/9/2024
0.3.1-alpha 90 3/5/2024
0.3.0-alpha 102 3/4/2024
0.2.16 116 2/14/2024
0.2.15 112 1/24/2024
0.2.14 106 1/20/2024
0.2.13 104 1/20/2024
0.2.12 120 1/7/2024
0.2.11 188 12/30/2023
0.2.10 130 12/29/2023
0.2.9 133 12/29/2023