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: ...
随机推荐
- GPUImage中饱和度调整的实现——GPUImageSaturationFilter
饱和度saturation,是指色彩的鲜艳程度,也称色彩的纯度.饱和度取决于该色中含色成分和消色成分(灰色)的比例.含色成分越大,饱和度越大:消色成分越大,饱和度越小.纯的颜色都是高度饱和的,如鲜红, ...
- python3自动生成并运行bat批处理,并重定向输入消除黑窗口
#coding:utf-8import os #bat文件的内容(temp.bat)bat_name='temp.bat's1='''echo offipconfigecho Hello world! ...
- 访问服务器时一直在转圈,等待localhost响应
之后把zookeeper的服务器的防火墙关了,就有响应了.可能就是防火墙的问题.
- nginx配置资源缓存
缓存nginx服务器的静态文件.如css,js,htm,html,jpg,gif,png,flv,swf,这些文件都不是经常更新.便于缓存以减轻服务器的压力. 打开配置文件/usr/local/ngi ...
- tar 排除文件
tar -cvf test.tgz test/ --exclude *.txt --exclude *.jpg
- BBS-项目流程分析-表的创建
http://www.cnblogs.com/alice-bj/tag/Django%20-%20BBS/ https://www.cnblogs.com/venicid/p/9365019.html ...
- void类型详解
void含义 void的字面意思是"无类型",void*则为"无类型指针",void*可以指向任何类型的数据. void几乎只有"注释"和限 ...
- 使用py2exe转换python文件为可执行程序
py2exe可以将python脚本转换成在Windows上的可独立执行.exe程序的工具.可以让Python脚本在没有安装python工具的Windows系统上运行,方便脚本共享. 操作环境 pyth ...
- 注解(Annotation)是什么?
- Jsp基本语法 第二章
今天是星期天,我学习了关于Jsp的一些基本页面元素 首先学习了一些基本页面注释 1.HTML的注释 <!-- htmI注释-->// 客户端可见 2.JSP的注释: <%-- ...