Operator overloads with dynamic linq

c# dynamic-linq expression-trees linq

Question

Think about the code below:

var vectorTest = new Vector2(1, 2) + new Vector2(3, 4); // Works

var x = Expression.Parameter(typeof(Vector2), "x");
var test = System.Linq.Dynamic
                 .DynamicExpression.ParseLambda(new[] { x }, null, "x = x + x");

Running it results in the following exception:

System.Linq.Dynamic.ParseException was unhandled by user code Message=Operator '+' incompatible with operand types 'Vector2' and 'Vector2' Source=DynamicLINQ Position=6

What can I do to make the parser "see" the+ operator saturating theVector2 type?

EDIT: I had the same difficulty with the= operator.
I can see why after looking at the source code; it searches through a particular interface that has a large number of methods for basic types, and if it can't find it, it raises an exception. The issue is that my kindVector2 The operator methods won't ever be found since (isn't in that list.

1
10
1/12/2012 4:49:04 PM

Accepted Answer

You must include the signature into one of the signature interfaces provided in the DynamicLinq library using theSystem.Linq.Dynamic.ExpressionParser . It will only perform recognized parsing actions.

It seems that it will examine every private interface identified inExpressionParser . Add a simple interface to theExpressionParser and the mistake seems to be suppressed.

interface ICustomSignatures
{
    void F(Microsoft.Xna.Framework.Vector2 x, Microsoft.Xna.Framework.Vector2 y);
}

It could be safer to add to or expand from the example in order to be on the safe side (and potentially follow the desired pattern).IAddSignatures interface.

interface ICustomSignatures : IAddSignatures
{
    void F(Microsoft.Xna.Framework.Vector2 x, Microsoft.Xna.Framework.Vector2 y);
}
2
1/12/2012 9:20:40 PM


Related Questions





Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow