1
私はイベントを公開する静的クラス、持っている:WeakReference - 私はそれを正しくしていますか?
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;
}
をそして私はこのパターンを使用して、それに登録しています:
class AnotherClass
{
WeakReference _me;
public MyMethodInAnotherClass()
{
_me = new WeakReference(this);
MyStaticClass.MyBoolChanged+=
(_me.Target as AnotherClass).MyMethodInAnotherClassCallback;
}
private void MyMethodInAnotherClassCallback(some arguments)
{
}
}
私が達成したい何がMyStaticClass
のみが実行されることですAnotherClass
のインスタンスが破棄されていない場合(および登録解除されていない場合)
いいえ、あなたは正しいことをしていません。 '_me.Target'を読んで自分自身への参照を明示的に与えてから、それをイベントサブスクリプションに格納します。 –
ところで、静的イベントはメモリリークの**致命的**です。あなたは**あなたが欲しいものです**確かですか? –
@MarcGravellいいえ、私は – UrbanEsc