Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create a new delegate type based on an existing one, in C#?

Is there any way that I can create a new delegate type based on an existing one? In my case, I'd like to create a delegate MyMouseEventDelegate which would have the same functionality as EventHandler<MouseEventArgs>.

Why do I want this? To take advantage of compile-time type-checking, of course! This way, I can have two different delegates: MyRightClickHandler and MyLeftClickHandler, and never the twain shall be confused - even if they are both functionally identical to EventHandler<MouseEventArgs>.

Is there syntax to do this sort of thing?

Oh, and code like:

using MyRightClickHandler = EventHandler<MouseEventArgs>

isn't good enough. It doesn't do any type-checking, since it doesn't actually create a new type. And I'd have to paste such a line into every code file in which I would refer to MyRightClickHandler.

like image 763
Shalom Craimer Avatar asked May 14 '09 06:05

Shalom Craimer


People also ask

What is the right way to add a method to the delegate?

Multiple methods can be assigned to the delegate using "+" or "+=" operator and removed using "-" or "-=" operator. It is called multicast delegate. If a multicast delegate returns a value then it returns the value from the last assigned target method.

Can a delegate point to more than 1 method?

A delegate is a type safe and object oriented object that can point to another method or multiple methods which can then be invoked later. It is a reference type variable that refers to methods.

How many methods can be called using a delegate?

Only one method can be called using a delegate.

How do you define generic delegates?

Delegates defined within a generic class can use the generic class type parameters in the same way that class methods do. Generic delegates are especially useful in defining events based on the typical design pattern because the sender argument can be strongly typed and no longer has to be cast to and from Object.


3 Answers

Well, it's as simple as copying the delegate declaration from the original. There's nothing in C# to do this automatically, but I can't see it's much of a burden:

public delegate void MyLeftClickHandler(object sender, MouseEventArgs e);
public delegate void MyRightClickHandler(object sender, MouseEventArgs e);

It's not like MouseEventHandler is going to change any time soon...

Have you actually been bitten by bugs due to using the wrong delegates in the wrong places though? I can't remember ever having found this a problem myself, and it seems to me you're introducing more work (and an unfamiliar set of delegates for other developers) - are you sure it's worth it?

like image 108
Jon Skeet Avatar answered Oct 13 '22 07:10

Jon Skeet


I you only want to achieve compile time type checking I guess it would be enough to create specialized EventArgs classes for the cases that you describe. That way you can still take advantage of the pre-defined delegates:

class RightMouseButtonEventArgs : MouseEventArgs {}
class LeftMouseButtonEventArgs : MouseEventArgs {}

EventHandler<RightMouseButtonEventArgs>
EventHandler<LeftMouseButtonEventArgs>
like image 27
Fredrik Mörk Avatar answered Oct 13 '22 07:10

Fredrik Mörk


No. According to the C# documentation, delegate types are sealed, and you cannot create a new delegate type that inherits from an existing one.

like image 24
Oneironaut Avatar answered Oct 13 '22 05:10

Oneironaut