重写的表达式调用运算符方法...但原始节点没有运算符方法
Rewritten expression calls operator method ... but the original node had no operator method
我正在尝试更改表达式树的一部分,其中将类型为 MyEnum 的 属性 X 与某个值 x:
进行比较
$model.X == .Constant<MyEnum>(x)
我想修改树以替换比较 属性 Y 类型 Guid
与值 y(将从 x 派生)。
$model.Y == .Constant<Guid>(y)
所以我从 ExpressionVisitor
继承并且我已经覆盖 VisitMember
以用 Y 代替 X,并且我已经覆盖 VisitConstant
以用 y 代替 x。
运行 这会产生以下 InvalidOperationException
:
System.InvalidOperationException : Rewritten expression calls operator method 'Boolean op_Equality(System.Guid, System.Guid)', but the original node had no operator method. If this is is intentional, override 'VisitBinary' and change it to allow this rewrite.
我的主要问题是:在 VisitBinary
我必须做什么?我的问题是:为什么异常消息告诉原始节点没有运算符方法。我认为这是不正确的。它肯定没有 op_Equality(System.Guid, System.Guid)
但它有 MyEnum 类型的相等运算符,我假设。
我从名字猜测 MyEnum
是某种 Enum
类型。
Enum
类型不需要运算符方法,因为 CIL 直接为它们处理相等比较(内置整数、浮点和布尔类型也是如此)。
无法通过 BinaryExression.Update()
更改方法(它将在您的访问者中显式调用或隐式调用,因为您的访问者已经更改了左右表达式中的一个或两个,这是默认行为在这种情况下)您将必须 VisitBinary()
通过适当调用 Expression.Equal()
.
创建和 return 一个新的 BinaryExpression
我正在尝试更改表达式树的一部分,其中将类型为 MyEnum 的 属性 X 与某个值 x:
进行比较$model.X == .Constant<MyEnum>(x)
我想修改树以替换比较 属性 Y 类型 Guid
与值 y(将从 x 派生)。
$model.Y == .Constant<Guid>(y)
所以我从 ExpressionVisitor
继承并且我已经覆盖 VisitMember
以用 Y 代替 X,并且我已经覆盖 VisitConstant
以用 y 代替 x。
运行 这会产生以下 InvalidOperationException
:
System.InvalidOperationException : Rewritten expression calls operator method 'Boolean op_Equality(System.Guid, System.Guid)', but the original node had no operator method. If this is is intentional, override 'VisitBinary' and change it to allow this rewrite.
我的主要问题是:在 VisitBinary
我必须做什么?我的问题是:为什么异常消息告诉原始节点没有运算符方法。我认为这是不正确的。它肯定没有 op_Equality(System.Guid, System.Guid)
但它有 MyEnum 类型的相等运算符,我假设。
我从名字猜测 MyEnum
是某种 Enum
类型。
Enum
类型不需要运算符方法,因为 CIL 直接为它们处理相等比较(内置整数、浮点和布尔类型也是如此)。
无法通过 BinaryExression.Update()
更改方法(它将在您的访问者中显式调用或隐式调用,因为您的访问者已经更改了左右表达式中的一个或两个,这是默认行为在这种情况下)您将必须 VisitBinary()
通过适当调用 Expression.Equal()
.
BinaryExpression