try 
            { 
                Assembly a = Assembly.LoadFrom(Application.StartupPath + "\\MultipleUpDown.exe");            
                Type type = a.GetType("MultipleUpDown.MainForm"); 
                object Myobject= Activator.CreateInstance(type);              
                //执行带参数的方法 
                MethodInfo miParas = type.GetMethod("NewTaskWithoutDialog"); 
                miParas.Invoke(Myobject, new object[] { TableInfo, UpDownType.DOWNLOAD, false, false, "", "升级后自动下载" }); 
            } 
            catch 
            { 
            } 
改成这个可以取到Type了。但是Activator.CreateInstance(type);              这里老报“调用目标发生异常”

解决方案 »

  1.   

    Type type = a.GetType("MainForm"); 
    为空了把  GetType(需要全路径..)
      

  2.   


                try
                {
                    Assembly a = Assembly.LoadFile(Application.StartupPath + "\\MultipleUpDown.exe");           
                    Type[] types = a.GetTypes();
    Form _Form = null;
                    foreach(Type type in types)
    {
                        try
        {
           Form _Form = a.CreateInstance(type.ToString()) as Form;
           if(_Form.Name == "MainForm")
           {
              break;
           }
        }
        catch
        {
        }
                    }
                    //执行带参数的方法
                    MethodInfo miParas = type.GetMethod("NewTaskWithoutDialog");
                    miParas.Invoke(_Form, new object[] { TableInfo, UpDownType.DOWNLOAD, false, false, "", "升级后自动下载" });
                }
                catch
                {
                }试试看
      

  3.   

    如果是窗体不能用 Activator.CreateInstance(type);        这样看看
     object Myobject = type.InvokeMember(null, BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.CreateInstance, null, null, null);
      

  4.   


    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Reflection;namespace myReflection
    {
        class Program
        {
            static void Main(string[] args)
            {
                Master ma = new Master();
                Type tp = ma.GetType();
                MethodInfo [] MI = tp.GetMethods();
                foreach (MethodInfo mm in MI)
                {
                    Console.WriteLine(mm.Name);
                }
            }
        }
        class Student
        {
            private string stuName;        public string StuName
            {
                get { return stuName; }
                set { stuName = value; }
            }
            private int stuAge;        public int StuAge
            {
                get { return stuAge; }
                set { stuAge = value; }
            }
            public void study()
            {
                Console.WriteLine('学习!');
            }
        }    class Master:Student
        {
            public void Say()
            {
                Console.WriteLine('硕士........');
            }
        }
    }
      

  5.   

        try
                {
                    Assembly a = Assembly.LoadFile(Application.StartupPath + "\\MultipleUpDown.exe");           
                    Type[] types = a.GetTypes();
            Form _Form = null;
                    foreach(Type type in types)
            {
                        try
                {
                   Form _Form = a.CreateInstance(type.ToString()) as Form;
                   if(_Form.Name == "MainForm")
                   {
                      break;
                   }
                }
                catch
                {
                }
                    }
                    //执行带参数的方法
                    MethodInfo miParas = type.GetMethod("NewTaskWithoutDialog");
                    miParas.Invoke(_Form, new object[] { TableInfo, UpDownType.DOWNLOAD, false, false, "", "升级后自动下载" });
                }
                catch
                {
                }
    这个a.CreateInstance(_type.ToString())老报:调用目标发生错误。请问。怎么办啊?
      

  6.   

    如果是窗体不能用 Activator.CreateInstance(type);        这样看看 
    object Myobject = type.InvokeMember(null, BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.CreateInstance, null, null, null); 这个也是说:调用的目标发生错误。请指教啊。
      

  7.   

          public MainForm()
            {
                InitializeComponent();            Yqun.Common.ContextCache.ClientLoginInfos.ClientLoginInfo.LocalStartPath =
                    Application.StartupPath;
                string appKey =
                    Yqun.ClientConfig.GetFirstAppKey();
                Yqun.ClientConfig.SetLocalDefaultContext(appKey);            loadActions();            if (!DebugMode)
                {
                    this.toolStripButton_New.Visible = false;
                }            InitSheetStyle();
                SetSheetStyle();            this.sheet.SelectionUnit = FarPoint.Win.Spread.Model.SelectionUnit.Row;            this.fp.GotFocus += new EventHandler(fp_GotFocus);
                this.treeView_Works.GotFocus += new EventHandler(treeView_Works_GotFocus);            this.TopMost = true;
                this.TopMost = false;        }        public MainForm(string arg):this()
            {
                this.Show();            byte[] bs = System.Convert.FromBase64String(arg);
                string xml = System.Text.Encoding.UTF8.GetString(bs);
                DataSet d =
                    UpDownUtility.GetTablePropertyStruct();
                Yqun.Common.Encoder.DataSetCoder.DeSerialize(d, xml);            NewTask(d);
            }
    就这两构造
      

  8.   

    你再建立个类 例如 class MainShow
     
    public object GetMainForm ()
    {
      return new MainForm();
    }然后反射这个类看看
      

  9.   

    尝试一下:
    Assembly a = Assembly.LoadFrom(Application.StartupPath + "\\MultipleUpDown.exe");
    Type type = a.GetType("MultipleUpDown.MainForm");
    ConstructorInfo ci = type.GetConstructor(new Type[] {});
    Form _Form = ci.Invoke(new object[] parameters{}) as Form;
      

  10.   


     [NonSerialized]
            Type moduleType;        [XmlIgnore]
            [Browsable(false)]
            public Type ModuleType
            {
                get
                {                if (moduleType == null)
                    {
                        try
                        {
                            moduleType = Assembly.GetType(窗体名, false,true);
                            if (moduleType == null)
                            {
                                Assembly[] ass = AppDomain.CurrentDomain.GetAssemblies();
                                foreach (Assembly var in ass)
                                {
                                    moduleType = var.GetType(窗体名, false);
                                    if (moduleType != null)
                                        break;
                                }
                            }
                        }
                        catch
                        {
                        }
                    }
                    return moduleType;
                }
                set { moduleType = value; }
            }        [NonSerialized]
            Assembly assembly;        [XmlIgnore]
            [Browsable(false)]
            public Assembly Assembly
            {
                get
                {                if (assembly == null)
                    {
                        try
                        {
                            assembly = AppDomain.CurrentDomain.Load(程序集);
                        }
                        catch
                        {                    }
                    }
                    return assembly;
                }
                set { assembly = value; }
            }我就这样取,然后再Activator.CreateInstance(type)的
      

  11.   

    像上面那样肯定能取到type的,取到type后自然能用Activator.CreateInstance(type)创建实例
      

  12.   

    谢谢各位啊:是我没放配置文件进去。所以构造函数哪里出了问题。谢谢啊。现在是我想先把反射出来的这个窗体给Show出来以后。再执行我自己的函数。请问该怎么能Show出我的窗体了。
      

  13.   

    type.GetMethod("ShowDialog")
    “type.GetMethod("ShowDialog")”引发了“System.Reflection.AmbiguousMatchException”类型的异常
        base {System.SystemException}: {"发现不明确的匹配。"}
      

  14.   

    我是这么做的: try
                {
                    Assembly a = Assembly.LoadFrom(Application.StartupPath + "\\MultipleUpDown.exe");
                    Type type = a.GetType("MultipleUpDown.MainForm");
                    Form MyForm = a.CreateInstance(type.FullName) as Form;
                    MethodInfo miParas = type.GetMethod("NewTaskWithoutDialog");
                    miParas.Invoke(MyForm, new object[] { TableInfo, UpDownType.DOWNLOAD, false, false, "升级后自动下载" });
                }
                catch
                {
                }大家还有没有好的办法了。