Csharp/C#教程:C#:System.Reflection.MethodInfo原因:(对象与目标类型不匹配)分享


C#:System.Reflection.MethodInfo原因:(对象与目标类型不匹配)

我下面有一节课,

namespace PocketWeb.AppClass { public class ApiBase { public string foo(string s) { return s; } } } 

我通过下面的System.Reflection.MethodInfo调用,但它导致TargetException:Object与目标类型不匹配。

  protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { var instance_class = Activator.CreateInstance(Type.GetType("PocketWeb.AppClass.ApiBase")); Type instance_method = instance_class.GetType(); System.Reflection.MethodInfo theMethod = instance_method.GetMethod("foo"); object[] obj = new object[] { "hello" }; Response.Write(theMethod.Invoke(this, obj)); //<---Error } } 

那么任何想法? 我尝试将foo的参数更改为对象,如:foo(object s){},但它没有帮助。

  Response.Write(theMethod.Invoke(this, obj)); 

这个参数是错误的,它指的是你的Page类。 传递instance_class而不是。

上述就是C#学习教程:C#:System.Reflection.MethodInfo原因:(对象与目标类型不匹配)分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/1020110.html

(0)
上一篇 2022年1月4日
下一篇 2022年1月4日

精彩推荐