检查是否已附加特定的事件处理程序方法

Check if a specific event handler method already attached
2021-11-25
  •  译文(汉语)
  •  原文(英语)

与此问题相关, 检查事件是否已存在

但不同的是我只想知道事件是否附加了特定的方法.所以可能附加了其他方法,但我只想知道是否存在特定的方法.

我的环境是 dotnet 4.0 中的 C#.

例如

Event += MyMethod1;
Event += MyMethod2;

// Some code
if (MyMethod1IsAttachedToEvent())
{
    // Achieved goal
}

这可能吗?

解决过程1

你不能.

事件关键字是明确的发明,以防止你做你想做的事.它使事件的委托对象无法访问,因此没有人可以弄乱事件处理程序.

来源:如何确定已订阅事件

解决过程2
foreach ( Delegate existingHandler in this.EventHandler.GetInvocationList() )
{
    if ( existingHandler == prospectiveHandler )
    {
          return true;
    }
}

使用该GetInvocationList方法循环遍历委托.

解决过程3
Event.GetInvocationList().Any(x => x.Method.Name.Equals("yourmethodname"));
解决过程4

迟到的答案在这里.我相信 Parimal Raj 的答案是正确的,因为我找不到直接访问事件的方法.但是,我创建了两种方法来解决这个问题:

  1. 添加前删除.如果该方法不存在,我在尝试删除不存在的方法时不会收到错误消息.这样您就可以确保调用列表只调用 method1 一次.

    Event -= MyMethod1;
    Event += MyMethod1;
    
  2. 您要向其添加事件的对象可能具有 .Tag 属性.您可以使用标签来存储有关您已添加的方法的信息.这里我只有一种方法,所以我只需要检查Tag是否为空.但是可以通过这种方式处理更复杂的场景:

    if(control.Tag == null)
    {
         //ony added once, when tag is null
         control.TextChanged += new EventHandler(validate); 
         control.Tag = new ControlTag();
    }
    

Related to this question, Check if an event already exists

but the difference is I just want to know if a particular method is attached to the event. So there may be other methods attached, but I just want to know if a particular one exists.

My environment is C# in dotnet 4.0.

E.g.

Event += MyMethod1;
Event += MyMethod2;

// Some code
if (MyMethod1IsAttachedToEvent())
{
    // Achieved goal
}

Is this possible?

Solutions1

No. You cannot.

The event keyword was explicitly invented to prevent you from doing what you want to do. It makes the delegate object for the event inaccessible so nobody can mess with the events handlers.

Source : How to dermine if an event is already subscribed

Solutions2
foreach ( Delegate existingHandler in this.EventHandler.GetInvocationList() )
{
    if ( existingHandler == prospectiveHandler )
    {
          return true;
    }
}

loop through the delegates using the GetInvocationList method.

Solutions3
Event.GetInvocationList().Any(x => x.Method.Name.Equals("yourmethodname"));
Solutions4

Late answer here. I believe Parimal Raj answer is correct, as I could not find a way to directly access the events. However, here are two methods I created to get around this:

  1. Delete before adding. If the method isn't there, I did not receive an error trying to delete the nonexistant method. This way you can insure the invocation list calls method1 only once.

    Event -= MyMethod1;
    Event += MyMethod1;
    
  2. The objects you are adding an event to may have a .Tag property. You can use the Tag to store info about the methods you already added. Here I only have one method, so I just need to check if Tag is null. But more complicated scenarios can be handled this way:

    if(control.Tag == null)
    {
         //ony added once, when tag is null
         control.TextChanged += new EventHandler(validate); 
         control.Tag = new ControlTag();
    }
    
转载于:https://stackoverflow.com/questions/15238701/check-if-a-specific-event-handler-method-already-attached

本人是.net程序员,因为英语不行,使用工具翻译,希望对有需要的人有所帮助
如果本文质量不好,还请谅解,毕竟这些操作还是比较费时的,英语较好的可以看原文

留言回复
我们只提供高质量资源,素材,源码,坚持 下了就能用 原则,让客户花了钱觉得值
上班时间 : 周一至周五9:00-17:30 期待您的加入