473,385 Members | 1,523 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.

Checking that an event has handlers added to it - using Reflection

Hello:

I am creating a simple class library for simplifying reflection.

I am creating an EventWrapper class that allows programmers to treat
it like an event (minus the operator overloading).

I am concerned with how to check that handlers have been added to the
event prior to raising it.

For instance,

if (TextChanged != null)
{
TextChanged(this, EventArgs.Empty);
}

tests whether there are any handlers prior to raising the event. How
can I do this with the EventInfo class?

Thanks!
Nov 27 '07 #1
8 1689
Basically there is no robust way of doing this... your cited code
masks a compiler trick - a hidden delegate field. EventInfo points to
the accessors, not the implementation.
It is (commonly) the *field* (sitting behind the accessors) that has
the value, and you'd need to check that. But sometimes the field is
developer-generated (with custom add/remove accessors) and sometimes
there *is* no such field (perhaps using EventHandlerList). Or perhaps
it does something even more esoteric - registers subscribers in a
centralised class for some reason.

Marc
Nov 27 '07 #2
On Nov 27, 10:55 am, Marc Gravell <marc.grav...@gmail.comwrote:
Basically there is no robust way of doing this... your cited code
masks a compiler trick - a hidden delegate field. EventInfo points to
the accessors, not the implementation.
It is (commonly) the *field* (sitting behind the accessors) that has
the value, and you'd need to check that. But sometimes the field is
developer-generated (with custom add/remove accessors) and sometimes
there *is* no such field (perhaps using EventHandlerList). Or perhaps
it does something even more esoteric - registers subscribers in a
centralised class for some reason.

Marc
It sounds like I should just let an error happen then. What would be
the benefit of masking it with a try {} catch {}. Would the hide
errors that took place in the event handlers? or would it just hide
that the event didn't have any listeners?
Nov 27 '07 #3
It sounds like I should just let an error happen then. What would be
the benefit of masking it with a try {} catch {}. Would the hide
errors that took place in the event handlers? or would it just hide
that the event didn't have any listeners?
To be honest, I'm confused as to how you are going to (robustly) do
*anything* useful (other than subscribe/unsubscribe) with an
EventInfo. If you are thinking of using the GetRaiseMethod(), then
note the remark on MSDN:
<q>This method returns a null reference (Nothing in Visual Basic) for
events declared with the C# event keyword or the Visual Basic Event
keyword. This is because the C# and Visual Basic compilers do not
generate such a method.</q>

http://msdn2.microsoft.com/en-us/library/1a4k4e35.aspx

Marc
Nov 27 '07 #4
On Nov 27, 12:32 pm, Marc Gravell <marc.grav...@gmail.comwrote:
It sounds like I should just let an error happen then. What would be
the benefit of masking it with a try {} catch {}. Would the hide
errors that took place in the event handlers? or would it just hide
that the event didn't have any listeners?

To be honest, I'm confused as to how you are going to (robustly) do
*anything* useful (other than subscribe/unsubscribe) with an
EventInfo. If you are thinking of using the GetRaiseMethod(), then
note the remark on MSDN:
<q>This method returns a null reference (Nothing in Visual Basic) for
events declared with the C# event keyword or the Visual Basic Event
keyword. This is because the C# and Visual Basic compilers do not
generate such a method.</q>

http://msdn2.microsoft.com/en-us/library/1a4k4e35.aspx

Marc
I will have to test this. Thank you for bringing that to my attention.

Ah . . . you are right. MSDN is right. How the heck will I be able to
raise events for dynamically generated code?
Nov 27 '07 #5
Maybe if you post how you expect to eventually consume the completed
EventWrapper (i.e. your use-case) we might suggest the best solution?
Nov 27 '07 #6
On Nov 27, 1:02 pm, Marc Gravell <marc.grav...@gmail.comwrote:
Maybe if you post how you expect to eventually consume the completed
EventWrapper (i.e. your use-case) we might suggest the best solution?
On Nov 27, 1:02 pm, Marc Gravell <marc.grav...@gmail.comwrote:
Maybe if you post how you expect to eventually consume the completed
EventWrapper (i.e. your use-case) we might suggest the best solution?
Here is the gist of what I am trying to do.

We have lots of business objects to make for lots of projects. Right
now the most time-consuming thing is the creation of the business
objects, writing each of their properties, writing all their SQL and
all the other gobbledy gook.

I want to make a tool that allows the developer to pick columns from
tables (which I have already done) and have it pull out the correct
type, do string length/other data verification, and write base-line
SQL. I have everything I need to do that now. I wrote a reflection
class that can execute generated code. I have code that builds C# and
compiles it.

Now, generally, I don't intend to run the generated Assemblies, or
generate them. My main intent is to create the source code, which I
have already done. However, I am currently in the process of creating
an XML-based format that will allow me to configure B.O.s and have the
effects take place instantly without needing to recompile. The problem
comes when my . . . wait a minute!

Actually, now that I think about it, you can only raise an event
inside a class anyway! My source code generator should be responsible
for creating code for raising events, not the EventInfo class. The
only thing you really should be able to do to a reflected Event *is*
add or remove handlers, which my code already supports.

I can't think of a good reason to allow reflection to raise events.
Thank you for the verification. Now I can get this code generator to
work!

Thanks, you may have just helped me save my company a lot of time and
money.

!Travis
Nov 27 '07 #7
On 27 Nov, 22:46, "jehugalea...@gmail.com" <jehugalea...@gmail.com>
wrote:
On Nov 27, 1:02 pm, Marc Gravell <marc.grav...@gmail.comwrote:
Maybe if you post how you expect to eventually consume the completed
EventWrapper (i.e. your use-case) we might suggest the best solution?

On Nov 27, 1:02 pm, Marc Gravell <marc.grav...@gmail.comwrote:
Maybe if you post how you expect to eventually consume the completed
EventWrapper (i.e. your use-case) we might suggest the best solution?

Here is the gist of what I am trying to do.

We have lots of business objects to make for lots of projects. Right
now the most time-consuming thing is the creation of the business
objects, writing each of their properties, writing all their SQL and
all the other gobbledy gook.

I want to make a tool that allows the developer to pick columns from
tables (which I have already done) and have it pull out the correct
type, do string length/other data verification, and write base-line
SQL. I have everything I need to do that now. I wrote a reflection
class that can execute generated code. I have code that builds C# and
compiles it.

Now, generally, I don't intend to run the generated Assemblies, or
generate them. My main intent is to create the source code, which I
have already done. However, I am currently in the process of creating
an XML-based format that will allow me to configure B.O.s and have the
effects take place instantly without needing to recompile. The problem
comes when my . . . wait a minute!

Actually, now that I think about it, you can only raise an event
inside a class anyway! My source code generator should be responsible
for creating code for raising events, not the EventInfo class. The
only thing you really should be able to do to a reflected Event *is*
add or remove handlers, which my code already supports.

I can't think of a good reason to allow reflection to raise events.
Thank you for the verification. Now I can get this code generator to
work!

Thanks
No problem, but on this occasion I'm genuinely not sure I did
anything! For reference the common pattern is:

public event SomeEventHandler SomeEvent; // optionally bespoke add/
remove

protected void OnSomeEvent(...) { // args perhaps by delegate
signature
// code that fires SomeEvent
}

then you can simply find you EventInfo and then look for a MethodInfo
based on On{TheName}

Marc
Nov 28 '07 #8
(and perhaps virtual depending on scenario)
Nov 28 '07 #9

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

Similar topics

2
by: Grzegorz Danowski | last post by:
Hello, I'd like to hide control if any of its event handlers is forbidden for current user (basing on PrincipalPermissionAttribute). How to list all methods that handle given event? I supposed...
1
by: Armin Zingler | last post by:
Hi, I add event handlers to different events of objects of different type. In an array or arraylist, I want to store the information about which events I added. Later, I want to process the...
5
by: Lloyd Sheen | last post by:
Is there a way to get the event handlers such that I can cache the info about handlers for a particular control, remove the handlers, do some code and restore the cached event handlers in VB.NET...
6
by: cthoes | last post by:
following program does the display of student details but i want to search for the particular student name from the added list of student. so it would be great any one can help me without using...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
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: 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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.