13

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?

user230910
  • 2,353
  • 2
  • 28
  • 50

5 Answers5

11

You can use the C# Interactive Window which is part of Visual Studio 2015

> #r "C:/MyApp/bin/Debug/Foo.dll"
> using MyApp;
> var personType = typeof(Person);
> var personProperties = personType.GetProperties();
> foreach(var personProperty in personProperty) { Console.WriteLine($"{nameof(Person)}.{personProperty.Name}"); }

Of course this can be shortened but this shows you how to use it.

Also to setup the project where the class is located, you can right click on Solution Explorer and then select "Initialize Interactive with Project".

S.N
  • 4,910
  • 5
  • 31
  • 51
Emond
  • 50,210
  • 11
  • 84
  • 115
  • 1
    This is totally awesome. Just came to know of the C# interactive window today and the code in this solution works like a boss :) Thanks @Erno ! – Shiva Jun 28 '17 at 07:00
  • `Console.WriteLine($"{personProperty.PropertyType.Name} {nameof(Person)}.{personProperty.Name} ` gives you the Property Type. – CAD bloke Oct 22 '17 at 08:14
  • 1
    I couldn't get this to work until I read the initialization instructions here: https://stackoverflow.com/a/11135787/93394 – kmote Feb 28 '19 at 21:30
10

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.

Robertcode
  • 911
  • 1
  • 13
  • 26
4

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)
wxt
  • 495
  • 1
  • 5
  • 12
3

All you have to do is print out the object in the Immediate Window of Visual Studio, you don't need R#.

e.g. ?myPerson

And all your properties will print out just like you want.

List of Values of a Class

Scott Wylie
  • 4,725
  • 2
  • 36
  • 48
  • Thanks for the attempt, but I would like to get the properties only (not the values), while I am still writing the code (not at runtime) – user230910 Sep 01 '14 at 09:20
3

There is a sample command (3. Copy to the clipboard properties of the selected class in Visual Studio text editor) for Visual Commander that you can customize further for your scenarios.

Sergey Vlasov
  • 26,641
  • 3
  • 64
  • 66
  • This is the best answer so far, I will accept it soon if no better one comes along.. – user230910 Sep 01 '14 at 09:21
  • Thanks for the answer, I have attempted to execute it but I have managed to get myself well stuck. I have asked a follow up question here, http://stackoverflow.com/questions/25724189/in-visual-studio-add-in-how-can-i-retrieve-the-properties-of-the-text-selectio?lq=1 – user230910 Sep 08 '14 at 12:31
  • 2
    If you are reading this in 2017, [@Erno's answer](https://stackoverflow.com/a/35622143/325521) is the way to go. – Shiva Jun 28 '17 at 07:04