I am trying to create an event delegate where the parameter is strongly typed to match the current class, for example:
public class HPCRequest { public delegate void RequestCompleteHandler(HPCRequest request); public event RequestCompleteHandler RequestComplete;
The problem is that the point of this class must be inherited, and I really want all these inheriting classes to have the "RequestComplete" event, where a delegate was introduced for this class:
public class HPCGetConfig : HPCRequest {
This is because at the moment, when I have a function that processes one of my "RequestComplete" events, I currently have to do this:
myGetConfigRequest.RequestComplete += new HPCRequest.RequestCompleteHandler(HPCGetExpectedLosses_RequestComplete); void HPCGetConfig_RequestComplete(HPCRequest request) { HPCGetConfig thisRequest = request as HPCGetConfig;
But I want to be able to do something like this:
request.RequestComplete += new HPCGetConfig.RequestCompleteHandler(HPCGetConfig_RequestComplete); request.SendRequestAsync(); } void HPCGetConfig_RequestComplete(HPCGetConfig request) { request.RequestComplete -= HPCGetConfig_RequestComplete;
Attempts
I tried this:
public delegate void RequestCompleteHandler<T>(T request) where T : HPCRequest; public event RequestCompleteHandler<T> RequestComplete;
but when I try to trigger an event from the base class using RequestComplete(this); , I get a compile-time error: `Delegate 'RequestCompleteHandler' has some invalid arguments.
This will happen if I configured the entire HPCRequest class as HPCRequest<T> by going:
public class HPCRequest<T> where T : HPCRequest<T> { public delegate void RequestCompleteHandler<T>(T request); public event RequestCompleteHandler<T> RequestComplete; public class HPCGetConfig : HPCRequest<HPCGetConfig> { ...
The same thing happens when you try to raise an event: RequestComplete(this);
I also tried all delegate creation forms and events and redefined them, for example:
public class HPCRequest { public delegate void RequestCompleteHandler(HPCRequest request); public virtual event RequestCompleteHandler RequestComplete; public sealed class HPCGetConfig : HPCRequest { public delegate void RequestCompleteHandler(HPCGetConfig request); public override event RequestCompleteHandler RequestComplete;
But this gives me a compile-time error because I cannot override the RequestComplete event with one of the delegate types.
Any other ideas?
Edit
The template for the entire HPCRequest class is not an option, after a very thorough attempt, I see that it just screwed up each attempt to use the HPCRequest type as a placeholder for any type of request. If this solution works, the HPCRequest class must be created and inherited without specifying a type parameter. I need a solution that does not require HPCRequest templates.
To make sure everyone knows exactly how I am trying to use this, I inserted some code examples in pastebin so you can experiment with how to get this event to pattern work without breaking anything. Here it is: http://pastebin.com/bbEYgLj1