通过匿名委托取消活动 [英] Event unsubscription via anonymous delegate

查看:60
本文介绍了通过匿名委托取消活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我多次使用Resharper 5.1代码分析,而从Resharper收到的评论为

I am using Resharper 5.1 code analysis many a times i get a comment from resharper as

通过匿名委托取消活动订阅"

#Part of Code  

if (((bool)e.NewValue))
{
    listView.PreviewTextInput += (o,args) =>
        listView_PreviewTextInput(o,args,listView);
}
else
{
    listView.PreviewTextInput -= (o, args) => 
        listView_PreviewTextInput(o, args, listView);
}

我该如何纠正或优化这个东西

How could i correct or optimze this thing

推荐答案

您可以将lamdba提取为变量:

You can extract the lamdba to a variable:

EventHandler func = (sender, e) =>
    listView_PreviewTextInput(sender, e, listView);

if (((bool)e.NewValue))
{
    listView.PreviewTextInput += func;
}
else
{
    listView.PreviewTextInput -= func;
}

这篇关于通过匿名委托取消活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆