473,800 Members | 2,541 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(eventha ndler handler in eventname.getin vocationlist())
handler.beginin voke();

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(eventha ndler handler in eventname.getin vocationlist())
{
EndInvokerClass endInvoker = new EndInvokerClass (handler);
endInvoker.ar = handler.beginin voke(new
AsyncCallback(e ndInvoker.endin voke);
}

where EndInvokerClass .endInvoke()

looks like this:

endInvoke()
{
try
{
handler.endinvo ke(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 2041
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
3938
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 delegate? The thing is I have been using the .net v 1.1 2003 frame work and i have been told that this might be the issue. I have been told that in this new framework the SingleCast delegates are abandoned and that everythin now is
2
9201
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 for those objects. In fact, all the programmer has to worry about is the total sum of objects loaded into RAM at any known point. Memory leaks are not a problem. .... So one would like to think. The reality is that delegates and event...
17
2677
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 cboFont_DrawItem event handler? Why does it need to be cast? -hazz ,................. cboFont.Items.AddRange(FontFamily.Families); } private void cboFont_DrawItem(object sender,
2
8463
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 Foo Public Delegate Sub MyEventHandler(ByVal sender As Object, _ ByVal e As EventArgs) Public Class MyObject
1
362
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(); 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
4
3393
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 'Delegate as a valid Type declaration for the Value of a Generic Dictionary. I am curious as to why I can compile and use this syntax : it seems to me to violate the requirement that the Value of a Generic Dictionary be a Type Name.
5
457
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 changing the object instance while leaving the same member function intact). Thanks in advance.
11
2353
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 method dynamically to match the arguments supplied to it when it is defined. For example... public delegate void MyDelegate(string myString, int myInt);
20
2911
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 through some LINQ queries. But how to cast? ( See the below, modified from somebody else's code. The problem is the query 'stops' (throws a cast exception) at "3", and never gets to "violet".
0
9690
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9551
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10504
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10274
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7576
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6811
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5469
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5606
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3764
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.