I have an ExpandoObject with an int field, and I want to cast it to a decimal using an expression tree.
Here is the method I'm using :
private static Expression<Func<dynamic, decimal>> CreateLambdaCastExpression()
{
// source
var sourceParameterExpression = Expression.Parameter(typeof (object), "source");
var binder = Binder.GetMember(
CSharpBinderFlags.None, "IntProp", typeof (ExpressionTreeUtils),
new[] {CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null)});
// source.sourceProperty
var sourcePropertyExpression = Expression.Dynamic(
binder, typeof (object), sourceParameterExpression);
// (decimal) source;
var castedValueExpression = Expression.Convert(sourcePropertyExpression, typeof (decimal));
// () => (decimal) source;
return Expression.Lambda<Func<dynamic, decimal>>(castedValueExpression,
sourceParameterExpression);
}
Calling it this way causes an InvalidCastException:
dynamic source = new ExpandoObject();
source.IntProp = 1;
decimal r = CreateLambdaCastExpression().Compile()(source);
If I set source.IntProp to 1m, it works (obviously)
I've read on msdn that ExpressionConvert only performs implicit conversion on user-defined types, so that might be the explanation.
Any idea on how to perform implicit casts on numeric types?