❔ Visual Studio - How to write out all properties when instansiating a class?
I would like to write out all properties automatically when creating an object
Seems like something VS would have a solution for?
(Could not find my answer on stackoverflow/chatgpt)
17 Replies
write out to where?
Just in the IDE
writing something like MyClass obj = new MyClass would generate this for very big classes
a few options, writing to log each individual property / reflection / json serialization
though what for
I just mean to faster write code, when I want to code the creation of an object
I don't get what you mean
why do you need to log all the properties
I don't
I want to quickly write a new object and initialize all of it's properties
OH
Inside Visual Studio, some kind of trick
Heh 🙂
there's not really an automatic way for VS to do this for you
do you want them to have default values?
Yes I do
I tried googling but that went to old VS-extensions non-maintained
public string Name {get;set} = "Default name";
so you introduce default values for the propertiesYes that will solve some cases, thank you @Snow 🙂
It's possible that making the properties
required
will let you do var thing = new Thing {}
and use a quick fix to initialize all required propertiesYou could overwrite the ToString() method. But that could be tedious, if you have a lot of properties.
Good Idea but I could not make it work in VS 2022
Could you not program a macro command to use reflection and do this?
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.