mercredi 25 août 2021

Get GetInvocationList of a public static event via reflection

I have a class which contains public static event, and I need to find out if it has handler attached to it or not. I know that I can use GetInvocationList() but the issue is that the event is static and I cannot seem to get a value by using reflection.

public class Business
{
  public static event EventHandler MyEvent;
}

I need to get event handlers of this event but from another class:

public class MyClass
{
  public void Test()
  {
    FieldInfo fieldInfo = typeof(Business).GetField("MyEvent", BindingFlags.Static | BindingFlags.Public | BindingFlags.FlattenHierarchy);
    // fieldInfo is null!!
    Delegate del = (Delegate)fieldInfo.GetValue(null);
    var list = del.GetInvocationList();
  }
}

I cannot get it to work to have fieldInfo returned with value instead of null!





Aucun commentaire:

Enregistrer un commentaire