各位大侠,小弟最近遇到一个问题。
在使用开源的插件框架,代码地址http://www.codeproject.com/Articles/91577/Plug-in-Framework。
怎么做到每个插件在AppDomain(应用程序域)中执行?
增、删、改插件时,如何卸载并更新正在运行的插件?
在线等。。。
AppDomain domain = null;
//创建应用程序域初始化信息
AppDomainSetup setup = new AppDomainSetup();
setup.ApplicationBase = AppDomain.CurrentDomain.BaseDirectory;
setup.ConfigurationFile = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;
setup.CachePath = setup.ApplicationBase;
setup.LoaderOptimization = LoaderOptimization.SingleDomain;
setup.ShadowCopyDirectories = setup.ApplicationBase;
setup.ShadowCopyFiles = "true"; //启用影像复制程序集
Evidence evidence = AppDomain.CurrentDomain.Evidence;
//创建AppDomain
domain = AppDomain.CreateDomain(AppDomainName, evidence, setup);
//执行Assembly
ExcuteAppDomain obj = (ExcuteAppDomain)domain.CreateInstanceFromAndUnwrap(AppDomainName, "MyPluginsTest.ExcuteAppDomain");
obj.ExcuteAssemblyByForm(DllPath, typeName);
public void ExcuteAssemblyByForm(string dllPath, string typeName)
{
assembly = AssemblyHelper.LoadAssembly(dllPath);
Type type = assembly.GetType("DemoForm_Show.Form1");
object o = Activator.CreateInstance(type);
int str = (int)type.InvokeMember("Add", BindingFlags.Default | BindingFlags.InvokeMethod, null, o, new object[] { "000" });
int str2 = (int)type.InvokeMember("Write", BindingFlags.Default | BindingFlags.InvokeMethod, null, o, new object[] { "000" });
PropertyInfo propertyInfo = type.GetProperty("Name1"); //获取Name属性对象
type.GetProperty("Name1").SetValue(o, "012", null);
var name = propertyInfo.GetValue(o, null); //获取Name属性的值
PropertyInfo propertyInfo2 = type.GetProperty("Age"); //获取Age属性对象
propertyInfo2.SetValue(o, "34", null); //把Age属性设置为34
var age = propertyInfo2.GetValue(o, null);
//assembly.CreateInstance(DllName, true);
IPlugin plugin = (IPlugin)assembly.CreateInstance(typeName, true);
PluginInfo pluginInfo = new PluginInfo();
pluginInfo.AssemblyPath = "";
pluginInfo.Plugin = plugin;
//调用dll的方法
IFormPlugin formPlugin = (IFormPlugin)pluginInfo.Plugin;
Form form = formPlugin.Content;
if (form.IsDisposed)
{
form = PluginHelper.CreateNewInstance<Form>(pluginInfo.AssemblyPath);
}
if (formPlugin.ShowAs == ShowAs.Dialog)
{
form.ShowDialog();
}
else
{
form.Show();
}
}