Is there a simple way to dump only some properties ?


Is there a way to dump only a few selected properties of an object ?

For instance, I work a lot with System.Data.DataColumn and I'd like to dump them, but I don't need all the properties, I just want a few ones.

Is there a simple way to do that?
The only solution I imagine is to create a metadata buddy class, but it seems that I need to put there all the properties, in order to add [Dump(false)] on them, which is a lot of work just for debug information.
Is there a way to have the attribute on all properties set to false by default and only dump them is I set [Dump(true)] on them ?

Or any other solution ?


VMelamed wrote May 24, 2016 at 10:51 PM

Hmm, this is interesting... Basically you are suggesting opt-in vs. opt-out... I'll think about it.

First, I suppose you understand that the buddy-classes are optional and you can put all dump attributes on the main class.

Honestly, for me it is very simple: I just right-click on the file in Visual Studio in the solution explorer and choose my extension menu Add Related->Metadata Class, and it generates the entire buddy class for me. If you want, you can take a look at the project (it has also some other options that are related to other OSS projects of mine.) The plug-in is in https://github.com/vmelamed/vm/tree/master/Aspects/Visix/AddRelatedClasses.

killergege wrote May 25, 2016 at 9:02 AM

Yes I understand that the buddy classes are optional, but what do you mean by "you can put all dump attributes on the main class" ?
I wanted to use the buddy class to choose which property I want to dump on objects I can't modify, like framework objects. Often they have a lot of properties I don't care or have a really deep hierarchy (like FileInfo that have a DirectoryInfo property which have a lot of chained Parent properties...), but maybe I don't understand how the attributes are applied in your library.

Let's say that I have a simple class like :
public class Demo 
    public string TheName {get;set;}
    public FileInfo MyFile {get;set;}
Can I easily specify which properties I want from the FileInfo property (let's say DirectoryName, Exists, Name) without buddy class ?

But that's a good idea to have added a VS extension for that, I might use that.

VMelamed wrote May 26, 2016 at 3:59 AM

I mean that you can do:
public class Demo 
    public string TheName {get;set;}

    public FileInfo MyFile {get;set;}
But this is ugly (IMO) and that's why I use buddy classes. I looked at the code thinking of how to reverse the default opt-out behavior and I think that there is actually a very simple solution. All classes and properties that do not have the Dump attribute use one and the same static default internal instance DumpAttribute.Default. I made it public and now you can do something like this in your initialization code:
DumpAttribute.Default.Skip = ShouldDump.Skip;
and then in your code you can have:
public class Demo 
    public string TheName {get;set;}

    public FileInfo MyFile {get;set;}
This will dump TheName but not MyFile. Actually now you can change all defaults of the properties and classes that do not have the Dump attribute. As soon as I test it I'll release it.

What do you think?

killergege wrote May 26, 2016 at 9:21 AM

That's a nice and simple workaround, I'll try that.
I can't do that this week, but I'll test your changes next week.

VMelamed wrote May 26, 2016 at 3:50 PM

Tested it and published on NuGet.

killergege wrote May 31, 2016 at 10:58 AM

Just tested it, it works great.
Thanks !

I noticed another error in the documentation, you mention ClassMetadataResolver.SetClassDumpData but this class doesn't exist. I guess you renamed it to ClassMetadataRegistrar and changed a bit the methods.

VMelamed wrote Jun 1, 2016 at 12:46 AM

Yeah... perfect argument for "the code is the documentation..." :)