使用反射引发事件 [英] Raise Event using Reflection

查看:105
本文介绍了使用反射引发事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用VB.NET 4.0和一个第三部分dll
3rd party dll中有一个事件:

Hi I am using VB.NET 4.0 and a 3rd Part dll
there is an event in 3rd party dll :

Private Sub BioBridgeSDK_OnAttTransactionEx(ByVal sender As Object, ByVal e As  AxBioBridgeSDKLib._DBioBridgeSDKEvents_OnAttTransactionExEvent) Handles                     BioBridgeSDK.OnAttTransactionEx
End Sub



我想通过动态使用反射来实现.当在第三部分设备上执行某项操作时,上述事件是由我的反思引起的.为此,我



I want to that with dynamically using reflection. when something doing on 3rd part device, the above event fore from my reflextion. for that I am doing,

Dim CAssembly As Assembly = Assembly.LoadFrom(My.Application.Info.DirectoryPath & "\" & "AxInterop.BioBridgeSDKLib.dll")
Dim Instance As Object = CAssembly.CreateInstance("AxBioBridgeSDKLib.AxBioBridgeSDK")
 
Dim ei As EventInfo = Instance.GetType.GetEvent("OnAttTransactionEx")
Dim mi As MethodInfo = Instance.GetType.GetMethod("add_OnAttTransactionEx")
ei.AddEventHandler(Instance, MulticastDelegate.CreateDelegate(ei.EventHandlerType, Instance, mi))



但上面的行给我一个错误:

System.ArgumentException was unhandled Message=Error binding to target method.

我的代码有什么问题?请轻松帮我



but the above line throw me an error :

System.ArgumentException was unhandled Message=Error binding to target method.

What is wrong in my code? please help me

推荐答案

您正在尝试添加事件处理程序并弄乱参数.添加事件处理程序将无济于事.您无需触摸事件处理程序的调用列表,只需引发事件即可.

使用GetEvent,您已经获得了类System.Reflection.EventInfo的实例.现在您需要提高它.下一步是调用类型为System.Reflection.MethodInfoSystem.Reflection.EventInfo.GetRaiseMethod,并使用反射方法调用来调用此方法.您应该期望该方法具有两个参数(一个隐藏参数是"self",它是声明类的实例,这是您已经初始化的Instance变量):对象类型的发送者和类型<的事件参数c6>或派生类.

您可以使用System.Reflection.MethodInfo.GetParameters来查找raise方法的签名,使用类System.Reflection.ParameterInfo可以找到每个参数的类型和其他属性.

您可以使用System.Reflection.MethodInfo.Invoke调用raise方法.这将是使用提供的参数引发事件实例的最后一步.

有关更多详细信息,请参见:
http://msdn.microsoft.com/en-us/library/system.reflection. methodinfo.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/system.reflection. parameterinfo.aspx [ ^ ].

—SA
You''re trying to add event handler and mess up with parameters. Adding an event handler won''t help you. You don''t need to touch the invocation list of the event handler, you need to raise the event.

Using GetEvent, you already got the instance of the class System.Reflection.EventInfo. Now you need to raise it. Your next step is to call System.Reflection.EventInfo.GetRaiseMethod of the type System.Reflection.MethodInfo and call this method using Reflection method invocation. You should expect the method with two parameters (and one hidden parameter is "self" which is the instance of the declaring class, this is your Instance variable you already initialized): sender of the object type and event arguments of the type System.EventArgs or a derived class.

You can find out the signature of the raise method by using System.Reflection.MethodInfo.GetParameters, the type and other properties of each parameter using the class System.Reflection.ParameterInfo.

You can invoke the raise method using System.Reflection.MethodInfo.Invoke. That will be your last step in raising the instance of the event with the parameters you provide.

For further detail, see:
http://msdn.microsoft.com/en-us/library/system.reflection.methodinfo.aspx[^],
http://msdn.microsoft.com/en-us/library/system.reflection.parameterinfo.aspx[^].

—SA


这篇关于使用反射引发事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆