我现在做了一个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
{
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
解决方案 »
- ASP.NET网站实现中英文转换(本地化资源)
- textbox锁定有效字符
- 求个正则表达式
- 怎么用编程的方式得到应用程序的版本和版权呀
- 上传文件时如何过滤后缀名
- ---------------------高分求救,,高人来。关于foreach (Control cbl in Controls)----------------------
- dr[1].ToString("yyyy-MM-dd");怎么会错呢??
- C# winform 自定义控件包含自定义控件 位置调整
- 超级菜问题!!!!! 在线等待
- 常见的小问题:string类型怎么转成int(float)...
- Vs2010格式化代码的问题,蛋疼
- c#
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;
}
我写了一个测试的小例子编译成.dll断点调试的时候可以跳到插件里面去,但在我上面这段代码里就不能断点调至Load()方法中,不知道为什么!
1.因为未实现代码,抛了NotImplementedException异常
2.这个dll不是最新的版本滴(因为他是人为copy到路径去滴,所以偶尔程序员们忘记了同步更新)当然这是俺们通常遇到的原因,而实际具体原因,我们需要下断点,在错误以前,看看实际反射出的对象到底是什么东西,基本上一看就清楚了
顶,用MEF或unity,强大又方便扩展~~