それ自体でラムダからサブスクライブを解除しようとしました。MethodInfo クラスを使用してラムダに関する情報を取得し、Delegate.CreateDelegate メソッドを使用してラムダと同じメソッドを作成しました。そのため、使用するイベントを含むクラス メソッドの 1 つで作成されたラムダが、別のクラス メソッド (バインディング例外) では機能しない場合は正常に機能します。
コードは次のとおりです。
public class TestClass
{
public event Action SomeEvent;
public void OnSomeEvent()
{
if (SomeEvent != null)
{
SomeEvent();
}
}
public TestClass()
{
//IT WORKS FINE!!!
//SomeEvent += () =>
//{
// Console.WriteLine("OneShotLambda");
// MethodInfo methodInfo = MethodInfo.GetCurrentMethod() as MethodInfo;
// Action action = (Action)Delegate.CreateDelegate(typeof(Action), this, methodInfo);
// SomeEvent -= action;
//};
}
}
class Program
{
static void Main(string[] args)
{
TestClass t = new TestClass();
t.SomeEvent += () =>
{
Console.WriteLine("OneShotLambda");
MethodInfo methodInfo = MethodInfo.GetCurrentMethod() as MethodInfo;
//GOT AN ERROR
Action action = (Action)Delegate.CreateDelegate(typeof(Action), t, methodInfo);
t.SomeEvent -= action;
};
t.OnSomeEvent();
t.OnSomeEvent(); //MUST BE NO MESSAGE
}
}