【问题标题】:How can I apply an implicit cast on numeric types using Expression Trees?如何使用表达式树对数字类型应用隐式强制转换?
【发布时间】:2015-08-15 11:00:10
【问题描述】:

我有一个带有 int 字段的 ExpandoObject,我想使用表达式树将其转换为小数。

这是我正在使用的方法:

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);
    }

以这种方式调用它会导致 InvalidCastException:

        dynamic source = new ExpandoObject();
        source.IntProp = 1;

        decimal r = CreateLambdaCastExpression().Compile()(source);

如果我将 source.IntProp 设置为 1m,它可以工作(显然)

我在msdn 上读到过,ExpressionConvert 只对用户定义的类型执行隐式转换,所以这可能是解释。

知道如何对数字类型执行隐式转换吗?

【问题讨论】:

  • 不确定我是否完全理解了这一点。但我相信Expression.Convert 遵循“实现方法是null”作为“expression.Type 是引用类型”。它是object。所以如果是var sourcePropertyExpression = Expression.Dynamic( binder, typeof (int), sourceParameterExpression); 可能会有帮助。

标签: c# dynamic expression-trees expandoobject


【解决方案1】:

更改这些行:

// CSharpBinderFlags.ConvertExplicit: explicit cast 
// (will convert double to decimal)
// CSharpBinderFlags.None: implicit cast
// (will convert int to decimal, won't convert double to decimal)
var convert = Binder.Convert(CSharpBinderFlags.ConvertExplicit, typeof(decimal), typeof(ExpressionTreeUtils));

// (decimal) source;
var castedValueExpression = Expression.Dynamic(
    convert, typeof(decimal), sourcePropertyExpression);

注意关于隐式/显式转换的注释。

(所以唯一改变的是var castedValueExpression 的构建方式)

ideone 带有完整示例。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-06-07
    • 2015-12-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多