访问“自我” 闭包中的对象
我遇到以下问题:(c#)
有一些类(IRC bot),它有方法,需要某些事件的结果才能完成(通过它可以是异步的)。
也许不清楚:
// simplified
class IRC
{
void DoSomeCommand()
{
OnListOfPeopleEvent += new Delegate(EventData e) {
if (e.IsForMe)
{
ReturnToUserSomeData();
// THIS IS WHAT I NEED
OnListOfPeopleEvent -= THIS DELEGATE;
}
}
TakeListOfPeopleFromIrc();
}
}
我想在功能完成时删除该委托。 有什么方法可以获取对闭包本身的引用吗?
I've got following problem: (c#)
There is some class (IRC bot), which has method, which needs result of some event for complete (through it could be asynchronous).
Maybe not clear:
// simplified
class IRC
{
void DoSomeCommand()
{
OnListOfPeopleEvent += new Delegate(EventData e) {
if (e.IsForMe)
{
ReturnToUserSomeData();
// THIS IS WHAT I NEED
OnListOfPeopleEvent -= THIS DELEGATE;
}
}
TakeListOfPeopleFromIrc();
}
}
And I want to delete that delegate when it the function is complete. Is there any way how to obtain the reference to closure in it itself?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用捕获自身的 cheaky 变量来做到这一点;-p
第一行带有
null
需要满足明确的赋值; 但您可以在匿名方法中捕获此变量。You can do this with a cheaky variable that captures itself ;-p
The first line with
null
is required to satisfy definite assignment; but you are allowed to capture this variable inside the anon-method.