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

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 2008
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: 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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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: 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...

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.