我现在做了一个Winform程序,要实现的功能就是可以动态的加载插件(插件编译为.dll文件)。现在插件已经写好了(名字为TestPlugin.dll,也就是有个类为TestPlugin.cs),TestPlugin.cs中有个Load()方法就是加载插件,但我在加载这个插件的时候,却报错说为实现改方法或操作,我调试的时候看了下是在加载的代码部分报错。具体报错位置是下面红色部分!不知道我说清楚没,求大神,或者谁有做个类似的项目给我发一份呢,谢谢了![email protected] bool LoadPlugin(string pluginName)
        {
            Boolean result = false;
            config = (PluginConfigurationSection)ConfigurationManager.GetSection("PluginSection");
            PluginConfigurationElement pe = new PluginConfigurationElement();            String path = Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath) + "\\Plugin";
            try
            {
                for (Int32 i = 0; i < config.PluginCollection.Count; i++)
                {
                    pe = config.PluginCollection[i];
                    if (pe.Name == pluginName)
                    {
                        Assembly assembly = Assembly.LoadFile(path + "\\" + pe.Assembly);
                        Type type = assembly.GetType(pe.Type);
                        IPlugin instance = (IPlugin)Activator.CreateInstance(type);
                        instance.Application = application;
                        instance.Load();                        plugins[pluginName] = instance;
                        result = true;
                        break;
                    }
                }
                if (!result)
                {
                    MessageBox.Show("Not Found the Plugin");
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
                result = false;
            }
            return result;
        }框架插件WinForm

解决方案 »

  1.   

    刚才代码没贴好,再贴一次!
    public bool LoadPlugin(string pluginName)
            {
                Boolean result = false;
                config = (PluginConfigurationSection)ConfigurationManager.GetSection("PluginSection");
                PluginConfigurationElement pe = new PluginConfigurationElement();            String path = Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath) + "\\Plugin";
                try
                {
                    for (Int32 i = 0; i < config.PluginCollection.Count; i++)
                    {
                        pe = config.PluginCollection[i];
                        if (pe.Name == pluginName)
                        {
                            Assembly assembly = Assembly.LoadFile(path + "\\" + pe.Assembly);
                            Type type = assembly.GetType(pe.Type);
                            IPlugin instance = (IPlugin)Activator.CreateInstance(type);
                            instance.Application = application;
                            instance.Load();
                            plugins[pluginName] = instance;
                            result = true;
                            break;
                        }
                    }
                    if (!result)
                    {
                        MessageBox.Show("Not Found the Plugin");
                    }
                }
                catch (Exception e)
                {
                    MessageBox.Show(e.Message);
                    result = false;
                }
                return result;
            }
      

  2.   

    有时候还有一种情况,你的确认为你实现了这个方法,但是方法体里却抛了一个NotImplementedException 的异常,这个异常就是告诉上面俺这玩意还木实现具体方法代码ps:实际上类似这种玩意现在很少用这种方式了,现在一般使用MEF或MAF做插件框架
      

  3.   


    我写了一个测试的小例子编译成.dll断点调试的时候可以跳到插件里面去,但在我上面这段代码里就不能断点调至Load()方法中,不知道为什么!
      

  4.   

    我不知道怎么告诉你,我只能说通常的原因是
    1.因为未实现代码,抛了NotImplementedException异常
    2.这个dll不是最新的版本滴(因为他是人为copy到路径去滴,所以偶尔程序员们忘记了同步更新)当然这是俺们通常遇到的原因,而实际具体原因,我们需要下断点,在错误以前,看看实际反射出的对象到底是什么东西,基本上一看就清楚了
      

  5.   


    顶,用MEF或unity,强大又方便扩展~~