C
C#9mo ago
OkOk

❔ 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
SG97
SG979mo ago
write out to where?
OkOk
OkOk9mo ago
Just in the IDE writing something like MyClass obj = new MyClass would generate this for very big classes
MyClass obj = new MyClass
{
string name = "a
int age = 1
MyClass obj = new MyClass
{
string name = "a
int age = 1
SG97
SG979mo ago
a few options, writing to log each individual property / reflection / json serialization though what for
OkOk
OkOk9mo ago
I just mean to faster write code, when I want to code the creation of an object
SG97
SG979mo ago
I don't get what you mean why do you need to log all the properties
OkOk
OkOk9mo ago
I don't I want to quickly write a new object and initialize all of it's properties
SG97
SG979mo ago
OH
OkOk
OkOk9mo ago
Inside Visual Studio, some kind of trick Heh 🙂
SG97
SG979mo ago
there's not really an automatic way for VS to do this for you do you want them to have default values?
OkOk
OkOk9mo ago
Yes I do I tried googling but that went to old VS-extensions non-maintained
SG97
SG979mo ago
public string Name {get;set} = "Default name"; so you introduce default values for the properties
OkOk
OkOk9mo ago
Yes that will solve some cases, thank you @Snow 🙂
Angius
Angius9mo ago
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 properties
RodF
RodF9mo ago
You could overwrite the ToString() method. But that could be tedious, if you have a lot of properties.
OkOk
OkOk9mo ago
Good Idea but I could not make it work in VS 2022
Luxx
Luxx9mo ago
Could you not program a macro command to use reflection and do this?
Accord
Accord9mo ago
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.