.NET获取属性名 [英] .Net Get property name
本文介绍了.NET获取属性名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想获得一个属性的名称,例如:
I would like to get the name of a property, for example:
Dim _foo As String
Public Property Foo as String
Get
Return _foo
End Get
Private Set(ByVal value as String)
_foo = value
End Set
Sub Main()
Console.Write(Foo.Name)'Outputs "Foo"
End Sub
任何的想法如何?
Any ideas how?
推荐答案
你的意思是一个属性,或者你的意思是一个领域?
Do you mean a property, or do you mean a field?
有聪明的lambda技术获取属性的名字 - 这里有一个C#示例:
There are clever lambda techniques for getting the names of properties - here's a C# example:
String GetPropertyName<TValue>(Expression<Func<TValue>> propertyId)
{
return ((MemberExpression)propertyId.Body).Member.Name;
}
这样称呼它:
Call it like this:
GetPropertyName(() => MyProperty)
和它将返回myProperty的
and it will return "MyProperty"
不知道如果这就是你以后还是不是。
Not sure if that's what you're after or not.
这篇关于.NET获取属性名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文