thomasgalliker / ObjectDumper

ObjectDumper is a utility which aims to serialize C# objects to string for debugging and logging purposes.
396 stars 40 forks source link

[Enhancement] Object Dumper to C# - make it detect what default constructor does #130

Open ishepherd opened 2 months ago

ishepherd commented 2 months ago

Generated C# calls the default constructor but it doesn't know what the constructor does.

Is it feasible (!!) for Object Dumper to call the constructor to find out the initial state, and make the right calls for desired state?

Example

Let's say the dumped object has property string A, and the value is null

thomasgalliker commented 2 months ago

I don‘t understand what you mean. ObjectDumper just traverses properties of an already constructed object and dumps content as c# initializer code.

If you can provide some sample code (objects to be dumped) and a simple unit test then I can look into it.

ishepherd commented 2 months ago

Thanks for your reply. Yes I know. And this feature is unreasonable to ask. (btw - Thanks for creating this project 😄)

Here was my case, approximately

class A {
    string Prop1 {get; set;} = "";
    string Prop2 {get; set;}
}

var x = new A { Prop1 = null };

I think Object Dumper would dump like this, depending on configuration:

// Incorrect.
// Prop1 will be ""
var x = new A();

// Correct, but not minimal.
var x = new A() { Prop1 = null, Prop2 = null }

Some perfect-world Object Dumper could dump it as

// Understands what the constructor does and overrides it
var x = new A { Prop1 = null };