Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio dump all properties of class into editor

Ok, here is one for the people that have lots of handy little add ins for visual studio, or can help with a keypress sequence.

Let's say I have a Person class:

class Person
{
    string Name { get; set; }
    int Age { get; set; }
}

And I'm busy coding away happily. I often get the situation where I need to assign values to all the properties of that class, or assign all the values of the properties to something else.

public override void CopyTo(Person myPerson)
{
    myPerson.Name = "XXX";
    myPerson.Age = 11;
}

I would like to generate this part:

myPerson.Name
myPerson.Age

I.e. Just dump all the properties of myPerson underneath each other in a little list. In the Visual Studio editor.

I have resharper installed and I had a quick look around for a utility that does specifically this, but I couldn't find one. Anyone can help?

like image 699
user230910 Avatar asked Aug 31 '14 09:08

user230910


People also ask

How do I show properties in Visual Studio?

You can find Properties Window on the View menu. You can also open it by pressing F4 or by typing Properties in the search box. The Properties window displays different types of editing fields, depending on the needs of a particular property.

How do I get Visual Studio project Properties code?

You access project properties by right-clicking the project node in Solution Explorer and choosing Properties, or by typing properties into the search box on the menu bar and choosing Properties Window from the results. .

Where is properties in VS code?

To open the Settings editor, use the following VS Code menu command: On Windows/Linux - File > Preferences > Settings. On macOS - Code > Preferences > Settings.


2 Answers

Here is what works for me in Visual Studio 2017:

Open up the Class editor with

Cntrl + Shift + C

then in the Class View window which pops up select the class name which contains the properties you want in the displayed structure. All the properties for the class will be listed in the below window pane. Shift + click the first property then shift + click the last one in the list. Then right click and select the copy option from the popup menu. You can paste from there into Visual Studio. The first paste will look like the following:

Person.Name Person.Age

From there you can just place a carriage return after each property so they end up on separate lines.

like image 90
Robertcode Avatar answered Sep 27 '22 20:09

Robertcode


C# Immediate Window is great! This is a one-liner to print out properties in some format needed. Just play with it to suit your needs:

typeof(MyApp.Person).GetProperties().Select(x => x.Name).Aggregate((x, y)=>x +", " +y)
like image 29
wxt Avatar answered Sep 27 '22 20:09

wxt