首页 > 代码库 > 插件模块与模块之间的通信(转)

插件模块与模块之间的通信(转)

在这里做个代码备注 防止下次忘记。。。
using System;using System.Collections.Generic;using System.IO;using System.Linq;using System.Reflection;using System.Windows;using System.Windows.Controls;using System.Windows.Forms;using MessageBox = System.Windows.MessageBox;namespace PLUGIN.SYSTEMFRAMEWORK.PLUGINS{ class PluginHelper { /// <summary> /// 模块之间 委托事件 通信 /// </summary> /// <param name="parentWindow">父Window</param> /// <param name="reflectioneventName">要反射模块中的 委托事件名称</param> /// <param name="currentEventName">在当前类中要执行的事件名称</param> /// <param name="filePath">反射文件的路经</param> /// <param name="plugin">插件模块信息</param> /// <param name="eventParameter">[参数]通信的消息 List集合对象</param> public static void ModulEventCommunication(Window parentWindow, string reflectioneventName, string currentEventName, string filePath, Plugin plugin, object[] eventParameter) { if (reflectioneventName == null) throw new ArgumentNullException("reflectioneventName"); if (currentEventName == null) throw new ArgumentNullException("currentEventName"); if (filePath == null) throw new ArgumentNullException("filePath"); if (eventParameter == null) throw new ArgumentNullException("eventParameter"); try { Assembly assembly = Assembly.LoadFrom(filePath + plugin.NameSpace + "." + plugin.Exends); Type[] type = assembly.GetTypes(); foreach (Type t in type) { //主要是判断该窗口是否实现 IElementsView 如果没有实现 就不需要反射或者不是插件 if (t.GetInterface("IElementsPlugin") != null && t.Name == plugin.ClassName) { //注:在这里要判断是否已经加载: if (AppData.LoadedPlugins.Contains(t.FullName)) { return; } //反射执行的成员和类型搜索 const BindingFlags myBindingFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static; EventInfo eventInfo = t.GetEvent(reflectioneventName, myBindingFlags); //获取到当前的类 var obj = (IElementsPlugin)assembly.CreateInstance(t.FullName); if (eventInfo != null) { Type tDelegate = eventInfo.EventHandlerType; MethodInfo methodHandler = parentWindow.GetType().GetMethod(currentEventName, myBindingFlags); //创建委托 Delegate d = Delegate.CreateDelegate(tDelegate, methodHandler); //获取将要处理的事件委托 MethodInfo minAddHandler = eventInfo.GetAddMethod(); object[] addHandlerArgs = { d }; //调用 minAddHandler.Invoke(obj, addHandlerArgs); FieldInfo field = t.GetField(reflectioneventName, myBindingFlags); if (field != null) { Object fieldValue = http://www.mamicode.com/field.GetValue(obj);"尊敬的用户您好!没有找到相应的模块!", "插件提示!", MessageBoxButton.OK, MessageBoxImage.Information); } catch (TargetParameterCountException) { MessageBox.Show("尊敬的用户您好!在调用模块时发现委托的参数不同!请检查参数的个数!", "插件提示!", MessageBoxButton.OK, MessageBoxImage.Information); } catch (Exception) { MessageBox.Show("尊敬的用户您好!系统发现版本与当前系统不匹配!", "插件提示!", MessageBoxButton.OK, MessageBoxImage.Information); } } /// <summary> /// 模块之间通信 事件委托 /// </summary> /// <typeparam name="T">对象实体</typeparam> /// <param name="parentWindow">父窗前</param> /// <param name="reflectioneventName">要反射模块中的 委托事件名称</param> /// <param name="currentEventName">在当前类中要执行的事件名称</param> /// <param name="filePath">反射文件的路经</param> /// <param name="plugin">插件模块信息</param> /// <param name="list">[参数]通信的消息</param> public static void ModuleEventCommunicationList<T>(Window parentWindow, string reflectioneventName, string currentEventName, string filePath, Plugin plugin, List<T> list) { if (reflectioneventName == null) throw new ArgumentNullException("reflectioneventName"); if (currentEventName == null) throw new ArgumentNullException("currentEventName"); if (filePath == null) throw new ArgumentNullException("filePath"); if (!list.Any()) return; try { Assembly assembly = Assembly.LoadFrom(filePath + plugin.NameSpace + "." + plugin.Exends); Type[] type = assembly.GetTypes(); foreach (Type t in type) { //主要是判断该窗口是否实现 IElementsView 如果没有实现 就不需要反射或者不是插件 if (t.GetInterface("IElementsPlugin") != null && t.Name == plugin.ClassName) { //注:在这里要判断是否已经加载: if (AppData.LoadedPlugins.Contains(t.FullName)) { return; } //反射执行的成员和类型搜索 const BindingFlags myBindingFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static; EventInfo eventInfo = t.GetEvent(reflectioneventName, myBindingFlags); //获取到当前的类 var obj = (IElementsPlugin)assembly.CreateInstance(t.FullName); if (eventInfo != null) { Type tDelegate = eventInfo.EventHandlerType; MethodInfo methodHandler = parentWindow.GetType().GetMethod(currentEventName, myBindingFlags); //创建委托 Delegate d = Delegate.CreateDelegate(tDelegate, methodHandler); //获取将要处理的事件委托 MethodInfo minAddHandler = eventInfo.GetAddMethod(); object[] addHandlerArgs = { d }; //调用 minAddHandler.Invoke(obj, addHandlerArgs); FieldInfo field = t.GetField(reflectioneventName, myBindingFlags); if (field != null) { Object fieldValue = http://www.mamicode.com/field.GetValue(obj);"尊敬的用户您好!没有找到相应的模块!", "插件提示!", MessageBoxButton.OK, MessageBoxImage.Information); } catch (TargetParameterCountException) { MessageBox.Show("尊敬的用户您好!在调用模块时发现委托的参数不同!请检查参数的个数!", "插件提示!", MessageBoxButton.OK, MessageBoxImage.Information); } catch (Exception) { MessageBox.Show("尊敬的用户您好!系统发现版本与当前系统不匹配!", "插件提示!", MessageBoxButton.OK, MessageBoxImage.Information); } } /// <summary> /// 模块之间通信 事件委托 /// </summary> /// <typeparam name="T">对象实体</typeparam> /// <param name="pwindow">父窗前</param> /// <param name="reflectionEventName">要反射模块中的 委托事件名称</param> /// <param name="currentEventName">在当前类中要执行的事件名称</param> /// <param name="filepath">反射文件的路经</param> /// <param name="plugin">插件模块信息</param> /// <param name="objEntity">通信的消息【Entity实体】</param> public static void ModuleEventCommuncationObjcet<T>(Window pwindow, string reflectionEventName, string currentEventName, string filepath, Plugin plugin, T objEntity) { if (reflectionEventName == null) throw new ArgumentNullException("reflectionEventName"); if (currentEventName == null) throw new ArgumentNullException("currentEventName"); if (filepath == null) throw new ArgumentNullException("filepath"); if (objEntity == null) throw new ArgumentNullException("objEntity"); try { Assembly assembly = Assembly.LoadFrom(filepath + plugin.NameSpace + "." + plugin.Exends); Type[] type = assembly.GetTypes(); foreach (Type t in type) { //主要是判断该窗口是否实现 IElementsView 如果没有实现 就不需要反射或者不是插件 if (t.GetInterface("IElementsPlugin") != null && t.Name == plugin.ClassName) { //注:在这里要判断是否已经加载: if (AppData.LoadedPlugins.Contains(t.FullName)) { return; } //反射执行的成员和类型搜索 const BindingFlags myBindingFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static; EventInfo eventInfo = t.GetEvent(reflectionEventName, myBindingFlags); //获取到当前的类 var obj = (IElementsPlugin)assembly.CreateInstance(t.FullName); if (eventInfo != null) { Type tDelegate = eventInfo.EventHandlerType; MethodInfo methodHandler = pwindow.GetType().GetMethod(currentEventName, myBindingFlags); //创建委托 Delegate d = Delegate.CreateDelegate(tDelegate, methodHandler); //获取将要处理的事件委托 MethodInfo minAddHandler = eventInfo.GetAddMethod(); object[] addHandlerArgs = { d }; //调用 minAddHandler.Invoke(obj, addHandlerArgs); FieldInfo field = t.GetField(reflectionEventName, myBindingFlags); if (field != null) { Object fieldValue = http://www.mamicode.com/field.GetValue(obj);"尊敬的用户您好!没有找到相应的模块!", "插件提示!", MessageBoxButton.OK, MessageBoxImage.Information); } catch (TargetParameterCountException) { MessageBox.Show("尊敬的用户您好!在调用模块时发现委托的参数不同!请检查参数的个数!", "插件提示!", MessageBoxButton.OK, MessageBoxImage.Information); } catch (Exception) { MessageBox.Show("尊敬的用户您好!系统发现版本与当前系统不匹配!", "插件提示!", MessageBoxButton.OK, MessageBoxImage.Information); } } }}

插件模块与模块之间的通信(转)