从 LogicalBinaryExpression 中获取属性;如何?
Get Properties out of LogicalBinaryExpression ; How?
我想知道是否有一种方法(并且是正确的)从 LogicalBinaryExpression
中检索属性,如果可能的话。
我想要类似的东西:
Dim whereClause as Expression(Of Func(Of Foo, Boolean)) = Function(f as Foo) f.ID = 1
Dim strignifiedWhereClause as string = Me.AMethodWhichhandlesThis(whereClause)
在 AMethodWhichhandlesThis
方法中,我想要一些东西来比较每个属性。如果我得到这些,我就可以使用其余代码了……这实际上只是从 LogicalBinaryExpression 中获取属性的一部分!我什至在某处读到我们根本不应该那样做,但他从来没有说......为什么,如果它不是真的我怎么能这样做?
对不起我的英语,我通常说法语。
要从表达式中提取信息,建议使用自定义访问器。
当您使用您的表达式执行时,以下访问者将 return "Id = 1"
:
Public Class WhereVisitor
Inherits ExpressionVisitor
Public Shared Function Stringify(expression As Expression) As String
Dim visitor As New WhereVisitor()
visitor.Visit(expression)
Return visitor.Value
End Function
Public Sub New()
Me._value = New StringBuilder()
End Sub
Private _value As StringBuilder
Public ReadOnly Property Value() As String
Get
Return Me._value.ToString()
End Get
End Property
Protected Overrides Function VisitBinary(node As BinaryExpression) As Expression
' node.Left and node.Right is not always of this type
' you have to check the type and maybe use another visitor
' to obtain the information you want
Dim left As MemberExpression = CType(node.Left, MemberExpression)
Dim right As ConstantExpression = CType(node.Right, ConstantExpression)
Me._value.AppendLine(String.Format("{0} = {1}", left.Member.Name, right.Value))
Return MyBase.VisitBinary(node)
End Function
End Class
您可以使用以下方式调用它:
Sub Main()
Dim whereClause As Expression(Of Func(Of Foo, Boolean)) = Function(f As Foo) f.Id = 1
Dim s As String = WhereVisitor.Stringify(whereClause)
Console.WriteLine(s)
End Sub
必须修改 visitor
以更好地满足您的需求,但您有一个起点来实现您想要的。
我想知道是否有一种方法(并且是正确的)从 LogicalBinaryExpression
中检索属性,如果可能的话。
我想要类似的东西:
Dim whereClause as Expression(Of Func(Of Foo, Boolean)) = Function(f as Foo) f.ID = 1
Dim strignifiedWhereClause as string = Me.AMethodWhichhandlesThis(whereClause)
在 AMethodWhichhandlesThis
方法中,我想要一些东西来比较每个属性。如果我得到这些,我就可以使用其余代码了……这实际上只是从 LogicalBinaryExpression 中获取属性的一部分!我什至在某处读到我们根本不应该那样做,但他从来没有说......为什么,如果它不是真的我怎么能这样做?
对不起我的英语,我通常说法语。
要从表达式中提取信息,建议使用自定义访问器。
当您使用您的表达式执行时,以下访问者将 return "Id = 1"
:
Public Class WhereVisitor
Inherits ExpressionVisitor
Public Shared Function Stringify(expression As Expression) As String
Dim visitor As New WhereVisitor()
visitor.Visit(expression)
Return visitor.Value
End Function
Public Sub New()
Me._value = New StringBuilder()
End Sub
Private _value As StringBuilder
Public ReadOnly Property Value() As String
Get
Return Me._value.ToString()
End Get
End Property
Protected Overrides Function VisitBinary(node As BinaryExpression) As Expression
' node.Left and node.Right is not always of this type
' you have to check the type and maybe use another visitor
' to obtain the information you want
Dim left As MemberExpression = CType(node.Left, MemberExpression)
Dim right As ConstantExpression = CType(node.Right, ConstantExpression)
Me._value.AppendLine(String.Format("{0} = {1}", left.Member.Name, right.Value))
Return MyBase.VisitBinary(node)
End Function
End Class
您可以使用以下方式调用它:
Sub Main()
Dim whereClause As Expression(Of Func(Of Foo, Boolean)) = Function(f As Foo) f.Id = 1
Dim s As String = WhereVisitor.Stringify(whereClause)
Console.WriteLine(s)
End Sub
必须修改 visitor
以更好地满足您的需求,但您有一个起点来实现您想要的。