我尝试从Visual Studio中的另一个类调用一个方法。我使用了从数组到方法调用的字符串,但是该示例使用了简单的字符串。
我读了很多此类问题的答案,但仍然不知道出了什么问题。没有什么能解决我的问题。
form1.cs的一部分
public partial class Form1 : Form
{
private void Form1_Load(object sender, EventArgs e)
{
Type type = Type.GetType("Database");
object instance = Activator.CreateInstance(type);
MethodInfo theMethod = type.GetMethod("MyMethod");
theMethod.Invoke(instance, null);
}
}
Database.cs的一部分
public class Database
{
public void MyMethod()
{
MessageBox.Show("Test2");
}
}
脚本无法执行,因为行object instance = Activator.CreateInstance(type);
返回错误System.ArgumentNullException
请帮助我修复此脚本。
PS对不起,我的英语-)
由于type.GetType("Database")
退货而导致异常null
您应该在参数中传递完全限定的名称空间,如下所示
type.GetType("namespace.Database");
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句