Generics and delegates

M

MP

Hi,
I am using VS 2005 and I am trying to write a Generic class to fire
asynchronously the remoting events from a server.
I declare the generic like so...
public class AsyncEvent<Del> where Del : Delegate
and I get this error... 'Constraint cannot be special class
'System.Delegate"...

I understand what the message says, but does this mean that I absolutly
cannot write a generic for delegates... Is there another type I can use?

-Martin
 
M

Mickey Williams [C# MVP]

MP said:
Hi,
I am using VS 2005 and I am trying to write a Generic class to fire
asynchronously the remoting events from a server.
I declare the generic like so...
public class AsyncEvent<Del> where Del : Delegate
and I get this error... 'Constraint cannot be special class
'System.Delegate"...

I understand what the message says, but does this mean that I absolutly
cannot write a generic for delegates... Is there another type I can use?

-Martin

Delegates can be parameterized. I think you're writing the dependency upside
down:

public delegate void RemotingCallback<T>(object sender, T e) where T:
EventArgs;
public event RemotingCallback<MyEventArgs> ServerNotify;
 
A

Andreas Huber

Mickey Williams [C# MVP] wrote:
[snip]
Delegates can be parameterized. I think you're writing the dependency upside
down:

public delegate void RemotingCallback<T>(object sender, T e) where T:
EventArgs;
public event RemotingCallback<MyEventArgs> ServerNotify;

So you think it is *always* bad design to require a Delegate for a
generic parameter? If so, why?

Regards,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top