Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
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))

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
Updated 22-Jun-11 1:27am
Manfred Rudolf Bihy 22-Jun-11 9:08am    
I like your question! 5+
Sergey Alexandrovich Kryukov 22-Jun-11 11:32am    
Yes, not so easy one. Hope you will like my solution as well :-)

1 solution

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:[^],[^].

Share this answer
Manfred Rudolf Bihy 22-Jun-11 12:59pm    
Sounds like sound advice to me! 5+
Sergey Alexandrovich Kryukov 22-Jun-11 13:04pm    
Thank you, Manfred.
pdnet 23-Jun-11 1:32am    
No No No. I want automatic event to fire. where something happen in dll.
suppose I Button dynamically add in form. then want to muse button click event dynamically.
same thing I want here using reflextion
Sergey Alexandrovich Kryukov 24-Jun-11 18:38pm    
Please explain it more. If you want to add an event handler to raise other event you don't need reflection or you need it only for raising event. Will you describe the the scenario? What is accessible and what is not? For example you can handle ControlAdded directly.
Santopk 25-Oct-11 9:11am

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900