ObjectDumper icon indicating copy to clipboard operation
ObjectDumper copied to clipboard

[Enhancement] Add an attribute to exclude properties from nested objects.

Open tstephansen opened this issue 5 months ago • 0 comments

Summary

I sometimes use ObjectDumper to create unit test data for my software. In some cases I am dumping an object that has many children (with children) and I want to exclude properties of these children. I know you can exclude properties of the object you're dumping but I don't see a way to do it for the object's children. I propose adding an [ExcludeFromObjectDumper] attribute that can be placed on the properties I want excluded from the dump. I have already made these modifications and added tests to support them in my fork of the project.

API Changes

Add an ExcludeFromObjectDumper attribute.

[System.AttributeUsage(System.AttributeTargets.Property)]
public class ExcludeFromObjectDumperAttribute : System.Attribute
{
}

In ObjectDumperCSharp.cs change the GetPropertiesToDump method:

var properties = type.GetRuntimeProperties()
    .Where(p => p.GetMethod != null && p.GetMethod.IsPublic && p.GetMethod.IsStatic == false &&
                p.GetCustomAttributes().All(c => c.GetType() != typeof(ExcludeFromObjectDumperAttribute)))
    .ToArray();

In ObjectDumperConsole.cs change the CreateObject method:

var properties = type.GetRuntimeProperties()
    .Where(p => p.GetMethod != null && p.GetMethod.IsPublic && p.GetMethod.IsStatic == false &&
                p.GetCustomAttributes().All(c => c.GetType() != typeof(ExcludeFromObjectDumperAttribute)))
    .ToList();

Intended Use Case

This proposal is useful if you are dumping objects that have nested objects and you want properties excluded from the nested objects.

tstephansen avatar Sep 09 '24 18:09 tstephansen