Handle anonymous method handlers in weak events
masterWhen subscribing to a weak event using an anonymous method or a lambda expression, the handler itself might be garbage collected prematurely if no other reference to it exists.
To prevent this, you have two options:
- Maintain a reference: Explicitly store the handler (the lambda/delegate) in a variable so it stays alive.
- Use a lifetime object: Use the
Subscribeoverload that accepts a lifetime object. This ensures the handler remains alive as long as the provided lifetime object is alive. Note that using this method requires the publisher to expose a specific subscription method rather than using standard C#eventsyntax.