VB.NET 函数以字符串形式获取属性名称

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28036577/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 18:41:22  来源:igfitidea点击:

VB.NET function get property name as string

.netvb.netfunctionpropertiesparameters

提问by Jason

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_Nameis a the name of a property, defined like:

提供它First_Name是一个属性的名称,定义如下:

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.

注意我只想在这个函数中返回属性名称“First_Name”,而不是“MyClass.First_Name”。

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

我发现了与我需要用 c# 编写的函数类似的代码的其他示例,但我无法在 VB.Net 中复制它们对 MemberExpression 的使用

Getting sub property names strongly typed

获取强类型的子属性名称

Get name of property as a string

以字符串形式获取属性名称

Retrieving Property name from lambda expression

从 lambda 表达式中检索属性名称

回答by John Koerner

Edit: In Visual Studio 2015 you can use the NameOfoperator to accomplish this:

编辑:在 Visual Studio 2015 中,您可以使用NameOf运算符来完成此操作:

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:

.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