gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 20:53:32 26 4
gpt4 key购买 nike

我在下面有一个类,

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

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

 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));

this 参数是错误的,它指的是您的 Page 类。改为传递 instance_class。

关于C#:System.Reflection.MethodInfo 原因:(对象与目标类型不匹配),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3860695/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com