private void ToolStripMenuItem_Plugin_Click(object sender, EventArgs e)
{
ToolStripMenuItem mi = (ToolStripMenuItem)sender;
string asmFile=null;
System.Reflection.Assembly assembly = null;
//MessageBox.Show(mi.Text);
try
{
foreach (KeyValuePair<string, string> val in this.pluginName)
{
if (val.Key == mi.Text)
{
asmFile = val.Value;
break;
}
}
if (asmFile != string.Empty)
{
assembly = System.Reflection.Assembly.LoadFrom(asmFile);
foreach (Type type in assembly.GetTypes())
{
if (!type.IsClass || type.IsNotPublic) continue;
Type[] tempInterfaces = type.GetInterfaces();
if (((IList)tempInterfaces).Contains(typeof(PluginInterface.Plugin)))
{
PluginInterface.Plugin plugin = (PluginInterface.Plugin)System.Activator.CreateInstance(type);
MessageBox.Show(plugin.GetPluginName());
}
} }
}
catch
{
Messages.Instance().Msgbox("异常!");
}
}程序会识别出插件文件夹中的插件并根据名字动态增加ToolStripMenuItem
但是,只要在程序中点击调用插件会发现,第一次点击插件插件可以被调用,也正常调用,但是再调用其他插件时就总是会调用第一次调用的插件。
在这里设置断点可以发现:
断点位置:assembly = System.Reflection.Assembly.LoadFrom(asmFile);
发现:assembly.CodeBase属性为第一次调用插件的位置。(无论调用其他什么插件,这个属性就是第一次调用的插件的位置)
但是asmFile变量中的值确是当前插件的位置
assembly = System.Reflection.Assembly.LoadFile(asmFile);