Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#变量表达式Func参数_C#_Asp.net Mvc_Reflection_Lambda_Expression - Fatal编程技术网

C#变量表达式Func参数

C#变量表达式Func参数,c#,asp.net-mvc,reflection,lambda,expression,C#,Asp.net Mvc,Reflection,Lambda,Expression,我正在编写一个动态模型绑定器,将函数绑定到api调用 这是我的测试控制器,有两个功能。它们具有不同的功能,具有不同的参数类型 public class MyTestController : System.Web.Mvc.Controller { public ActionResult MyFunction() { // Do awesome stuff return result; } public ActionResult MyOth

我正在编写一个动态模型绑定器,将函数绑定到api调用

这是我的测试控制器,有两个功能。它们具有不同的功能,具有不同的参数类型

public class MyTestController : System.Web.Mvc.Controller {

    public ActionResult MyFunction() {
        // Do awesome stuff
        return result;
    }

    public ActionResult MyOtherFunction(int i) {
        // Do other awesome stuff
        return result;
    }
}
有了这个模型绑定器,我希望能够选择特定的函数并创建我的api调用名

public static class ModelBinder {
    public static string GetApiCall<T>( Expression<Func<T, Func<ActionResult>>> expression ) {
        var unaryExpression = (UnaryExpression)expression.Body;
        var methodCallExpression = (MethodCallExpression)unaryExpression.Operand;
        var methodInfoExpression = (ConstantExpression)methodCallExpression.Arguments.Last();
        var methodInfo = (MemberInfo)methodInfoExpression.Value;

        var controllerName = typeof( T ).Name.Replace( "Controller", "" );
        var methodName = methodInfo.Name;

        var myApiCallName = string.Format( "{0}_{1}", controllerName, methodName ).ToLower();
        return myApiCallName;
    }
}
公共静态类ModelBinder{

公共静态字符串GetApiCall(表达式是否需要包含MyOtherFunction的参数?
x=>x.MyOtherFunction(i)
No我只需要函数名这似乎可以满足您的需要:为什么您需要关心参数中的返回类型?您不需要执行
typeof(t)
当您可以摆脱泛型并执行类似于
expression.Body.Type
的操作时。我认为您最初尝试的操作是不可能的。请参阅。这与在.NET库中看到的
Func
Func
Func
等单独定义的原因相同。。。
var apiCallName1 = ModelBinder.GetApiCall<MyTestController>( x => x.MyFunction );
var apiCallName2 = ModelBinder.GetApiCall<MyTestController>( x => x.MyOtherFunction ); // Compiler Says no!
public static string GetApiCall<T>( Expression<Func<T, MagicDelegateTypeThatTakesAllFunctionsWithAllParameterTypes>> expression ) {
    // do awesome stuff
}
public static string GetApiCall<T>( Expression<Func<T, Delegate>> expression ) {
    // do awesome stuff
}

// Calling the method
ModelBinder.GetApiCall<MyTestController>( x => new Func<int, ActionResult>( x.MyOtherFunction ) );