dimanche 12 septembre 2021

How to clear all static event handlers in a class via reflection?

I have STATIC class with lots of static event callbacks. Instead of hard coding a reset method, I want to use reflection to automatically clear all the event subscriptions.


public static void NotifyA(int p1) { notifyA?.Invoke(p1); }
public static event Action<int> notifyA;

public static void NotifyB(float p1) { notifyB?.Invoke(p1); }
public static event Action<float> notifyB;

// and so on...

I am stuck at this stage don't know how to actually clear the events.

    public static void ResetEvents()
    {
        var type = typeof(ClientEvents);

        var events = type.GetEvents(BindingFlags.Static | BindingFlags.Public);

        foreach (var current in events )
        {
            // current.RemoveEventHandler(???);

        }
    }




Aucun commentaire:

Enregistrer un commentaire