今天我遇到了一个有趣的问题,同时在动态表达式构建库中实现了一个功能。更具体而言,但无关紧要的是,在表达式中定义运算符优先级的功能。
当LINQ引擎编译最终表达式时,我遇到InvalidOperationException
声明Lambda parameter out of scope
。
在分配相关的ParameterExpression
对象后,问题就会出现。
使用完整且格式良好的lambda表达式树,我发现在编译Lambda时将Lambda的ParameterExpression
对象重新分配给有效引用是无效的。
这是我在应用修复之前最初使用的行为的简短描述:
Queryable.Where
一起使用,根表达式为LambdaExpression
,使用Expression.Lambda(expression, Expression.Parameter(GetType(type), "name"))
构建结果是一个表达式树,其中所有同名的ParameterExpression
引用都指向同一个对象 - 但编译时遇到了InvalidOperationException
。
我应用的修复程序采用了以下行为:
ParameterExpression
Expression.Lambda(expression, parameterArray)
parameterArray
最终结果编译得很好,即使Lambda表达式结构在概念上与前一行为的输出相同。
问题是:为什么第一次失败,第二次失败?
下面是一个测试夹具类来重现(请原谅vb),包含测试用例和几个支持类(取决于nUnit,LinqKit):
注意:TestFixture&缺少测试属性声明 - 如何在markdown ???
Imports LinqKit
Imports NUnit.Framework
Imports System.Linq.Expressions
_
Public Class ParameterOutOfScopeTests
Public Class TestObject
Public Name As String
Public DateOfBirth As DateTime = DateTime.Now
Public DateOfDeath As DateTime?
End Class
Public Class ParameterNormalisation
Inherits ExpressionVisitor
Public Sub New(ByVal expression As Expression)
_expression = expression
End Sub
Private _expression As expression
Private _parameter As ParameterExpression
Private _name As String
Public Function Normalise(ByVal parameter As ParameterExpression) As Expression
_parameter = parameter
_name = parameter.Name
_expression = Me.Visit(_expression)
Return _expression
End Function
Public Function Normalise(ByVal name As String) As Expression
_name = name
_expression = Me.Visit(_expression)
Return _expression
End Function
Protected Overrides Function VisitParameter(ByVal p As System.Linq.Expressions.ParameterExpression) As System.Linq.Expressions.Expression
Debug.WriteLine("ClientExpressionParameterNormalisation.VisitParameter:: Parameter visited: " & p.Name & " " & p.GetHashCode)
If p.Name.Equals(_name) Then
If _parameter Is Nothing Then
_parameter = p
Debug.WriteLine("ClientExpressionParameterNormalisation.VisitParameter:: Primary parameter identified: " & p.GetHashCode)
ElseIf Not p Is _parameter Then
Debug.WriteLine("ClientExpressionParameterNormalisation.VisitParameter:: Secondary parameter substituted: " & p.GetHashCode & " with " & _parameter.GetHashCode)
Return MyBase.VisitParameter(_parameter)
Else
Debug.WriteLine("ClientExpressionParameterNormalisation.VisitParameter:: Parameter already common: " & p.GetHashCode & " with " & _parameter.GetHashCode)
End If
End If
Return MyBase.VisitParameter(p)
End Function
End Class
_
Public Sub Lambda_Parameter_Out_Of_Scope_As_Expected()
Dim treeOne As Expression(Of Func(Of TestObject, Boolean)) = Function(test As TestObject) test.DateOfBirth > Now And test.Name.Contains("name")
Dim treeTwo As Expression(Of Func(Of TestObject, Boolean)) = Function(test As TestObject) Not test.DateOfDeath.HasValue
Dim treeThree As Expression = Expression.And(treeOne.Body, treeTwo.Body)
Dim realParameter As ParameterExpression = Expression.Parameter(GetType(TestObject), "test")
Dim lambdaOne As LambdaExpression = Expression.Lambda(treeThree, realParameter)
Dim delegateOne As [Delegate] = lambdaOne.Compile
End Sub
_
Public Sub Lambda_Compiles()
Dim treeOne As Expression(Of Func(Of TestObject, Boolean)) = Function(test As TestObject) test.DateOfBirth > Now And test.Name.Contains("name")
Dim treeTwo As Expression(Of Func(Of TestObject, Boolean)) = Function(test As TestObject) Not test.DateOfDeath.HasValue
Dim treeThree As Expression = Expression.And(treeOne.Body, treeTwo.Body)
Dim normaliser As New ParameterNormalisation(treeThree)
Dim realParameter As ParameterExpression = Expression.Parameter(GetType(TestObject), "test")
treeThree = normaliser.Normalise(realParameter)
Dim lambdaOne As LambdaExpression = Expression.Lambda(treeThree, realParameter)
Dim delegateOne As [Delegate] = lambdaOne.Compile
End Sub
_
Public Sub Lambda_Fails_But_Is__Conceptually__Sound()
Dim treeOne As Expression(Of Func(Of TestObject, Boolean)) = Function(test As TestObject) test.DateOfBirth > Now And test.Name.Contains("name")
Dim treeTwo As Expression(Of Func(Of TestObject, Boolean)) = Function(test As TestObject) Not test.DateOfDeath.HasValue
Dim treeThree As Expression = Expression.And(treeOne.Body, treeTwo.Body)
Dim realParameter As ParameterExpression = Expression.Parameter(GetType(TestObject), "test")
Dim lambdaOne As LambdaExpression = Expression.Lambda(treeThree, realParameter)
Dim normaliser As New ParameterNormalisation(lambdaOne)
lambdaOne = DirectCast(normaliser.Normalise("test"), LambdaExpression)
Dim delegateOne As [Delegate] = lambdaOne.Compile
End Sub
End Class
答案 0 :(得分:3)
AFAIK表达式树不会将使用相同参数创建的两个ParameterExpression对象视为“相同参数”。
在没有测试你的代码的情况下,那就是突出的:当我读到第一个(失败的)场景时,你用所遇到的第一个替换所有同名的参数,但第一个遇到的参数不是与您在Expression.Lambda()调用中创建的对象相同的ParameterExpression对象。在第二个(后续)场景中,它是。
已编辑我应该补充一点,我没有使用LinqKit的ExpressionVisitor,但据我所知,它基于我使用的代码,其中VisitLambda不是很强大:
protected virtual Expression VisitLambda(LambdaExpression lambda)
{
Expression body = this.Visit(lambda.Body);
if (body != lambda.Body)
{
return Expression.Lambda(lambda.Type, body, lambda.Parameters);
}
return lambda;
}
请注意,访问表达式的主体,但不访问其参数。如果LinqKit没有改善这一点,那将是失败的原因。