2

我正在创建一个动态表达式构建器并尝试实现“喜欢”功能。在写我自己的之前,我已经搜索了任何现有的功能,并找到了一个接近我需要的功能。经过几次实验后,我无法将它用于字符串以外的类型。

当我传递一个类型的参数时,int我得到这个错误:

在“System.String”类型上声明的方法“System.String ToString()”不能用“System.Int32”类型的实例调用

我的代码如下所示:

private static MethodCallExpression GetLowerCasePropertyAccess(MemberExpression propertyAccess)
{
    //return Expression.Call(Expression.Call(propertyAccess, "ToString", new Type[0]), typeof(string).GetMethod("ToLower", new Type[0]));
    return Expression.Call(Expression.Call(propertyAccess, typeof(string).GetMethod("ToString", System.Type.EmptyTypes)), typeof(string).GetMethod("ToLower", System.Type.EmptyTypes));
}

private static readonly MethodInfo ContainsMethod = typeof(String).GetMethod("Contains", new Type[] { typeof(String) });

public static Expression<Func<T, bool>> Create<T>(string propertyName, ComparisonOperators comparisonOperator, dynamic comparedValue1, dynamic comparedValue2 = null)
{
    ParameterExpression parameterExpression = Expression.Parameter(typeof(T), "x");
    MemberExpression memberExpression = Expression.MakeMemberAccess(parameterExpression, typeof(T).GetProperty(propertyName));
    ConstantExpression constantExpression = Expression.Constant(comparedValue1, comparedValue1.GetType());
    Expression expressionBody = null;

    switch (comparisonOperator)
    {
        ...

        case ComparisonOperators.Contains:
            //var indexOf = Expression.Call(memberExpression, "IndexOf", null, Expression.Constant(comparedValue1, typeof(string)), Expression.Constant(StringComparison.InvariantCultureIgnoreCase));
            //expressionBody = Expression.GreaterThanOrEqual(indexOf, Expression.Constant(0));
            expressionBody = Expression.Call(GetLowerCasePropertyAccess(memberExpression), ContainsMethod, Expression.Constant(comparedValue1.ToLower()));
            break;
    }

    return Expression.Lambda<Func<T, bool>>(expressionBody, new ParameterExpression[] { parameterExpression });
}
4

3 回答 3

0

我不确定我是否完全理解你在做什么,但我认为你的错误是由这一行引起的:

return Expression.Call(Expression.Call(propertyAccess, typeof(string).GetMethod("ToString", System.Type.EmptyTypes)), typeof(string).GetMethod("ToLower", System.Type.EmptyTypes));

它将始终尝试调用类型的ToString方法string,因此,如果您尝试使用Int32属性,那么您将尝试调用String.ToString(),因为ToString()不同类型的实现会有所不同,并且这两种实现不一定兼容,您'会得到你看到的异常:

Method 'System.String ToString()' declared on type 'System.String' cannot be called with instance of type 'System.Int32'

从你正在做的事情来看,我认为这可能是你所追求的:

return Expression.Call(Expression.Call(propertyAccess, propertyAccess.Type.GetMethod("ToString", System.Type.EmptyTypes)), typeof(string).GetMethod("ToLower", System.Type.EmptyTypes));

它将使用ToString(使用从 获得的类型propertyAccess.Type)的正确实现。

于 2013-09-01T15:59:26.770 回答
0

Linq to entity 不支持.ToString方法。要将数值转换为字符串,您需要使用SqlFunctions.StringConvert方法。我已经修复了您的代码,现在您可以like对字符串和数字列执行以下操作:

private static Expression GetConvertToStringExpression(Expression e)
{
    // if property string - no cast needed
    // else - use SqlFunction.StringConvert(double?) or SqlFunction.StringConvert(decimal?);
    Expression strExpression = null;
    if (e.Type == typeof(string))
        strExpression = e;

    var systemType = Nullable.GetUnderlyingType(e.Type) ?? e.Type;

    if (systemType == typeof(int) 
        || systemType == typeof(long) 
        || systemType == typeof(double)
        || systemType == typeof(short)
        || systemType == typeof(byte)) // continue
    {
        // cast int to double
        var doubleExpr = Expression.Convert(e, typeof (double?));
        strExpression = Expression.Call(StringConvertMethodDouble, doubleExpr);
    }

    if (systemType == typeof (decimal))
    {
        // call decimal version of StringConvert method
        // cast to nullable decimal
        var decimalExpr = Expression.Convert(e, typeof (decimal?));
        strExpression = Expression.Call(StringConvertMethodDecimal, decimalExpr);
    }

    return strExpression;
}

private static MethodCallExpression GetLowerCasePropertyAccess(Expression propertyAccess)
{
    var stringExpression = GetConvertToStringExpression(propertyAccess);
    if (stringExpression == null)
        throw new Exception(string.Format("Not supported property type {0}", propertyAccess.Type));

    return Expression.Call(stringExpression,
        typeof (string).GetMethod("ToLower", Type.EmptyTypes));
}

private static readonly MethodInfo StringConvertMethodDouble = typeof (SqlFunctions).GetMethod("StringConvert",
    new Type[] {typeof (double?)});
private static readonly MethodInfo StringConvertMethodDecimal = typeof(SqlFunctions).GetMethod("StringConvert",
    new Type[] { typeof(decimal?) });
于 2013-09-01T22:52:06.403 回答
0

我刚刚做了这样的事情:

    public Expression<Func<T,bool>> BuildContainsExpression<T>(MemberExpression memberExp, object comparedValue)
    {
        var parameter = Expression.Parameter(memberExp.Member.DeclaringType, "x");
        var method = typeof(string).GetMethod("Contains", types: new[] { typeof(string) });

        var comparison = Expression.Equal(
                    Expression.Call(
                        method: method,
                        instance: memberExp,
                        arguments: Expression.Constant(comparedValue)),
                    Expression.Constant(true)
            );

        return Expression.Lambda<Func<T, bool>>(comparison, parameter);
    }

它构建了一个如下所示的表达式:( x.Language.Contains("tr") 使用我的动态参数) 示例表达式

于 2019-07-10T17:16:24.307 回答