コード例 #1
0
        private void button2_Click(object sender, EventArgs e)
        {
            List <string> pluginpath = helper.FindPlugin();

            pluginpath = helper.DeleteInvalidPlungin(pluginpath);
            foreach (string filename in pluginpath)
            {
                try
                {
                    //获取文件名
                    string asmfile = filename;
                    string asmname = Path.GetFileNameWithoutExtension(asmfile);
                    if (asmname != string.Empty)
                    {
                        // 利用反射,构造DLL文件的实例
                        Assembly asm = Assembly.LoadFile(asmfile);
                        //利用反射,从程序集(DLL)中,提取类,并把此类实例化
                        Type[] t = asm.GetExportedTypes();
                        foreach (Type type in t)
                        {
                            if (type.GetInterface("IPlugin") != null)
                            {
                                IPlugin show = (IPlugin)Activator.CreateInstance(type);
                                listBox1.Items.Add(show);
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    loglibrary.LogHelper.Error(ex);
                    loglibrary.LogHelper.Flush();
                }
            }
        }