473,385 Members | 2,003 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

EventInfo.AddEventHandler question

Hi folks ! :O)

Let's say I have the following EventHandler defined in a class :
'***
Option Explicit On

Public Delegate Sub MyEventHandler(ByVal sender As Object, ByVal e As
MyEventArgs)

Public NotInheritable Class MyEventArgs
Inherits EventArgs

Private m_nValue As Int32

Public Property Value() As Int32
Get
Return m_nValue
End Get
Set(ByVal Value As Int32)
m_nValue = Value
End Set
End Property

End Class

Public NotInheritable Class MyEventThrower

Public Event MyEvent As MyEventHandler

Public Sub MyThrowEventSub()

Dim e As New MyEventArgs
RaiseEvent MyEvent(Me, e)
Console.WriteLine(e.Value)

End Sub

End Class
'***

How would one use the EventInfo.AddEventHandler() method in order to catch
this event ?

Here's the sample code i tried that failed miserably :
'***
Private Sub Form1_Load( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load

Dim a As [Assembly] = [Assembly].LoadFile("C:\MyAssembly.dll")
Dim t As Type = a.GetType(String.Format("{0}.MyEventThrower", _
a.GetName().Name))
Dim o As Object = Activator.CreateInstance(t)
Dim ev As EventInfo = t.GetEvent("MyEvent")
Dim handler As [Delegate] = _
[Delegate].CreateDelegate(ev.EventHandlerType, _
Me,
"MyEventHandler")

ev.AddEventHandler(o, handler)
t.InvokeMember("MyThrowEventSub", _
BindingFlags.Public Or _
BindingFlags.InvokeMethod Or _
BindingFlags.Instance, _
Nothing, o, Nothing)
ev.RemoveEventHandler(o, handler)

End Sub

' this doesn't not compile since MyEventArgs is not defined..
'Public Sub MyEventHandler(ByVal sender As Object, ByVal e As
MyEventArgs)
' e.Value = 15
'End Sub
'***

Thanks in advance for any ideas.
--
Best Regards
Yanick
Nov 21 '05 #1
3 4715
"Zoury" <yanick_lefebvre at hotmail dot com> schrieb
' this doesn't not compile since MyEventArgs is not defined..
'Public Sub MyEventHandler(ByVal sender As Object, ByVal e As
MyEventArgs)
' e.Value = 15
'End Sub
'***

Set a reference to the dll. As you use the type 'MyEventArgs', it must be
declared.
Armin

Nov 21 '05 #2
Hi Armin ! :O)
Set a reference to the dll. As you use the type 'MyEventArgs', it must be
declared.


What if we don't want or can't set any reference to the dll ?
If i had a reference to the dll in the first place i wouldn't need to use
reflection at all. ;O)
For the records, I did it by declaring the MyEventHandler with an EventArgs
parameter type like this (Object would have worked to..) :
'***
Public Delegate Sub MyEventHandler(ByVal sender As Object, ByVal e As
EventArgs)

Public NotInheritable Class MyEventArgs
Inherits EventArgs

Private m_nValue As Int32

Public Property Value() As Int32
Get
Return m_nValue
End Get
Set(ByVal Value As Int32)
m_nValue = Value
End Set
End Property

End Class

Public NotInheritable Class MyEventThrower

Public Event MyEvent As MyEventHandler

Public Sub MyThrowEventSub()

Dim e As New MyEventArgs
RaiseEvent MyEvent(Me, e)
Console.WriteLine(e.Value)

End Sub

End Class
'***

I can now use whatever member of the MyEventArgs class instance using
reflection on the caller side like this :
'***
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Dim a As [Assembly] =
[Assembly].LoadFile("c:\LoadAssemblyAndAddHandlerLibrary.dll ")
Dim t As Type = a.GetType(String.Format("{0}.MyEventThrower",
a.GetName().Name))
Dim o As Object = Activator.CreateInstance(t)
Dim ev As EventInfo = t.GetEvent("MyEvent")
Dim handler As [Delegate] =
[Delegate].CreateDelegate(ev.EventHandlerType, Me, "MyEventHandler")

ev.AddEventHandler(o, handler)
t.InvokeMember("MyThrowEventSub", _
BindingFlags.Public Or _
BindingFlags.InvokeMethod Or _
BindingFlags.Instance, _
Nothing, o, Nothing)
ev.RemoveEventHandler(o, handler)

End Sub

Public Sub MyEventHandler(ByVal sender As Object, ByVal e As EventArgs)

Dim a As [Assembly] =
[Assembly].LoadFile("c:\LoadAssemblyAndAddHandlerLibrary.dll ")
Dim t As Type = a.GetType(String.Format("{0}.MyEventArgs",
a.GetName().Name))
Dim p As PropertyInfo = t.GetProperty("Value")
p.SetValue(e, 15, Nothing)

End Sub
'***
--
Best Regards
Yanick
Nov 21 '05 #3
"Zoury" <yanick_lefebvre at hotmail dot com> schrieb
Hi Armin ! :O)
Set a reference to the dll. As you use the type 'MyEventArgs', it
must be declared.


What if we don't want or can't set any reference to the dll ?
If i had a reference to the dll in the first place i wouldn't need
to use reflection at all. ;O)

How can you know the signature at devlopment time if type information is
discovered at run time?

The contradiction is: You say you don't have type information, that's why
you load it at run time. On the other side, you say, you do have some
information, that's why you know how to declare the event handler.

I think you took the wrong approach. If you do know the signature - or
whatever piece of information - write a base library containing base
classes or interfaces. Reference the base library. Load and create instances
using reflection from any assembly. Determine if the base interfaces are
supported or if the objects are derived from a base class (If typeof ...
is). If they are, assign the objects to variables of types declared in the
base library. Then you can also handle their events.
Armin

Nov 21 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Mohammed Mazid | last post by:
Can anyone please help me on how to move to the next and previous question? Here is a snippet of my code: Private Sub cmdNext_Click() End Sub Private Sub cmdPrevious_Click() showrecord
4
by: Mohammed Mazid | last post by:
Can anyone please help me on how to move to the next and previous question? Here is a snippet of my code: Private Sub cmdNext_Click() End Sub Private Sub cmdPrevious_Click() showrecord
3
by: Stevey | last post by:
I have the following XML file... <?xml version="1.0"?> <animals> <animal> <name>Tiger</name> <questions> <question index="0">true</question> <question index="1">true</question> </questions>
1
by: Sergio Geralnik | last post by:
I am trying to use this method to get a MethodInfo object but the method always returns a null. See my sample code below: this.button1 = new System.Windows.Forms.Button(); this.button1.Click +=...
3
by: Ekqvist Marko | last post by:
Hi, I have one Access database table including questions and answers. Now I need to give answer id automatically to questionID column. But I don't know how it is best (fastest) to do? table...
0
by: Zoury | last post by:
Hi folks ! :O) Let's say I have the following EventHandler defined in a class : '*** Option Explicit On Public Delegate Sub MyEventHandler(ByVal sender As Object, ByVal e As MyEventArgs) ...
53
by: Jeff | last post by:
In the function below, can size ever be 0 (zero)? char *clc_strdup(const char * CLC_RESTRICT s) { size_t size; char *p; clc_assert_not_null(clc_strdup, s); size = strlen(s) + 1;
56
by: spibou | last post by:
In the statement "a *= expression" is expression assumed to be parenthesized ? For example if I write "a *= b+c" is this the same as "a = a * (b+c)" or "a = a * b+c" ?
2
by: Allan Ebdrup | last post by:
Hi, I'm trying to render a Matrix question in my ASP.Net 2.0 page, A matrix question is a question where you have several options that can all be rated according to several possible ratings (from...
3
by: Zhang Weiwu | last post by:
Hello! I wrote this: ..required-question p:after { content: "*"; } Corresponding HTML: <div class="required-question"><p>Question Text</p><input /></div> <div...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.