Search code examples
c#visual-studiovisual-studio-2012resharperadd-in

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?


Solution

  • 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)