1

我正在尝试使用表达式调用方法,并返回一个用于构建 sql 查询的字符串。

电话很简单

var actualUsers = db.Query<UserModel>( u => u.FirstName.Contains( "ues" ) && u.LastName == "" );

在我的评估器中,我可以"LastName = ''"很容易地使用 BinaryExpression 评估器。

但是,当我尝试从 中获取“FirstName”时,MethodCalLExpression我能得到的最接近的是u.FirstName使用expression.Object.ToString();

获取 MethodCallExpression 的属性名称的正确方法是什么?

这是我到目前为止的方法。

    public static IList<TModel> Query<TModel> ( this IDbProvider iDbProvider, Expression<Func<TModel, object>> exp ) where TModel : class, new()
    {
        // SELECT * FROM [Users] WHERE FirstName LIKE '%ues%'
        // Should return the "Guest" user.

        var sqlExpression = GetSqlExpression( exp.Body );
        return // ....
    }

    private static string GetSqlExpression ( Expression expression )
    {
        if ( expression is MethodCallExpression )
        {
            return GetMethodCallOperator( ( MethodCallExpression )expression );
        }
        // more crap...
    }

    private static string GetMethodCallOperator ( MethodCallExpression expression )
    {
        var lambda = Expression.Lambda( expression.Arguments[0] );
        var compiledExpression = lambda.Compile().DynamicInvoke();


        var commandText = string.Empty;
        var key = expression.Object.ToString(); // "u.FirstName" -- should just be "FirstName"
        var value = compiledExpression.ToString(); // "ues"
        switch ( expression.Method.Name )
        {
            case "Contains":
                commandText = "[{0}] LIKE '%{1}%'";
                break;
        }

        // return "[FirstName] LIKE '%ues%'";
        return string.Format(commandText ,key, value);
    }
4

1 回答 1

3

在您的示例中:

var memberAccess = expression.Object as MemberExpression;
Console.WriteLine(memberAccess.Member.Name); //will print FirstName

MemberExpression是类,描述访问字段或属性。使用它来访问属性名称。我假设,您将解析后的表达式传递u.FirstName.Contains( "ues" )GetMethodCallOperator

完整示例:

void PrintName(Expression<Func<Foo, bool>> func)
{
    var methodCall = func.Body as MethodCallExpression;
    var property =  methodCall.Object as MemberExpression;
    Console.WriteLine (property.Member.Name);
}

PrintName(u => u.FirstName.Contains( "ues" )); //prints FirstName

在哪里:

class Foo
{
    public string FirstName { get; set; }
}
于 2013-10-28T17:17:17.393 回答