Skip to content

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

License

Notifications You must be signed in to change notification settings

ycherkes/VarDump

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

85 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Stand With Ukraine

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.

Actively used as a part of Object Dumper Visual Studio and Visual Studio Code extension

nuget version nuget downloads

C# & VB Dumper:

Run .NET fiddle

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:

Run .NET fiddle

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:

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

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

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

Extensibility:

With middleware:

Run .NET fiddle

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:

Run .NET fiddle

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.

About

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

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages