将要调用的方法名传递给处理程序

本文关键字:处理 程序 调用 方法 | 更新日期: 2023-09-27 18:21:28

我正试图通过querysting传递要调用的方法名,并让ProcessRequest在我的处理程序中调用该方法。我在这里边学习边学习,那么最好的方法是什么呢。这是我的。。。

我在methodInfo.Invoke.上得到错误The best overloaded method match for Invoke(object, object[]) has some invalid arguments

 public class SocialSharingHandler : IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
        string method = (string)context.Request.QueryString["m"];
        if (!string.IsNullOrEmpty(method))
        {
            MethodInfo methodInfo = typeof(SocialSharingHandler).GetMethod(method);
            methodInfo.Invoke(new SocialSharingHandler(), context.Request.Form);
        }
    }
....

将要调用的方法名传递给处理程序

methodInfo.Invoke(new SocialSharingHandler(), new object[] { context.Request.Form });

错误消息说明预期对象的参数类型和对象对象的数组[]

public class SocialSharingHandler : IHttpHandler 
 {
    public void ProcessRequest(HttpContext context)
    {
         string method = (string)context.Request.QueryString["m"];
         if (!string.IsNullOrEmpty(method))
         {
              MethodInfo methodInfo = typeof(SocialSharingHandler).GetMethod(method);
              methodInfo.Invoke(new SocialSharingHandler(), new object[] { context.Request.Form });
         }     
     } 
 }