Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VB.NET function get property name as string

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

like image 350
Jason Avatar asked Jan 20 '15 01:01

Jason


People also ask

What is get and set property in VB net?

In properties, the Get accessor is useful to return the property value and the Set accessor is useful to assign a new value. The value keyword in Set accessor is useful to define the value which is going to be assigned by the Set accessor.


1 Answers

Edit: In Visual Studio 2015 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
like image 50
John Koerner Avatar answered Sep 20 '22 11:09

John Koerner