vs.net 如何克隆对对象事件的订阅?

发布于 2024-08-02 02:55:44 字数 308 浏览 11 评论 0原文

我有一个自定义 datagridviewcolumn,其中添加了一个事件。 问题是,我无法弄清楚如何查看谁订阅了当前列对象的事件并将这些订阅添加到克隆的列对象。

我通过要求调用程序将处理程序的地址传递给自定义列中的委托来解决此问题,而不是向事件添加处理程序。

请原谅我的术语,我希望你明白我想说的!

通过接收对该方法的引用,datagridviewcolumn 现在拥有控制权,然后可以轻松克隆该引用。

这很好,但控件的用户希望能够通过在 Visual Studio 中选择事件来订阅事件 - 这将创建该方法的模板。

I have a custom datagridviewcolumn in which i've added an event.
The problem is, I can't work out how to see who has subscribed to the current column object's event and add those subscriptions to the cloned column object.

I get around this problem by asking the calling program to pass the address of the handler to a delegate in the custom column, instead of adding a handler to the event.

Forgive my terminology, I hope you understand what i'm trying to say!

By receiving the reference to the method, the datagridviewcolumn now has control and can then easily clone that reference.

This is fine, but users of controls expect to be able to suscribe to events by selecting the event in visual studio - which creates a template of the method.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

っ左 2024-08-09 02:55:44

至少在 C# 中,您可以为事件提供“加法器”和“移除器”,例如属性的 getter 和 setter。

也许您可以在有人向事件添加事件处理程序的过程中使用它来进行一些自定义处理?

编辑
我对 VB.NET 不太了解,但我用 google 搜索了一下并找到了以下代码片段:

Public Delegate Sub WorkDone(ByVal completedWork As Integer ) 
Private handlers As New ArrayList() 

Public Custom Event WorkCompleted As WorkDone 
  AddHandler (ByVal value As WorkDone)
    If handlers.Count <= 5 Then 
      handlers.Add(value)  
    End If  
  End AddHandler 

  RemoveHandler(ByVal value As WorkDone) 
    handlers.Remove(value)  
  End RemoveHandler 

  RaiseEvent (ByVal completedWork As Integer) 
    If completedWork > 50 Then  
      For Each handler As WorkDone In handlers  
        handler.Invoke(completedWork)  
      Next  
    End If  
  End RaiseEvent  
End Event 

这应该可以帮助您自定义事件处理程序,以便您可以“看到”从类中添加到事件的委托。

At least in C# you can have "adders" and "removers" for events, like getters and setters for properties.

Maybe you can use that to do some custom processing during the process of somebody adding an event handler to the event?

EDIT
I don't know much about VB.NET, but I googled a little and found the following snippet:

Public Delegate Sub WorkDone(ByVal completedWork As Integer ) 
Private handlers As New ArrayList() 

Public Custom Event WorkCompleted As WorkDone 
  AddHandler (ByVal value As WorkDone)
    If handlers.Count <= 5 Then 
      handlers.Add(value)  
    End If  
  End AddHandler 

  RemoveHandler(ByVal value As WorkDone) 
    handlers.Remove(value)  
  End RemoveHandler 

  RaiseEvent (ByVal completedWork As Integer) 
    If completedWork > 50 Then  
      For Each handler As WorkDone In handlers  
        handler.Invoke(completedWork)  
      Next  
    End If  
  End RaiseEvent  
End Event 

This should help you customize your event handler, so that you can "see" the delegates being added to the event from within your class.

故乡的云 2024-08-09 02:55:44

对于任何感兴趣的人,我偶然发现了一种无需自定义事件即可执行此操作的方法。

通过在事件名称后添加 Event,您可以访问某些属性和方法。
因此,在 Thorsten 的示例中,我将该事件称为 WorkCompletedEvent

具体到这个问题,有一个方法GetInitationList,它返回附加到事件的委托列表。

除此之外,检查 WorkCompletedEvent IsNot Nothing 会告诉您是否有该事件的处理程序,而无需检索调用列表。

For anyone interested, I chanced upon a way to perform this without the need for a custom event.

By suffixing the event name with Event, you have access to some properties and methods.
So in Thorsten's example, I would refer to the event as WorkCompletedEvent.

Specific to this question, there is the method GetInvocationList that returns a list of delegates attached to the event.

In addition to this, checking if WorkCompletedEvent IsNot Nothing, will tell you whether there are handlers for the event without having to retrieve the invocation list.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文