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?
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.
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. .
To open the Settings editor, use the following VS Code menu command: On Windows/Linux - File > Preferences > Settings. On macOS - Code > Preferences > Settings.
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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With