下面的代码有错,主要是表达一下用意:
object frm = Application.OpenForms["Form1"];
if (frm != null)
{
    Type typeFrm = frm.GetType();
    MethodInfo inf = typeFrm.GetMethod("Fill", new Type[] { typeof(string) });
    inf.Invoke(frm, new object[] { "xxx" });
}当前项目为Prj1,其中Form1在另一个项目Prj2中,并且Prj1没有引用Prj2,Form1.cs文件中有Fill(string code)的public方法,想通过反射调用Form1的Fill方法,请问该怎样修改上述代码?

解决方案 »

  1.   

    那我也给你举个例子,这样获取Type:
    Assembly.Load("System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089").GetType("System.Data.SqlClient.SqlConnection")
      

  2.   


                try
                {
                    Assembly assembly = Assembly.LoadFile(dll);
                    string p= Path.GetFileNameWithoutExtension(dll);
                    file = p + ".Class1";
                    type = assembly.GetType(file); //必须使用名称空间+类名称
                    object obj = assembly.CreateInstance(file);//必须使用名称空间+类名称
                    MethodInfo method = type.GetMethod("Fill");//方法的名称
                    method.Invoke(obj, new object[] { "xxx" });//带参数的实例方法
                }
                catch
                {
                    MessageBox.Show(".......!");
                }