473,396 Members | 2,036 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,396 software developers and data experts.

Need to cast System.Delegate to one of my own delegates dynamically. How do I do this?

More importantly, WHY do I wanna do this?

Well, I have events that I raise asynchronously like this:

foreach(eventhandler handler in eventname.getinvocationlist())
handler.begininvoke();

Now, I'd like to put a callback in there so I can call endinvoke and catch
any exceptions that get thrown in event handling code. Now, to call
endinvoke, you have to save the IAsynchResult returned by begininvoke, and
then you have to call endinvoke on the same delegate instance. As you can
see from my snippet above, doing this in a loop would entail collections of
IAsynchResults and delegate instances. I find that to be less than elegant,
so I have a class that I create that stores the delegate instance and the ar
and contains the callback. My code then looks like this:

foreach(eventhandler handler in eventname.getinvocationlist())
{
EndInvokerClass endInvoker = new EndInvokerClass(handler);
endInvoker.ar = handler.begininvoke(new
AsyncCallback(endInvoker.endinvoke);
}

where EndInvokerClass.endInvoke()

looks like this:

endInvoke()
{
try
{
handler.endinvoke(ar);
}
catch(exception ex)
{
//notify the client of the exception that they should have caught in
their event handler in the first place
}
}

I would like to use this class for many different event handler types, so I
have the handler declared as System.Delegate in my class. The problem is,
you can't call .EndInvoke() on System.Delegate. Apparently it only works on
some delegate that you define yourself.

So, either I cast the delegates dynamically or I have to include a property
for every expected event handler type. Do you feel me here?
Jul 21 '05 #1
1 2011
So, either I cast the delegates dynamically or I have to include a property
for every expected event handler type. Do you feel me here?


You could perhaps do a late bound call to EndInvoke with Reflection.

FWIW, this should be a lot easier to do in the next .NET version
(codename Whidbey) with the generics support they're adding.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.
Jul 21 '05 #2

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

Similar topics

0
by: IceShock | last post by:
I have been studying the C# language for sometime now and i have noticed something about delegates. When are single cast delegates to be used? even a better question: How do we define a singlecast...
2
by: Jon Davis | last post by:
The garbage handler in the .NET framework is handy. When objects fall out of scope, they are automatically destroyed, and the programmer doesn't have to worry about deallocating the memory space...
17
by: Hazz | last post by:
In this sample code of ownerdraw drawmode, why does the '(ComboBox) sender' line of code need to be there in this event handler? Isn't cboFont passed via the managed heap, not the stack, into this...
2
by: Phil Jones | last post by:
I think I might be missing something simple/stupid - but I'm trying to get the delegates that are listed as event handlers for an event from within an object instance. For example: Namespace...
1
by: 0to60 | last post by:
More importantly, WHY do I wanna do this? Well, I have events that I raise asynchronously like this: foreach(eventhandler handler in eventname.getinvocationlist()) handler.begininvoke(); ...
4
by: Bill Woodruff | last post by:
< note : this message was sparked in part by comments by David Browne on a previous thread : "inserting an anonymous method as a value in a generic dictionary ?" : David had shown the use of...
5
by: Larry Smith | last post by:
Hi there, Given a delegate that points to a non-static member function, is there a cleaner way to change the object instance it's called on other than invoking "Delegate.CreateDelegate()" (and...
11
by: matsi.inc | last post by:
I am looking to make something like a delegate that i can use in my projects but am having a hard time getting started. The behavior I am most interested in is how a delegate changes it's Invoke...
20
by: raylopez99 | last post by:
Inspired by Chapter 8 of Albahari's excellent C#3.0 in a Nutshell (this book is amazing, you must get it if you have to buy but one C# book) as well as Appendix A of Jon Skeet's book, I am going...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.