如何获取房产名称
有没有办法获取对象的属性名称?
例如,如果我有:
public class Car : Vehicle
{
public string Make { get; set; }
}
以及
var car = new Car { Make="Ford" };
如何获取代码中 Make 属性的名称?
IE。 car.Make
的名称为 "Make".
所以我想获取字符串 "Make"
我想这样做是因为我想将属性名称传递给方法。
更新:
我想要属性的名称而不是属性数组:
在这里找到答案:
http://handcraftsman.wordpress.com/2008/11/11/how-to-get-c-property-names-without-magic-strings/
is there a way to get the name of a property of an object?
For example if I have:
public class Car : Vehicle
{
public string Make { get; set; }
}
and
var car = new Car { Make="Ford" };
How can I get the name of the Make property in the code?
ie. that car.Make
has the name "Make".
So I want to get the string "Make"
I'm wanting to do this because I'm wanting to pass the property name to a method.
Update:
I want the name of the property not an array of properties:
Found the answer here:
http://handcraftsman.wordpress.com/2008/11/11/how-to-get-c-property-names-without-magic-strings/
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
然后遍历列表
and then iterate through the list
将获取所有属性
然后您可以为
pi
元素执行.Name
would fetch all properties
You can then do a
.Name
forpi
elements我想要属性的名称而不是属性数组:
在这里找到答案:
http://handcraftsman.wordpress.com/2008/11/11/how-to-get-c-property-names-without-magic-strings/
I want the name of the property not an array of properties:
Found the answer here:
http://handcraftsman.wordpress.com/2008/11/11/how-to-get-c-property-names-without-magic-strings/