从Lambda表达式获取包含参数值的对象数组 [英] Getting object array containing the values of parameters from a Lambda Expression

查看:36
本文介绍了从Lambda表达式获取包含参数值的对象数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个将传递lambda表达式的函数,我想将lambda接受的参数转换为对象数组.

I'm writing a function that will be passed a lambda expression, and I want to convert the parameters that the lambda takes to an object array.

我能够做到的唯一方法是从此处,我的函数如下所示:

The only way I've been able to do it is with code I borrowed from here, and my function looks something like this:

public class MyClassBase<T> where T : class 
{
    protected void DoStuff(Expression<Action<T>> selector)
    {
        ReadOnlyCollection<Expression> methodArgumentsCollection = (selector.Body as MethodCallExpression).Arguments;
        object[] methodArguments = methodArgumentsCollection.Select(c => Expression.Lambda(c is UnaryExpression ?
                        ((UnaryExpression)c).Operand : c)
                        .Compile()
                        .DynamicInvoke())
                        .ToArray();
        // do more stuff with methodArguments
    }       
}

interface IMyInterface
{
    void MethodSingleParam(string param1);
}

class MyClass : MyClassBase<IMyInterface>
{
    void MakeCall()
    {
        DoStuff(x => x.MethodSingleParam("abc"));
    }
}

是否有更整洁的方法?当我想要的只是参数值时,似乎不得不编译并调用lambda似乎有点过头了.

Is there a neater way of doing this? It seems like overkill having to compile and invoke the lambda when all I want is the parameter values.

推荐答案

在一般情况下,Compile()是您所能做的几乎所有事情.想像一下您是否愿意致电

Well, in the general case, a Compile() is pretty much all you can do. Imagine if you'd call

DoStuff(x => x.MethodSingleParam(Math.Abs(a.SomeMethod())));

您将如何处理?您需要执行 Math.Abs​​(a.SomeMethod())来找出返回的值.这也向您表明,这种自省类型非常脆弱(不能保证第二次调用 a.SomeMethod()返回相同的值).

How would you handle that? You'd need to execute Math.Abs(a.SomeMethod()) to find out what value it returns. This also shows you that this type of introspection is rather brittle (no guarantees that a second call to a.SomeMethod() returns the same value).

但是,当传递的参数是常量(由 ConstantExpression 表示)时,您确定,并且不需要Compile():

However, when the parameter passed is a constant (represented by a ConstantExpression), you will be certain, and you don't need a Compile():

protected void DoStuff(Expression<Action<T>> selector)
{
    ReadOnlyCollection<Expression> methodArgumentsCollection = 
                  (selector.Body as MethodCallExpression).Arguments;
    object[] methodArguments = methodArgumentsCollection.Select(c =>
              c is ConstantExpression 
              ? ((ConstantExpression) c).Value 
              : ... ).ToArray();
    // do more stuff with methodArguments
}

上面 ConstantExpression 的检查可确保以下代码不会调用Compile():

The check for ConstantExpression above ensures that the following code will not call Compile():

DoStuff(x => x.MethodSingleParam("abc"));

就像我说的那样,在这里编译并不是真正安全的事情,因此在这种情况下您最好返回null或抛出错误.(这就是为什么我在这里放置了 ... 的原因;如果需要,您可以将编译器放回此处.)

Like I said, compiling is not really a safe thing to do here, so you might as well return null or throw an error in such cases. (Which is why I've placed a ... here; you can put your Compile back here if you need to.)

这篇关于从Lambda表达式获取包含参数值的对象数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆