我尝试从Visual Studio中的另一个类调用一个方法。我使用从数组到字符串的方法调用但是该示例使用简单的字符串。从视觉c中的另一个类调用动态方法#
我读了很多这样的问题的答案,但仍然不知道出了什么问题。没有解决我的问题。 Database.cs的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);
}
}
部分
public class Database
{
public void MyMethod()
{
MessageBox.Show("Test2");
}
}
脚本无法执行,因为线object instance = Activator.CreateInstance(type);
返回错误System.ArgumentNullException
请帮我修复这个脚本。
P.S.对不起,我的英语 - )
什么是你想达到这个参数? – mfeineis 2015-02-06 13:17:56
异常说明参数为空。你确定Type.GetType(“数据库”)返回你想要的类型吗? – 2015-02-06 13:18:47
尝试在'Type.GetType()'调用中包含名称空间(或者如果不在同一个程序集中,则为程序集限定名称)。 – 2015-02-06 13:19:27