9

I'm trying to create a function that when a property is passed to it as a parameter, it returns the name used to define the property as a string. For example,

Shared Function PropertyToStr(Propert As Object)
     'Code to get property name and return it as a string goes here  
End Function

Providing that First_Name is a the name of a property, defined like:

Property First_Name as String

The function should work like:

Dim str as String = PropertyToStr(First_Name) ' Resulting in str = "First_Name"

Note I ONLY want in this function to return the property name "First_Name", and not "MyClass.First_Name" for example.

I have found other examples of similar code to my function I require written in c# yet I have not been able to replicate their use of the MemberExpression in VB.Net

Getting sub property names strongly typed

Get name of property as a string

Retrieving Property name from lambda expression

1
  • 1
    I two would like to know the answer to this, great question @Jason Commented Jan 20, 2015 at 2:05

1 Answer 1

13

Edit: In Visual Studio 2015 (VB 14 or later) you can use the NameOf operator to accomplish this:

Property First_Name As String

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    MessageBox.Show(NameOf(First_Name))
End Sub

Original answer for older versions of .net:

Running those other answers through some code converters and cleaning it up yields the following, which seems to work just fine.

Private Shared Function GetMemberInfo(method As Expression) As MemberExpression
    Dim lambda As LambdaExpression = TryCast(method, LambdaExpression)
    If lambda Is Nothing Then
        Throw New ArgumentNullException("method")
    End If

    Dim memberExpr As MemberExpression = Nothing

    If lambda.Body.NodeType = ExpressionType.Convert Then
        memberExpr = TryCast(DirectCast(lambda.Body, UnaryExpression).Operand, MemberExpression)
    ElseIf lambda.Body.NodeType = ExpressionType.MemberAccess Then
        memberExpr = TryCast(lambda.Body, MemberExpression)
    End If

    If memberExpr Is Nothing Then
        Throw New ArgumentException("method")
    End If

    Return memberExpr
End Function

Public Shared Function GetPropertyName(Of T)(prop As Expression(Of Func(Of T))) As String
    Dim expression = GetMemberInfo(prop)
    Return expression.Member.Name
End Function

Property First_Name As String
Property LastName As String
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    MessageBox.Show(GetPropertyName(Function() First_Name))
    MessageBox.Show(GetPropertyName(Function() LastName))
End Sub
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.