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
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);
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.
using System;
using System.Linq;
var dictionary = new[]
{
new
{
Name = "Name1",
Surname = "Surname1"
}
}.ToDictionary(x => x.Name, x => x);
Console.WriteLine(dictionary.Dump());
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());
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;
}
}
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
Repository | License |
---|---|
Heavily customized version of System.CodeDom |
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.