Skip to content

ycherkes/VarDump

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

115 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Stand With Ukraine

VarDump

nuget version nuget downloads

VarDump turns runtime .NET objects into readable C# or Visual Basic source code.

Use it when you want copyable object initializers for tests, diagnostics, documentation, samples, or debugging workflows where plain text dumps are not enough.

dotnet add package VarDump

Quick Start

C# Source Output

This is the smallest useful VarDump example: create a dumper, pass any object, and print the generated source.

Run .NET fiddle

using System;
using VarDump;

var person = new
{
    Name = "Nick",
    Age = 23,
    Tags = new[] { "admin", "active" }
};

var source = new CSharpDumper().Dump(person);

Console.WriteLine(source);

Visual Basic Source Output

Use VisualBasicDumper when the output needs to be Visual Basic source.

using System;
using VarDump;

var person = new { Name = "Nick", Age = 23 };
var source = new VisualBasicDumper().Dump(person);

Console.WriteLine(source);

Core Features

VarDump is built for source-code output.

Feature Notes
C# and Visual Basic output Use CSharpDumper or VisualBasicDumper.
Complex collection support Anonymous collections, groups, lookups, read-only, immutable, frozen, and queryable collections.
Extended array support Includes multi-dimensional arrays and layout handling.
Date and time support Handles common .NET date/time types with configurable output style.
Numeric formatting Supports decimal, binary, hexadecimal, padding, and digit separators for integral values.
Output limits Cap collection size and traversal depth for large graphs.
Type name policy Choose short, nested-qualified, or fully qualified type names.
TextWriter output Stream output when you do not want to allocate one large string.

For behavior examples, see the unit tests.

Configure Output

Member Sorting And Skipped Values

Pass DumpOptions to either dumper to control member selection, sorting, formatting, collection layout, string literal style, and more.

Run .NET fiddle

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

var options = new DumpOptions
{
    SortDirection = ListSortDirection.Ascending,
    IgnoreNullValues = false,
    IgnoreDefaultValues = false
};

var person = new { Name = "Nick", Age = 23, MiddleName = (string?)null };
var source = new CSharpDumper(options).Dump(person);

Console.WriteLine(source);

Full reference: DumpOptions API Guide.

Modern C# Literal Styles

Use literal style options when generated C# should target newer language features.

using VarDump;
using VarDump.Visitor;

var options = new DumpOptions
{
    StringLiteralStyle = StringLiteralStyle.Raw,
    CollectionLiteralStyle = CollectionLiteralStyle.Expression
};

var source = new CSharpDumper(options).Dump(new[] { "one", "two" });

Raw string literals require modern C# language support. Expression collection literals require C# 12 support.

Extension Methods

Install the separate extension package when you want DumpText(), DumpConsole(), DumpDebug(), or DumpTrace() on any object.

dotnet add package VarDump.Extensions

Object Extension Methods

Run .NET fiddle

using System;
using System.Linq;

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

Console.WriteLine(dictionary.DumpText());
dictionary.DumpConsole();
dictionary.DumpDebug();
dictionary.DumpTrace();

Visual Basic Extension Output

Run .NET fiddle

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.DumpText());

Advanced And Extensibility

Use the advanced APIs when you need to transform object descriptions before output or teach VarDump how to serialize a specific type.

Need Start here
Mask or rewrite member values before output Extensibility Guide: descriptor middleware
Add support for a known object type Extensibility Guide: known object visitors
Tune every output option DumpOptions API Guide
Compare behavior with ObjectDumper.NET Comparison fiddle

Comparison

VarDump grew out of work on Object Dumper Visual Studio, Visual Studio Code, and Rider extensions. It focuses on richer source-code generation options than ObjectDumper.NET.

Feature VarDump ObjectDumper
Console-style dump N/A Yes
Collections: anonymous, groups, lookups, read-only, immutable, frozen, queryable Yes No
Extended array support Yes No
Extended Date-Time support Yes No
Hex/Binary formatting and digit separator Yes No
Max collection size Yes N/A
Nested types Yes No
Output to TextWriter Yes No

Powered By

Repository License
Heavily customized version of System.CodeDom MIT

Privacy Notice: No personal data is collected.

This tool has been working well for my personal needs, but outside that its future depends on your feedback. Please open an issue with problems, ideas, or examples that should work better.

Sponsor the project on GitHub or PayPal.

About

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

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages