C#反射调用其它DLL的委托事件 传值
C#反射调用其它DLL的委托事件 传值
在插件式开发。我们要调用其它插件或模块的委托事件时、那么我们需要通过反射。
复制代码
namespace Module2
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
[Plugin("MainWindow", "测试反射")]
public partial class MainWindow
{
public delegate void TestHandler(string msg,string info,string text);
public event TestHandler TestEvent;
public MainWindow()
{
InitializeComponent();
}
protected virtual void ValueInformation(string text, string info, string tb)
{
TestHandler handler = TestEvent;
if (handler != null)
handler(text,info,tb);
}
private void button1_Click(object sender, RoutedEventArgs e)
{
if (TestEvent != null)
{
}
ValueInformation("你好!钓鱼岛是中国的!!","打死小日本!!",textBox1.Text);
}
public override void UninstallEvent()
{
//添加已经打开的窗口 :在关闭的时候 要删除此项
ApplicationSharedData.LoadedPlugins.Remove("Module2.MainWindow");
base.UninstallEvent();
}
}
}
复制代码
复制代码
/// <summary>
/// 模块之间 委托事件 通信
/// </summary>
/// <param name="currentWindow">当前的Window</param>
/// <param name="reflectioneventName">要反射模块中的 委托事件名称</param>
/// <param name="currentEventName">在当前类中要执行的事件名称</param>
/// <param name="filePath">反射文件的路经</param>
/// <param name="plugin">插件</param>
/// <param name="eventParameter">通信的消息</param>
public static void ModulEventCommunication(Window currentWindow, 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("IElementsView") != null && t.Name == plugin.ClassName)
{
//注:在这里要判断是否已经加载:
if (LoadedPlugins.Contains(t.FullName))
{
return;
}
//反射执行的成员和类型搜索
const BindingFlags myBindingFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static;
EventInfo eventInfo = t.GetEvent(reflectioneventName, myBindingFlags);
//获取到当前的类
var obj = (IElementsView)assembly.CreateInstance(t.FullName);
if (eventInfo != null)
{
Type tDelegate = eventInfo.EventHandlerType;
MethodInfo methodHandler = currentWindow.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 = field.GetValue(obj);
if (fieldValue != null && fieldValue is Delegate)
{
Delegate objectDelegate = fieldValue as Delegate;
//动态调用
objectDelegate.DynamicInvoke(eventParameter);
}
}
}
if (obj != null)
{
obj.ShowWindow();
//添加已经打开的窗口 :在关闭的时候 要删除此项
LoadedPlugins.Add(t.FullName);
}
}
}
}
catch (FileNotFoundException)
{
MessageBox.Show("尊敬的用户您好!没有找到相应的模块!", "插件提示!", MessageBoxButton.OK,
MessageBoxImage.Information);
}
catch (TargetParameterCountException)
{
MessageBox.Show("尊敬的用户您好!在调用模块时发现委托的参数不同!请检查参数的个数!", "插件提示!", MessageBoxButton.OK,
MessageBoxImage.Information);
}
catch (Exception)
{
MessageBox.Show("尊敬的用户您好!系统发现版本与当前系统不匹配!", "插件提示!", MessageBoxButton.OK,
MessageBoxImage.Information);
}
}
复制代码
本模块调用:
复制代码
void Test2_Click(object sender, RoutedEventArgs e)
{
var item = (MenuItem)sender;
string path = @Environment.CurrentDirectory + "\\";
Plugin plugin = ApplicationSharedData.Plugins[item.Header.ToString()];
ApplicationSharedData.ModulEventCommunication(this, "TestEvent", "DeleBindData", path, plugin, new object[] { "参数1", "参数2","" });
//注 在反射TestEvent 时候 ,TestEvent有几个参数 那么S_TestEvent 也有几个参数。
//不然会抛出异常信息,TargetParameterCountException 参数的个数不同
}
private static void DeleBindData(string msg, string info,string tb)
{
_textbox.Text = tb;
}
复制代码

void Test2_Click(object sender, RoutedEventArgs e)
{
var item = (MenuItem)sender;
string path = @Environment.CurrentDirectory + "\\";
Plugin plugin = ApplicationSharedData.Plugins[item.Header.ToString()]; ApplicationSharedData.ModulEventCommunication(this, "TestEvent", "DeleBindData", path, plugin, new object[] { "参数1", "参数2","" });
//注 在反射TestEvent 时候 ,TestEvent有几个参数 那么S_TestEvent 也有几个参数。
//不然会抛出异常信息,TargetParameterCountException 参数的个数不同
} private static void DeleBindData(string msg, string info,string tb)
{
_textbox.Text = tb;
}

C#反射调用其它DLL的委托事件 传值的更多相关文章
- C#反射调用外部Dll,执行其中异步函数并取返回值
using System.Reflection; 1.载入Dll Assembly asm=Assembly.LoadFile(FullPath);//FullPath 为Dll所在位置的全路径. 2 ...
- 反射调用发生错误信息 LoadNeither
错误信息: Service cannot be started. System.Reflection.TargetInvocationException: Exception has been thr ...
- 外壳exe通过反射调用dll时
外壳exe通过反射调用dll时,dll是 4.0的框架,外壳exe也需要编译成4.0的框架,如果dll本身有调用32位的dll,那么外壳exe也需要编译成32位. 调试时报的那个错,直接继续运行,不影 ...
- QT编写DLL给外部程序调用,提供VC/C#/C调用示例(含事件)
最近这阵子,接了个私活,封装一个开发包俗称的SDK给客户调用,查阅了很多人家的SDK,绝大部分用VC编写,而且VC6.0居多,估计也是为了兼容大量的XP用户及IE浏览器,XP自带了VC6.0运行库,所 ...
- 反射调用DLL
public static type GetClassType(string ClassName,string dllname) { Type ClassType =null; foreach(Ass ...
- 利用反射--调用一个按钮的Click事件
最基本的调用方法 (1)button1.PerformClick();(2)button1_Click(null,null);(3)button_Click(null,new EventArgs()) ...
- C# 反射调用私有事件
原文:C# 反射调用私有事件 在 C# 反射调用私有事件经常会不知道如何写,本文告诉大家如何调用 假设有 A 类的代码定义了一个私有的事件 class A { private event EventH ...
- 2019-11-29-C#-反射调用私有事件
原文:2019-11-29-C#-反射调用私有事件 title author date CreateTime categories C# 反射调用私有事件 lindexi 2019-11-29 08: ...
- 2019-8-30-C#-反射调用私有事件
title author date CreateTime categories C# 反射调用私有事件 lindexi 2019-08-30 08:52:57 +0800 2018-09-19 20: ...
随机推荐
- C# 设计模式-策略者模式(Strategy)
策略模式的用意是针对一组算法,将每一个算法封装到具有共同接口的独立的类中,从而使得它们可以相互替换.策略模式使得算法可以在不影响到客户端的情况下发生变化. 策略模式是对算法的包装,是把使用算法的责任和 ...
- winform 布局、容器
一.布局 属性:1.Anchor: 绑定控件到容器边缘位置保持不变 注:四周全锁定时控件随界面变化时变大 2.Dock:绑定到容器的边缘 注:下控件到边需先将下控件定义到边再将左右控件新建.到边 3 ...
- mybatis三(关联查询)
一.类属性 @Alias("depart")public class Department { private Integer id; private String departN ...
- python3自动生成并运行bat批处理,并重定向输入消除黑窗口
#coding:utf-8import os #bat文件的内容(temp.bat)bat_name='temp.bat's1='''echo offipconfigecho Hello world! ...
- Flex学习笔记-皮肤
1文件结构 MXML应用程序 index.mxml 皮肤文件 components.button.skin.btnSkin1.mxml 皮肤文件的组件随便引用了spark.components.Bu ...
- shell doc
参考: http://c.biancheng.net/view/1114.html
- os sys 模块
os.getcwd() 获取当前工作目录,即当前python脚本工作的目录路径 os.chdir("dirname") 改变当前脚本工作目录:相当于shell下cd os.chdi ...
- DLL 调试(C# 调用 C++ 的 DLL)
操作步骤: (1) C++ 的 DLL 项目中设置断点: (2) C# 工程右键[属性]->[调试]->[启动调试器]中选中[启动本机代码调试]. 注:要调试 DLL 必须有 DLL 的源 ...
- Scrapy框架--代理和cookie
如何发起post请求? 代理和cookie: cookie:豆瓣网个人登录,获取该用户个人主页这个二级页面的页面数据. 如何发起post请求? 一定要对start_requests方法进行重写. 1. ...
- [PHP]PHP缓存机制之Output Control
---------------------------------------------------------------------------------------------------- ...