【问题标题】:Expression.Convert type for Expression.PropertyExpression.Property 的 Expression.Convert 类型
【发布时间】:2014-07-24 18:33:16
【问题描述】:

我正在尝试转换参数表达式,但在转换为值类型时遇到了问题。以下是我的代码示例:

public static MemberExpression ConvertToType(ParameterExpression sourceParameter,
                                              PropertyInfo propertyInfo, 
                                              TypeCode typeCode)
{
    var sourceExpressionProperty = Expression.Property(sourceParameter, sourceProperty);

    //throws an exception if typeCode is a value type.
    Expression convertedSource = Expression.Convert(sourceExpressionProperty,
                                                Type.GetType("System." + typeCode));

    return convertedSource;
}

我得到以下无效操作异常:

No coercion operator is defined between types 'System.String' and 'System.Decimal'.

任何有关此转换的帮助将不胜感激。

【问题讨论】:

  • Convert 与 C# 转换基本相同。同样的规则去。您将不得不使用一些转换方法。
  • 啊,那也许是 Expression.Call 然后是一些转换方法?
  • 从下面提供的链接中,使用Convert.ChangeType。您拥有所需的所有信息。
  • 我遇到了一些麻烦。我的表情符很弱。
  • Expression.Call(typeof(Convert).GetMethod(...), sourceExpressionProperty, Expression.Quote(typecode))

标签: c# lambda expression-trees


【解决方案1】:
public class ExpressionUtils
{
    public static MethodCallExpression ConvertToType(
        ParameterExpression sourceParameter,
        PropertyInfo sourceProperty,
        TypeCode typeCode)
    {
        var sourceExpressionProperty = Expression.Property(sourceParameter, sourceProperty);
        var changeTypeMethod = typeof(Convert).GetMethod("ChangeType", new Type[] { typeof(object), typeof(TypeCode) });
        var callExpressionReturningObject = Expression.Call(changeTypeMethod, sourceExpressionProperty, Expression.Constant(typeCode));
        return callExpressionReturningObject;
    }
}

请注意,生成的表达式是对 Convert.ChangeType 方法的调用,该方法将返回 System.Object。

这是一个单元测试:

[TestClass]
public class UnitTest1
{
    private class MyClass
    {
        public string ValueAsString { get; set; }
    }

    [TestMethod]
    public void TestMethod1()
    {
        var parameter = Expression.Parameter(typeof(MyClass));
        var property = typeof(MyClass).GetProperty("ValueAsString");
        var lambdaBody = ExpressionUtils.ConvertToType(parameter, property, TypeCode.Decimal);
        var lambda = Expression.Lambda<Func<MyClass, object>>(lambdaBody, parameter);
        var valueAsDecimal = (decimal) lambda.Compile().Invoke(new MyClass { ValueAsString = "42" });
        Assert.AreEqual(42m, valueAsDecimal);
    }
}

【讨论】:

  • 这或多或少是我采用的方法,除了我处理空值。
【解决方案2】:

我采用的解决方案是:

private static Expression GetConvertedSource(ParameterExpression sourceParameter,
                                             PropertyInfo sourceProperty, 
                                             TypeCode typeCode)
{
    var sourceExpressionProperty = Expression.Property(sourceParameter,
                                                       sourceProperty);

    var changeTypeCall = Expression.Call(typeof(Convert).GetMethod("ChangeType", 
                                                           new[] { typeof(object),
                                                            typeof(TypeCode) }),
                                                            sourceExpressionProperty,
                                                            Expression.Constant(typeCode)
                                                            );

    Expression convert = Expression.Convert(changeTypeCall, 
                                            Type.GetType("System." + typeCode));

    var convertExpr = Expression.Condition(Expression.Equal(sourceExpressionProperty,
                                            Expression.Constant(null, sourceProperty.PropertyType)),
                                            Expression.Default(Type.GetType("System." + typeCode)),
                                            convert);



    return convertExpr;
}

注意Expression.Condition 处理空值。

【讨论】:

  • 您应该已经接受了另一个答案并对其进行了编辑以包括空检查,因为他为您指出了正确的方向。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多