怎么知道已经注册了哪些handler

zhangfire 2006-11-21 01:36:12
如:
button1.Click += myEventHandler1;
button1.Click += myEventHandler2;

注册handler是在runtime时完成的,因此需要判断是否在注册重复的handler。
请问怎么取到已经注册的handler?毕竟Event触发时,是知道需要执行哪些handler的。

分不够再加:)
...全文
321 17 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhangfire 2006-11-23
  • 打赏
  • 举报
回复
已经有结论了,呵呵。
1、如果要取所有的handler,只能在内部取,不能在外部取;
2、这个问题如果要解决,只能在外面单独记录所有的handler。
zhangfire 2006-11-22
  • 打赏
  • 举报
回复
https://forums.microsoft.com/msdn/showpost.aspx?postid=728500&siteid=1
zhangfire 2006-11-22
  • 打赏
  • 举报
回复
to 愚翁
framework自定义的control的delegate能否直接取到?
Knight94 2006-11-21
  • 打赏
  • 举报
回复
to 这时候,好像只能得到一个handler 在myHandler.GetInvocationList,但是会执行handler

不是一个,是两个。(这里你可以用我的例子测试一下)

其实这里用委托对象来判断不是很正确,因为有可能用一个变量去new两次,如果要判断的话,用方法的名字要合适一些。
zhangfire 2006-11-21
  • 打赏
  • 举报
回复
发现少打了字:
这时候, 在myHandler.GetInvocationList中好像只能得到一个handler,但是会执行这个handler两次。
zhangfire 2006-11-21
  • 打赏
  • 举报
回复
to 愚翁
button1.Click += myEventHandler1;
button1.Click += myEventHandler1;

这时候,好像只能得到一个handler 在myHandler.GetInvocationList,但是会执行handler
Knight94 2006-11-21
  • 打赏
  • 举报
回复
to 如果有重复的handler名字,是否就不能取出两个了?达不到判断是否已经有该handler的目的了。

你说的是委托的target?
// delegate sample
HelloHandler myHandler = new HelloHandler( WriteHello1 );
myHandler += new HelloHandler( WriteHello2 );
foreach( HelloHandler handler in myHandler.GetInvocationList() )
{
Debug.WriteLine( handler.Method.Name );//get method's name
handler( "test" );
}
zhangfire 2006-11-21
  • 打赏
  • 举报
回复
to shalen520:
感觉这样不太现实,我不会有地方去保存这个key值的,如果有的话,我可以直接保存这个handler的reference了。

to 愚翁老大:
如果有重复的handler名字,是否就不能取出两个了?达不到判断是否已经有该handler的目的了。
zhangfire 2006-11-21
  • 打赏
  • 举报
回复
非常感谢各位了,我看看先。
shalen520 2006-11-21
  • 打赏
  • 举报
回复
因为继承自Control,所以有一个EventHandlerList类型的Events属性

定义一个静态只读object作为key,通过这个key在Events中添加、移出或者查找EventHandler

Knight94 2006-11-21
  • 打赏
  • 举报
回复
Sample code as follows:
public delegate bool HelloHandler( string Msg );
private bool WriteHello1( string sMsg )
{
Debug.WriteLine( "WriteHello1:" + sMsg );
return true;
}
private bool WriteHello2( string sMsg )
{
Debug.WriteLine( "WriteHello2:" + sMsg );
return true;
}

// delegate sample
HelloHandler myHandler = new HelloHandler( WriteHello1 );
myHandler += new HelloHandler( WriteHello2 );
foreach( HelloHandler handler in myHandler.GetInvocationList() )
handler( "test" );

shalen520 2006-11-21
  • 打赏
  • 举报
回复
优化事件实现
上述的事件实现并没有优化性能。此实现为每个委托实例生成一个字段,当控件上定义了很多事件时,就会增加存储成本。基类 System.Web.UI.Control 为存储和检索事件委托提供了更有效的数据结构(通过其 Events 属性)。Events 属性的类型为 EventHandlerList,该类型是为有效存储和检索事件委托而设计的数据结构。下面的示例说明了使用 Events 属性的事件实现。此 C# 示例与本主题前面定义的 MyButton 示例只在其 Click 事件的实现上有所不同。实现此事件的代码用粗体突出显示。

注意 此版本的 Visual Basic .NET 不支持事件属性。
[C#]
using System;
using System.Web.UI;

namespace CustomControls
{
public class OptimizedEventButton: Control, IPostBackEventHandler
{
// Defines a key for storing the delegate for the Click event
// in the Events list.
private static readonly object ClickEvent = new object();

// Defines the Click event using the event property syntax.
// The Events property stores all the event delegates of
// a control as name/value pairs.
public event EventHandler Click
{
// When a user attaches an event handler to the Click event
// (Click += myHandler;), the Add method
// adds the handler to the
// delegate for the Click event (keyed by ClickEvent
// in the Events list).
add
{
Events.AddHandler(ClickEvent, value);
}
// When a user removes an event handler from the Click event
// (Click -= myHandler;), the Remove method
// removes the handler from the
// delegate for the Click event (keyed by ClickEvent
// in the Events list).
remove
{
Events.RemoveHandler(ClickEvent, value);
}
}

// Invokes delegates registered with the Click event.
//
protected virtual void OnClick(EventArgs e)
{
// Retrieves the event delegate for the Click event
// from the Events property (which stores
// the control's event delegates). You must
// cast the retrieved delegate to the type of your
// event delegate.
EventHandler clickEventDelegate = (EventHandler)Events[ClickEvent];
if (clickEventDelegate != null) {
clickEventDelegate(this, e);
}
}

// Method of IPostBackEventHandler that raises change events.
//
public void RaisePostBackEvent(string eventArgument)
{

OnClick(new EventArgs());
}

protected override void Render(HtmlTextWriter output)
{

output.Write("<INPUT TYPE = submit name = " + this.UniqueID +
" Value = 'Click Me' />");
}
}
}

注意 为简单起见,本文档中的一些其他示例使用事件字段来定义事件。但是,在您的控件中,应该使用此处讨论的优化实现。
鲁虾 2006-11-21
  • 打赏
  • 举报
回复
up
zhangfire 2006-11-21
  • 打赏
  • 举报
回复
倒,我没有装这个,我用的是2005版的august2006的,能给出是哪个子项吗?我去查查
shalen520 2006-11-21
  • 打赏
  • 举报
回复
看看这个:

ms-help://MS.MSDNQTR.2003FEB.2052/cpguide/html/cpcondefiningcustomevent.htm

优化事件实现
kui1015 2006-11-21
  • 打赏
  • 举报
回复
帮顶
冰宇枫 2006-11-21
  • 打赏
  • 举报
回复
友情up~~

111,092

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧