如果VB.NET中不存在,则以编程方式添加一个事件处理程序 [英] Programmatically add an event handler in VB.NET if it doesn't exist

查看:132
本文介绍了如果VB.NET中不存在,则以编程方式添加一个事件处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在ASP.NET网页中为多个控件使用单个事件处理程序。当且仅当它不存在时,我希望在运行时添加事件处理程序。在C#中,我会写如下:

I'm trying to use a single event handler for several controls in an ASP.NET web page. I would like to add the event handler at runtime if and only if it doesn't already exist. In C#, I would write it as seen below:

if (myTextBox.OnTextChanged == null)
{
    myTextBox.OnTextChanged += DoTextChangingValidation;
}

同样,我知道我可以删除事件处理程序如下:

Likewise, I know I can remove the event handler as follows:

if (myTextBox.OnTextChanged != null)
{
    myTextBox.OnTextChanged -= DoTextChangingValidation;
}

我知道如何在Visual Basic中添加和删除事件处理程序...但是我怎么弄清楚如何检查是否已经分配了?

I know how to add and remove the event handler in Visual Basic... But how do I figure out how to check to see if it is already assigned?

推荐答案

你不能从对象外部,在C#或Visual Basic中。事件基本上是两个访问者:添加删除。如果您手动编码,则可以立即显示:

You cannot do that from outside the object, either in C# or in Visual Basic. An event is basically two accessors: add and remove. This is immediately visible if you hand-code it:

public event EventHandler Click
{
     add { ... }
     remove { ... }
}

add_Click(EventHandler) remove_Click(EventHandler)方法。即使您使用默认事件实现,

These become add_Click(EventHandler) and remove_Click(EventHandler) methods. Even if you use the default event implementation,

public event EventHandler Click;

它仍然没有什么不同,除了使用默认实现为您生成访问器,私人多播代理字段与事件相同的名称存储这些处理程序。

it's still no different, except that the accessors are generated for you with the default implementation, which uses a private multicast delegate field with the same name as the event to store those handlers.

这意味着两件事:


  1. 对于该类的客户端,他们可以对事件进行的只有两件事是添加删除处理程序,因为只有访问者被暴露。没有访问者列出当前注册的处理程序

  1. For clients of the class, the only two things they can do about an event is add or remove handlers, since only accessors are exposed. There's no accessor to list currently registered handlers

即使您为事件使用默认实现,它提供了一个字段,该字段仍然是私有的,除了在同一个类的方法之外,它可以访问它。如果您有代码权限,可以使用反射,但请参阅#1为什么不是一般解决方案。

Even if you use default implementation for events, which provides a field, that field is still private, so you can't access it except from inside a method of the same class. You can use reflection if you have code permission to do so, but see #1 for why it's not a general solution.

这实际上是按照目的完成的。原因是这样的:对象可以被共享,而其他代码可能已经为它的事件注册了它的处理程序。如果您可以访问处理程序列表,那么您可以自己调用它们,可能会违反合同,并以类的所有者无意访问私有方法。

This is actually done on purpose. The reason is this: the object may be shared, and other code may have registered its handlers for its events. If you get access to list of handlers, you'd be able to call them yourself, potentially breaking the contract, and accessing private methods in a manner the owner of the class did not intend.

如果你想要这样的事情,需要在提供事件的类中完成 - 可以编写自己的 add 删除以检查是否有重复,或通过属性暴露私人字段。

If you want this kind of thing, it needs to be done in the class providing the event - either write your own add and remove to check for dupes, or expose the private field via a property.

这篇关于如果VB.NET中不存在,则以编程方式添加一个事件处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆