如何在 C# 中基于现有委托类型创建新委托类型?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/861835/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 04:38:42  来源:igfitidea点击:

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

c#inheritancedelegates

提问by Shalom Craimer

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 MyMouseEventDelegatewhich would have the same functionality as EventHandler<MouseEventArgs>.

有什么方法可以基于现有的委托类型创建新的委托类型?就我而言,我想创建一个MyMouseEventDelegateEventHandler<MouseEventArgs>.

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

我为什么要这个?当然,要利用编译时类型检查!这样,我可以有两个不同的代表:MyRightClickHandlerand MyLeftClickHandler,并且永远不会混淆两个代表- 即使它们在功能上都与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.

不够好。它不做任何类型检查,因为它实际上并没有创建一个新类型。而且我必须将这样的一行粘贴到我要引用的每个代码文件中MyRightClickHandler

采纳答案by Jon Skeet

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:

嗯,这就像从原始副本中复制委托声明一样简单。C# 中没有什么可以自动执行此操作,但我看不出它有多大负担:

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

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

它不会MouseEventHandler很快改变......

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?

你是否真的因为在错误的地方使用错误的代表而被错误咬伤?我不记得自己曾经发现过这个问题,而且在我看来你正在引入更多的工作(以及其他开发人员不熟悉的一组委托) - 你确定它值得吗?

回答by Fredrik M?rk

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:

我只想实现编译时类型检查,我想为您描述的情况创建专门的 EventArgs 类就足够了。这样你仍然可以利用预定义的委托:

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

EventHandler<RightMouseButtonEventArgs>
EventHandler<LeftMouseButtonEventArgs>

回答by Oneironaut

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

不可以。根据C# 文档,委托类型是密封的,您不能创建从现有委托类型继承的新委托类型。

回答by Trade-Ideas Philip

I didn't believe jon-skeet. I had to try it for myself. He was right.

我不相信乔恩斯基特。我不得不亲自尝试一下。他是对的。

public void F(object sender, MouseEventArgs e) {}; 
public MyLeftClickHandler l;
public MyRightClickHandler r;
public void Test()
{
    l = F;  // Automatically converted.
    r = F;  // Automatically converted to a different type.
    l = r;  // Syntax error.  Incompatible types.
    l = new MyLeftClickHandler(r);  // Explicit conversion works.
}

That wasn't obvious!

那不是很明显!