鉴于“事件”,我如何获得基础代表? [英] Given an 'event', how do I obtain underlying delegates ?

查看:66
本文介绍了鉴于“事件”,我如何获得基础代表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我的WinForms应用程序中的各种自定义控件在加载时注册

''Application.Idle''事件,并在dispose上取消注册。为了避免内存泄漏,我必须记得在处理时取消注册。


在申请退出之前,我想做一个'断言''向自己保证

,没有我忘记取消注册的情况。我觉得一个事件只不过是一个代表集合。

因此我尝试过:


调试.Assert(Application.Idle.Count == 0);


然而,这并没有编译,并提出了一个问题:给定一个事件

对象,我如何获得底层委托集合?


Dave

Hi,

Various custom controls in my WinForms application register for the
''Application.Idle'' event on load, and unregister on dispose. To avoid memory
leaks, it''s essential that I remember to unregister on dispose.

Before my application exits, I''d like to do an ''assert'' to assure myself
that there are no cases in which I''ve forgotten to unregister. I was under
the impression that an event is nothing more than a collection of delegates.
I therefore tried:

Debug.Assert(Application.Idle.Count==0);

This doesn''t compile, however, and begs the question: Given an event
object, how do I obtain the underlying delegate collection?

Dave

推荐答案

是什么让你觉得你需要这样做吗?


你不是。


-

HTH,

Kevin Spencer

Microsoft MVP

..Net开发人员

你可以带鱼去骑自行车,

但需要很长时间,

且自行车必须*要*改变。


"我们称之为''戴夫'' < GH **** @ englewood.com>在消息中写道

新闻:JY ******************** @ giganews.com ...
What makes you think you need to do that?

You don''t.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
You can lead a fish to a bicycle,
but it takes a very long time,
and the bicycle has to *want* to change.

"The One We Call ''Dave''" <gh****@englewood.com> wrote in message
news:JY********************@giganews.com...
你好,

我的WinForms应用程序中的各种自定义控件在加载时注册
''Application.Idle''事件,并在dispose上取消注册。为了避免内存泄漏,我必须记得在注册时取消注册。

在我的应用程序退出之前,我想做一个'断言'来保证
我自己没有忘记取消注册的情况。我的印象是,一个活动只不过是一个代表的集合。因此我尝试了:

Debug.Assert(Application.Idle.Count == 0);

然而,这并没有编译,并提出了一个问题:给定一个事件
对象,如何获取底层委托集合?

Dave
Hi,

Various custom controls in my WinForms application register for the
''Application.Idle'' event on load, and unregister on dispose. To avoid
memory leaks, it''s essential that I remember to unregister on dispose.

Before my application exits, I''d like to do an ''assert'' to assure
myself that there are no cases in which I''ve forgotten to unregister. I
was under the impression that an event is nothing more than a collection
of delegates. I therefore tried:

Debug.Assert(Application.Idle.Count==0);

This doesn''t compile, however, and begs the question: Given an event
object, how do I obtain the underlying delegate collection?

Dave



我们打电话给我们'' 戴夫 '' " < GH **** @ englewood.com>在消息中写道

新闻:JY ******************** @ giganews.com ...
"The One We Call ''Dave''" <gh****@englewood.com> wrote in message
news:JY********************@giganews.com...
给定一个事件对象,我如何获得底层委托集合?
Given an event object, how do I obtain the underlying delegate collection?




我认为这些内容:


//活动的订阅者数量:

int n = Idle.GetInvocationList()。长度;


- 或者 -


//获取每个订阅者:

System.Delegate [] DelegateList = Idle.GetInvocationList();

foreach(DelegateList中的IdleDelegate委托){

...

}

- Alan



I think something along these lines:

// Number of subscribers to the event:
int n = Idle.GetInvocationList().Length;

- OR -

// Get at each subscriber:
System.Delegate[] DelegateList = Idle.GetInvocationList();
foreach (IdleDelegate Delegate in DelegateList) {
...
}
-- Alan


> 我们称之为''戴夫'' < GH **** @ englewood.com>在消息中写道
> "The One We Call ''Dave''" <gh****@englewood.com> wrote in message
新闻:JY ******************** @ giganews.com ......
news:JY********************@giganews.com...
给定一个事件对象,我如何获得底层代表
集合?
Given an event object, how do I obtain the underlying delegate
collection?


我认为这些内容如下:

//订阅者数量事件:
int n = Idle.GetInvocationList()。长度;
- 或者 -

//获取每个订阅者:
System.Delegate [] DelegateList = Idle.GetInvocationList();
foreach(DelegateList中的IdleDelegate委托){
...
}


I think something along these lines:

// Number of subscribers to the event:
int n = Idle.GetInvocationList().Length;
- OR -

// Get at each subscriber:
System.Delegate[] DelegateList = Idle.GetInvocationList();
foreach (IdleDelegate Delegate in DelegateList) {
...
}




这两个将导致编译器错误事件...只能出现

左侧+ =或 - =" ;;


这是一个有趣的,我现在正在试验它。


克里斯



Both of those will result in compiler error "The event...can only appear
on the left hand side of += or -=";

This is a fun one and I''m experimenting with it right now.

Chris


这篇关于鉴于“事件”,我如何获得基础代表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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