【发布时间】:2014-11-17 18:51:53
【问题描述】:
我正在尝试为一个类构建一个字符串,其中包含该实例的所有属性名称及其各自值的列表。
根据问题Get Property Info from an object without giving the property name as string,我使用Expression 类型Func 类型T, Object 来获取属性名称,如下所示:
Private Function GetPropertyValuePair(Of T)(lambda As Expression(Of Func(Of T, Object))) As String
Dim body As Expression = lambda
'ensure we have the right type of thing
If TypeOf body Is LambdaExpression Then body = DirectCast(body, LambdaExpression).Body
If TypeOf body Is UnaryExpression Then body = DirectCast(body, UnaryExpression).Operand
If body.NodeType <> ExpressionType.MemberAccess Then Throw New InvalidOperationException()
'get property info
Dim memberInfo = DirectCast(body, MemberExpression).Member
Dim propInfo = DirectCast(memberInfo, PropertyInfo)
Return propInfo.Name + " - " + propInfo.GetValue(Me, Nothing)
End Function
通过这种方式,我通过获取PropertyInfo 获取当前值,并使用GetValue 在当前实例中搜索该属性。
那我可以这样称呼它:
GetPropertyValuePair(Of Foo)(Function(x) x.Bar)
这将产生:
Bar - WhateverTheValueIsForBar
问题是当我的属性是嵌套时,GetValue 会抛出异常:
对象与目标类型不匹配。
例如,在运行以下表达式时:
GetPropertyValuePair(Of Foo)(Function(x) x.Bar.Car)
属性信息的类型为Car,在Foo 上无法直接找到。
我可以从原始 lambda 表达式中获取实例值,而不必首先使用 GetValue 进行搜索吗?
这可以通过 MVC @Html.EditorFor 帮助器实现,因此必须有某种机制来传递传入函数的值,如下所示:
@Html.EditorFor(Function(model) model.Bar.Car)
有什么想法吗?
【问题讨论】:
标签: .net vb.net reflection lambda propertyinfo