简体   繁体   English

Visual Studio将类的所有属性转储到编辑器中

[英]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: 假设我有一个Person类:

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

Ie Just dump all the properties of myPerson underneath each other in a little list. 即只需将myPerson的所有属性转储到一个小列表中。 In the Visual Studio editor. 在Visual Studio编辑器中。

I have resharper installed and I had a quick look around for a utility that does specifically this, but I couldn't find one. 我安装了resharper,我快速浏览了一个专门做这个的实用程序,但我找不到一个。 Anyone can help? 有人可以帮忙吗?

You can use the C# Interactive Window which is part of Visual Studio 2015 您可以使用C#Interactive Window ,它是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". 另外,要设置类所在的项目,可以右键单击“解决方案资源管理器”,然后选择“使用项目初始化交互”。

All you have to do is print out the object in the Immediate Window of Visual Studio, you don't need R#. 您所要做的就是在Visual Studio的立即窗口中打印出对象,您不需要R#。

eg ?myPerson 例如?myPerson

And all your properties will print out just like you want. 所有属性都会按照您的要求打印出来。

类的值列表

Visual Commander有一个示例命令 (3.复制到Visual Studio文本编辑器中所选类的剪贴板属性),您可以进一步自定义场景。

C# Immediate Window is great! C#立即窗口很棒! 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)

Here is what works for me in Visual Studio 2017: 以下是Visual Studio 2017中适合我的内容:

Open up the Class editor with 打开类编辑器

Cntrl + Shift + C 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. 按住Shift并单击第一个属性,然后按住Shift键并单击列表中的最后一个属性。 Then right click and select the copy option from the popup menu. 然后右键单击并从弹出菜单中选择复制选项。 You can paste from there into Visual Studio. 您可以从那里粘贴到Visual Studio中。 The first paste will look like the following: 第一个粘贴将如下所示:

Person.Name Person.Age Person.Name Person.Age

From there you can just place a carriage return after each property so they end up on separate lines. 从那里你可以在每个房产后放置一个回车,这样他们就会分开来。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM