如何将委托转换为相同的委托? [英] How to convert delegate to identical delegate?
本文介绍了如何将委托转换为相同的委托?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
对委托有两个描述:首先,在第三方程序集中:
There are two descriptions of the delegate: first, in a third-party assembly:
public delegate void ClickMenuItem (object sender, EventArgs e)
秒,标准:
public delegate void EventHandler (object sender, EventArgs e);
我正在尝试编写一种方法,该方法将接收EventHandler类型的参数,并将使用参数ClickMenuItem调用第三方库.
I'm trying to write a method that will receive a parameter of type EventHandler and will call third-party library, with the parameter ClickMenuItem.
如何将ClickMenuItem转换为EventHandler?
How to convert the ClickMenuItem to EventHandler?
推荐答案
幸运的是,它很简单.您可以只写:
Fortunately, it's simple. You can just write:
ClickMenuItem clickMenuItem = ...; // Wherever you get this from
EventHandler handler = new EventHandler(clickMenuItem);
相反:
EventHandler handler = ...;
ClickMenuItem clickMenuItem = new ClickMenuItem(handler);
这甚至可以在C#1.0中使用.请注意,如果您随后更改了原始变量的值,则不会的更改将反映在已转换"变量中.例如:
This will even work in C# 1.0. Note that if you then change the value of the original variable, that change won't be reflected in the "converted" one. For example:
ClickMenuItem click = new ClickMenuItem(SomeMethod);
EventHandler handler = new EventHandler(click);
click = null;
handler(this, EventArgs.Empty); // This will still call SomeMethod
这篇关于如何将委托转换为相同的委托?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文