Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
tstephansen opened this issue Sep 9, 2024 · 0 comments

Comments

@tstephansen
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant