Get string name of property using reflection

前端 未结 7 928
北海茫月
北海茫月 2020-11-28 06:56

There is a whole wealth of reflection examples out there that allow you to get either:

    1. All properties in a class

    2. A single property, provided you kn

7条回答
  •  孤独总比滥情好
    2020-11-28 07:27

    In case anyone needs it...here is the VB .NET version of the answer:

    Public Shared Function GetPropertyName(Of t)(ByVal PropertyExp As Expression(Of Func(Of t))) As String
       Return TryCast(PropertyExp.Body, MemberExpression).Member.Name
    End Function
    

    Usage:

    Dim name As String = GetPropertyName(Function() (New myObject).AProperty)
    

提交回复
热议问题