直接上代码
//创建对象实例
Info info = new Info();
Button btn = (Button)sender;
info.Text = btn.Text;
info.Author = "vey";
info.icon = Icon;
//加载dll
localPath = Directory.GetCurrentDirectory();
dllPath = localPath + @"\TestDll.dll";
Assembly assem = Assembly.LoadFile(dllPath);
//获取所有的类型名,然后遍历,通过类型名字来区别了
Type[] tys = assem.GetTypes();
foreach (Type ty in tys)
{
if (ty.Name == "TestForm3") //Class名称
{
//获取不带参数的构造函数
//ConstructorInfo magicConstructor = ty.GetConstructor(Type.EmptyTypes);
//获取实例,不带参(上述构造时不带参)
//object magicClassObject = magicConstructor.Invoke(new object[] { });
//获取带参数的构造函数
ConstructorInfo magicConstructor = ty.GetConstructor(new Type[] { typeof(Info) });
//获取实例,带参(上述构造时有多少个参数就填写多少个)
object magicClassObject = magicConstructor.Invoke(new object[] { info });
//根据方法名获取方法
MethodInfo mi = ty.GetMethod("getForm");
//获取不带参方法执行结果
var obj = mi.Invoke(magicClassObject, null);
//获取带参方法执行结果.方法里面需要多少个参数,就写多少个 例如 :new object[] { "222", 000 }
//var obj = mi.Invoke(magicClassObject, new object[] { "222", 000 });
//根据返回结果转换类型
Form frm = (Form)obj;
frm.Icon = this.Icon;
frm.ShowDialog();
}
}