使用强类型的方法作为参数而不指定参数 [英] Using a strongly typed method as an argument without specifying parameters

查看:195
本文介绍了使用强类型的方法作为参数而不指定参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以传递方法的强类型名称作为lambda表达式,而不提供参数和/或括号?

Is it possible to pass in the strongly typed name of a method as a lambda expression without also providing the parameters and/or parentheses?

例如,如果我有以下方法:

For example, if I have the following method:

public class CalleeClass
{
    public void MethodA(obj param1, obj param2)
    {
        ...
    }
}

我想通过以下方式将此方法称为:

I would like to call this method elsewhere via:

return new MyClass<CalleeClass>(c => c.MethodA); //Note: no ()'s or arguments

MyClass 负责使用方法名称作为目标的MVC路由。这里的目标是,我们希望能够通过控制器方法使用强类型的视图,我不想提供不能使用的哑参数。

Where MyClass would be responsible for, say, MVC routing using the method name as the target. The goal here is that we want to be able to use strongly typed views via controller methods, and I don't want to have to provide "dumb" parameters that don't get used.

目前,为了使用方法名称,我使用的代码类似于以下内容,但是这种风格仍然需要传递假参数和/或括号。

Presently, I am using code similar to the following in order to use the method names, but this style still requires passing in fake arguments and/or parentheses.

public void MyClass<T>(Expression<Action<T>> action)
{
    var methodName = (action.Body as MethodCallExpression).Method.Name;
}

编辑:对不起,感到困惑,但是最初试图简化这个问题,只包括我以为你需要的,这样做就省略了一些关键信息。这里的最终目标是让MyClass接收一个通用类型+ lambda表达式,而且lambda表达式可以传递强类型的方法名称,而无需实例化一个对象。 -MB

Sorry for the confusion, but I initially tried to simplify the issue by only including what I thought you'd need, and in doing so left out some key info. The ultimate goal here is to have MyClass receive a generic type + lambda expression, and the lambda expression can pass in the strongly typed method name without instantiating an object. -MB

推荐答案

您可以实际传递没有参数的方法:

You can actually pass the method without parameters:

class PassActionTest
{
    public void Test( )
    {
        var c = new C();
        var myClass =  new MyClass(c.MethodA); 
    }
}

class MyClass
{
    public MyClass(Action<object,object> action)
    {
        string methodName = action.Method.Name;
    }
}

class C
{
    public void MethodA(object param1, object param2)
    {
    }
}

编辑:根据Matt Beckman的编辑,包含MethodA不应该被实例化。我的新解决方案是:

EDIT: According to Matt Beckman's EDIT, the class containing MethodA should not be instantiated. My new solution is:

class PassActionTest
{
    public void Test()
    {
        var myClass = new MyClass(c => c.MethodA);
    }
}

class MyClass
{
    public MyClass(Expression<Func<C, Action<object, object>>> expr)
    {
        UnaryExpression unaryExpr = (UnaryExpression)expr.Body;
        MethodCallExpression methodCallExpr = (MethodCallExpression)unaryExpr.Operand;
        ConstantExpression constantExpr = (ConstantExpression)methodCallExpr.Arguments[2];
        MethodInfo methodInfo = (MethodInfo)constantExpr.Value;
        string methodName = methodInfo.Name;
    }

}

class C
{
    public void MethodA(object param1, object param2)
    {
    }
}

分析表达式有点复杂,但我有测试它,它的工作。

It is a bit complicated to analyse the expression, but I have tested it and it works.

这篇关于使用强类型的方法作为参数而不指定参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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