VarDump 1.0.4.4-alpha

This is a prerelease version of VarDump.
There is a newer version of this package available.
See the version list below for details.
The owner has unlisted this package. This could mean that the package is deprecated, has security vulnerabilities or shouldn't be used anymore.
dotnet add package VarDump --version 1.0.4.4-alpha
NuGet\Install-Package VarDump -Version 1.0.4.4-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" Version="1.0.4.4-alpha" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add VarDump --version 1.0.4.4-alpha
#r "nuget: VarDump, 1.0.4.4-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 as a Cake Addin
#addin nuget:?package=VarDump&version=1.0.4.4-alpha&prerelease

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

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

VarDump is a highly customizable utility for serialization 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 options = new DumpOptions { SortDirection = ListSortDirection.Ascending };

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

var vbDumper = new VisualBasicDumper(options);
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;}
}

To dump with extension methods, please install a separate VarDump.Extensions package.

Dump Extension methods:

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

using System;
using System.Linq;

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

Console.WriteLine(dictionary.Dump());

Dump 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;

VarDumpExtensions.VarDumpFactory = VarDumpFactories.VisualBasic;

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

Console.WriteLine(dictionary.Dump());

Extensibility:

With middleware:

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

using System;
using VarDump;
using VarDump.Visitor;
using VarDump.Visitor.Descriptors;
using VarDump.Visitor.Descriptors.Specific;

// 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 options = new DumpOptions
{
    Descriptors = 
    { 
        new ObjectContentReplacer { Replacement = MaskCardNumber }
    }
};

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

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

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

// VB string
Console.WriteLine(vb);

static ReflectionDescription MaskCardNumber(ReflectionDescription description)
{
    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
    };

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

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;

// 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 options = new DumpOptions
{
    ConfigureKnownObjects = (knownObjects, nextDepthVisitor, _, codeWriter) =>
    {
        knownObjects.Add(new FormattableStringVisitor(nextDepthVisitor, codeWriter));
    }
};

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

return;

class FormattableStringVisitor(INextDepthVisitor nextDepthVisitor, ICodeWriter codeWriter) : IKnownObjectVisitor
{
    public string Id => nameof(FormattableString);

    public bool IsSuitableFor(object obj, Type objectType)
    {
        return obj is FormattableString;
    }

    public void ConfigureOptions(Action<DumpOptions> configure)
    {
    }

    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)(() => nextDepthVisitor.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

    • No dependencies.
  • .NETStandard 2.0

    • No dependencies.

NuGet packages (2)

Showing the top 2 NuGet packages that depend on VarDump:

Package Downloads
VarDump.Extensions

Extension methods for simplifying the usage of the VarDump library.

Frank.Reflection.Dump

This is a library that allows you to dump the contents of a type to a string as initilization code. This is helpful for debugging and logging, and can be used to generate code especially for unit tests.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.4.12 66 6/8/2024
1.0.4.11 155 6/3/2024
1.0.3 162 4/2/2024
1.0.2 129 3/31/2024
1.0.1 142 3/17/2024
1.0.0 130 3/17/2024
0.2.16 149 2/14/2024
0.2.15 123 1/24/2024
0.2.14 629 1/20/2024
0.2.13 99 1/20/2024
0.2.12 221 1/7/2024
0.2.11 390 12/30/2023
0.2.10 143 12/29/2023
0.2.9 136 12/29/2023
0.2.8 106 12/10/2023
0.2.7 114 12/3/2023
0.2.6 174 9/9/2023
0.2.5 168 8/5/2023
0.2.4 141 7/29/2023
0.2.3 148 7/23/2023
0.2.2 153 7/3/2023
0.2.1 144 7/2/2023
0.2.0 142 7/1/2023
0.1.0 3,244 4/17/2023
0.0.9 163 4/5/2023
0.0.8 187 4/3/2023
0.0.7 220 3/5/2023
0.0.6 270 1/27/2023
0.0.5 265 1/27/2023
0.0.4 255 1/26/2023
0.0.3 275 1/21/2023
0.0.2 266 1/20/2023
0.0.1 282 1/20/2023