.NET LAMBDA传递方法参数 [英] .NET Lambda Pass Method Parameter

查看:176
本文介绍了.NET LAMBDA传递方法参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我失去了一些东西很明显,但我有一些麻烦定义一个方法,需要一个方法的参数获取的传递方式方法的信息。我不想实际执行的方法。

I hope i'm missing something obvious, but I'm having some troubles defining a method that takes a parameter of a method to fetch the method information for the passed method. I do NOT want actually execute the method.

我希望能够做的:

  busObject.SetResolverMethod<ISomeInterface>(x=>x.GetNameById);

其中GetNameById是在接口ISomeInterface定义的方法。在这种情况下,该方法的一个例子被传递的签名将是:

Where GetNameById is a method defined on the interface ISomeInterface. In this case, an example of the method being passed in's signature would be:

 MyVarA GetNameById(int id){ .... }

在上面的例子中,所述的 SetResolverMethod 的的身体应该能够返回/存储字符串GetNameById。

In the above example, the SetResolverMethod's body should be able to return / store the string "GetNameById".

有该方法被传递将符合没有标准的签名(除非它总是返回某种类型的对象)。

There is no standard signature the method being passed in will conform to (except that it will always return an object of some kind).

目前我设置的方法作为字符串(例如GetNameById),但我想检查它被编译时间,因此这个问题。

Currently I'm setting the method as a string (i.e. "GetNameById"), but I want it to be compile time checked, hence this question.

推荐答案

这不是特别pretty的/流利,但如果你真的想避免通过虚拟参数值,那么你可以使用一个前pression的返回一个代表。

It's not particularly pretty/fluent but if you really want to avoid having to pass dummy parameter values then you can use an expression that returns a delegate.

SetResolverMethod<ISomeInterface>(x => new Func<int, MyVarA>(x.GetNameById));

SetResolverMethod 的实施将是这个样子:

The SetResolverMethod implementation would look something like this:

public void SetResolverMethod<T>(Expression<Func<T, Delegate>> expr)
{
    var unary = (UnaryExpression) expr.Body;
    var methodCall = (MethodCallExpression) unary.Operand;
    var constant = (ConstantExpression) methodCall.Arguments[2];
    var method = (MethodInfo) constant.Value;
    Console.WriteLine(method.Name);
}

编辑:如果你愿意创建为集重载每个 Func键与其中的;&GT; 委托,可以提高流畅度由包括你的方法的泛型参数类型的方法参数类型。

If you're willing to create as set of overloads for each Func<> delegate, you can improve the fluency by including the method parameter types in the generic parameter types of your method.

p.SetResolverMethod<ISomeInterface, int, MyVarA>(x => x.GetNameById);

正如你所看到的,调用者不再需要指定一个委托类型,因此节省了约8个字符。

As you can see, the caller no longer needs to specify a delegate type, thus saving around 8 characters.

我实现了三个重载为0,1和2个参数:

I've implemented three overloads for 0, 1 and 2 parameters:

public void SetResolverMethod<T, TResult>(Expression<Func<T, Func<TResult>>> expr)
{
    SetResolverMethod((LambdaExpression) expr);
}

public void SetResolverMethod<T, T1, TResult>(Expression<Func<T, Func<T1, TResult>>> expr)
{
    SetResolverMethod((LambdaExpression) expr);
}

public void SetResolverMethod<T, T1, T2, TResult>(Expression<Func<T, Func<T1, T2, TResult>>> expr)
{
    SetResolverMethod((LambdaExpression) expr);
}

private void SetResolverMethod(LambdaExpression expr)
{
    var unary = (UnaryExpression) expr.Body;
    var methodCall = (MethodCallExpression) unary.Operand;
    var constant = (ConstantExpression) methodCall.Arguments[2];
    var method = (MethodInfo) constant.Value;
    Console.WriteLine(method.Name);
}

这篇关于.NET LAMBDA传递方法参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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