是否有一种标准方法来获取 mvc 路由方法的变量名
Is there a standard way to obtain variable names of a mvc route method
我正在开发一种工具来创建表达式代理,无需手动查找路由名称即可生成 mvc 路由。过去,我通过给定路线上的属性手动提供此信息。
但是我想知道是否有一种方法可以获取 MVC 框架提供的这些信息。
让我们想象一下这个场景:
public class SomeController : ApiController
{
public int SomeMethod(int someParam1, string someParam2)
{
}
}
var methodInfo = typeof(SomeController).GetMethod("SomeMethod");
var result = ImaginaryMethod(methodInfo); // Should return ["someParam1", "someParam2"]
我想知道是否有办法通过方法的 MethodInfo 或其他方式获取参数名称。
有没有一些我不知道的框架方法已经做到了?
GetParameters
of MethodInfo
提供了关于方法参数的所有信息,所以另一个 returns 只有参数名称的方法可能是不必要的。
例如:
public static string[] ImaginaryMethod(System.Reflection.MethodInfo method)
{
return method != null ? method.GetParameters().Select(p => p.Name).ToArray() : new string[0];
}
我正在开发一种工具来创建表达式代理,无需手动查找路由名称即可生成 mvc 路由。过去,我通过给定路线上的属性手动提供此信息。
但是我想知道是否有一种方法可以获取 MVC 框架提供的这些信息。
让我们想象一下这个场景:
public class SomeController : ApiController
{
public int SomeMethod(int someParam1, string someParam2)
{
}
}
var methodInfo = typeof(SomeController).GetMethod("SomeMethod");
var result = ImaginaryMethod(methodInfo); // Should return ["someParam1", "someParam2"]
我想知道是否有办法通过方法的 MethodInfo 或其他方式获取参数名称。
有没有一些我不知道的框架方法已经做到了?
GetParameters
of MethodInfo
提供了关于方法参数的所有信息,所以另一个 returns 只有参数名称的方法可能是不必要的。
例如:
public static string[] ImaginaryMethod(System.Reflection.MethodInfo method)
{
return method != null ? method.GetParameters().Select(p => p.Name).ToArray() : new string[0];
}