Visual Studio将类的所有属性转储到编辑器中 [英] Visual Studio dump all properties of class into editor
问题描述
好吧,这是给那些有很多方便的Visual Studio附加插件的人使用的,或者可以帮助您完成按键序列的人。
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.
假设我有a Person类:
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;
}
我想生成此部分:
myPerson.Name
myPerson.Age
即只需将myPerson的所有属性互相放在一个小列表中即可。在Visual Studio编辑器中。
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?
推荐答案
C#立即窗口很棒!这是一种以所需格式打印属性的方法。只需根据您的需要进行操作即可。
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)
这篇关于Visual Studio将类的所有属性转储到编辑器中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!