I have a static class, which exposes a event:
public static class MyStaticClass
{
static bool myBool= false;
public static bool MyBool
{
get { return myBool; }
private set
{
myBool= value;
var handler = MyBoolChanged;
if (handler != null)
handler(null, null);
}
}
public static event EventHandler MyBoolChanged;
}
And then I am registering to it using this pattern:
class AnotherClass
{
WeakReference _me;
public MyMethodInAnotherClass()
{
_me = new WeakReference(this);
MyStaticClass.MyBoolChanged+=
(_me.Target as AnotherClass).MyMethodInAnotherClassCallback;
}
private void MyMethodInAnotherClassCallback(some arguments)
{
}
}
What I want to achieve is that MyStaticClass will only execute the handler if the instance of AnotherClass has not been disposed (and has not deregistered).
The best way I can see to use this is to forget about an event, and use some kind of list instead; let’s say
List<WeakReference>; you could then have:with:
with additional mechanisms to remove a specific IFoo, and to remove all dead foos left todo.
Then you just need
AnotherClass : IFoo, with aBar()implementation that applies your callback.Additional emphasis: static collections (including events) are fairly dangerous; you must have some kind of sweep occasionally to remove empty items, and try to unsubscribe promptly where possible (in
Dispose(), for example). As an illustration: