1

Closed

SerializableExpressionConverter::VisitSerializableConstantExpression omits to assign RepresentedType to ConstantExpression.

description

Often times when making a ConstantExpression, you must set the type to something different than the value type to meet the Method parameters of a MethodCallExpression.
For example, a function might have the parameters Foo(SomeType left, object right).
 
To call this during an expression tree, my ConstantExpression would be made as so:
return Expression.Constant(value, typeof(object));
 
Although it gets serialized, this is not reflected in the deserialization of the SerializableExpression:
protected override Expression VisitSerializableConstantExpression(SerializableConstantExpression expression)
    {
        if (expression.Value != null)
        {
            return Expression.Constant(expression.Value); // Should be changed to return Expression.Constant(expression.Value, expression.Type.RepresentedType);
 
As it is now, the later call shown below will throw an exception:
return Expression.MakeBinary(expression.NodeType, left, right, expression.IsLiftedToNull, method, conversion); // throws a "operands don't match the parameters of the method."

 

Fix:
Change the line to:
return Expression.Constant(expression.Value, expression.Type.RepresentedType);
 
That way the ConstantExpression is deserialized to be identical to the way it was made.
 
File: SerializableExpressionConverter.cs
Closed Nov 5, 2009 at 7:22 AM by mbauer
Bug was fixed in Change Set #29266.

comments