MoaidHathot / Dumpify

Adding `.Dump()` extension methods to Console Applications, similar to LinqPad's.
MIT License
959 stars 40 forks source link

Dump() does not print out public fields #23

Open dyster opened 6 months ago

dyster commented 6 months ago

if you create a public class with public fields like public class MyClass { public uint MyNumber; } and call Dump() on it you just get an empty table with the header but if you change the fields to properties it will work, i.e public class MyClass { public uint MyNumber { get; set; } }

MoaidHathot commented 6 months ago

Hi @dyster, this feature is actually available but is turned-off by default, so you need to enable it. You can do that either globally (for all Dumps) or on per-dump basis. Enabling it globally:

DumpConfig.Default.MembersConfig.IncludeFields = true;

Enabling it on per-dump basis:

new Test().Dump(members: new MembersConfig { IncludeFields = true });

Btw, you can include nonpublic members as well, example:

DumpConfig.Default.MembersConfig.IncludeNonePublicMembers = true;
dyster commented 6 months ago

Yes I just branched the code to have a look and realised quickly there was a MembersConfig where I could set it. My next thought on that was then is that the correct default behaviour? Followed by well, if you follow good practice then you should not have public fields on your class so maybe it shouldn't be the default (I was only fooling around with some test code). Followed again by, well, what does it do when you use public fields on a struct?

Which I just tried and no it does not print public fields by default, but that is (I think at least) how you are normally supposed to use structs? So should the default behaviour be different for a class or a struct, and should the MembersConfig also differentiate between the two?