How do I unsubscribe all handlers from an event for a particular class in C#?

后端 未结 5 1607
滥情空心
滥情空心 2020-12-08 07:04

Basic premise:

I have a Room which publishes an event when an Avatar \"enters\" to all Avatars within the Room. When an Avatar leaves the Room I want it to remove a

5条回答
  •  渐次进展
    2020-12-08 07:31

    you can run on all the event subscribers with:

    _Event.GetInvocationList()
    

    and remove each event handler.

    Delegate[] subscribers = myEvent.GetInvocationList();
    
    for(int i = 0; i < subscribers.Length; i++)    
    {    
        myEvent -= subscribers[i] as yourDelegateType;   
    }
    

提交回复
热议问题