調用託管dll的幾種辦法

調用託管dll的幾種辦法

#region 第二种方法
        public static void downc(string fnamespace, string fclass, string fmethod)
        {
            try
            {
                object[] par = new object[1];
                par[0] = str;
                Assembly MyAssembly = AppDomain.CurrentDomain.Load(fnamespace);
                Type tp = MyAssembly.GetType(fnamespace + "." + fclass);
                object o = Activator.CreateInstance(tp);
                MethodInfo m = tp.GetMethod(fmethod);
                if (m != null)
                {
                    Form f1 = (Form)m.Invoke(o, par);
                    Application.Run(f1);
                    if (fnamespace == "login")
                    {
                        if (((String[])par[0])[0] != null)
                        {
                            str[0] = ((String[])par[0])[0].ToString();
                        }
                    }
                }
                else
                {
                    MessageBox.Show("找不到指定的方法!");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        #endregion

        #region 第三种方法
        public static void fload()
        {
            try
            {
                object[] par = new object[1];
                par[0] = str;
                AppDomain ad = AppDomain.CreateDomain("webdll1");
                Assembly ass = Assembly.LoadFrom("webdll.dll");
                object md = ad.CreateInstanceAndUnwrap(ass.FullName, "webdll.login");
                Type type = md.GetType();
                object o = Activator.CreateInstance(type);
                MethodInfo mf = type.GetMethod("flogin");
                Form f1 = (Form)mf.Invoke(o, par);
                Application.Run(f1);
                if (((String[])par[0])[0] != null)
                {
                    str[0] = ((String[])par[0])[0].ToString();
                }
                Thread.CurrentThread.Abort();
                AppDomain.Unload(ad);
            }
            catch (Exception mt)
            {
                MessageBox.Show(mt.Message);
            }
        }
        #endregion

        #region 第四种方法
        public static void mm()
        {
            try
            {
                Assembly ad = AppDomain.CurrentDomain.Load("webdll");
                Type tp = ad.GetType("webdll.login");
                object o = Activator.CreateInstance(tp);
                MethodInfo mi = tp.GetMethod("ff");
                Form f1 = (Form)mi.Invoke(o, null);
                Application.Run(f1);
            }
            catch (Exception mt)
            {
                MessageBox.Show(mt.Message);
            }
        }
        #endregion

 

posted @ 2012-03-19 14:50  祝福灵魂  阅读(188)  评论(0编辑  收藏  举报