Click here to Skip to main content
12,749,754 members (32,006 online)
Rate this:
Please Sign up or sign in to vote.
See more: VB VB.NET
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
Posted 22-Jun-11 2:24am
Updated 22-Jun-11 2:27am
Manfred R. Bihy 22-Jun-11 9:08am
I like your question! 5+
SAKryukov 22-Jun-11 11:32am
Yes, not so easy one. Hope you will like my solution as well :-)

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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

Manfred R. Bihy 22-Jun-11 12:59pm
Sounds like sound advice to me! 5+
SAKryukov 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
SAKryukov 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)

    Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170215.1 | Last Updated 22 Jun 2011
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100