Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: 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 1:24am
pdnet515
Edited 22-Jun-11 1:27am
v2
Comments
Manfred R. Bihy at 22-Jun-11 9:08am
   
I like your question! 5+
SAKryukov at 22-Jun-11 11:32am
   
Yes, not so easy one. Hope you will like my solution as well :-)
--SA

1 solution

Rate this: bad
good
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:
http://msdn.microsoft.com/en-us/library/system.reflection.methodinfo.aspx[^],
http://msdn.microsoft.com/en-us/library/system.reflection.parameterinfo.aspx[^].
 
—SA
  Permalink  
Comments
Manfred R. Bihy at 22-Jun-11 12:59pm
   
Sounds like sound advice to me! 5+
SAKryukov at 22-Jun-11 13:04pm
   
Thank you, Manfred.
--SA
pdnet at 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 at 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.
--SA
Santopk at 25-Oct-11 9:11am
   
http://dotnetclass.blogspot.com/2011/09/dynamic-addhandler-dynamic-event-adding.html

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

  Print Answers RSS
0 OriginalGriff 527
1 CPallini 510
2 Sergey Alexandrovich Kryukov 438
3 George Jonsson 271
4 Afzaal Ahmad Zeeshan 184
0 OriginalGriff 5,370
1 CPallini 4,360
2 Sergey Alexandrovich Kryukov 3,822
3 George Jonsson 3,037
4 Gihan Liyanage 2,435


Advertise | Privacy | Mobile
Web03 | 2.8.140916.1 | Last Updated 22 Jun 2011
Copyright © CodeProject, 1999-2014
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