Is there a way to know in VB.NET if a handler has been registered for an event?

前端 未结 6 1236
难免孤独
难免孤独 2021-01-17 14:07

In C# I can test for this...

public event EventHandler Trigger;
protected void OnTrigger(EventArgs e)
{
    if (Trigger != null)
        Trigger(this, e);
}
         


        
6条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-17 14:23

    Yes. null is called "Nothing" in Visual Basic.

    If Trigger IsNot Nothing Then
    

    Update

    The above answer describes how to check something for null in VB .NET. Unfortunately, events are handled special by the VB.NET compiler.

    For this event definition:

    Public Event Trigger as EventHandler
    

    You would use this code to check for subscriptions

    If TriggerEvent Is Nothing
    

    Notice how VB.Net added a field with the suffix Event to represent the delegate. Have a look here for an explanation.

提交回复
热议问题