什么时候事件为空? [英] When is an event null?

查看:85
本文介绍了什么时候事件为空?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有这个:


class C {

public delegate void MyEventHandler();

公共事件MyEventHandler MyEvent ;


public void foo(){

MyEvent(); // NullReferenceException?

}

}


在什么情况下会尝试在foo中引发事件

方法生成NullReferenceException,为什么?

Suppose I have this:

class C {
public delegate void MyEventHandler();
public event MyEventHandler MyEvent;

public void foo() {
MyEvent(); // NullReferenceException?
}
}

Under what circumstances will trying to raise the event in the foo
method generate a NullReferenceException, and why?

推荐答案

dv ***** @ gmail.com 写道:

假设我有这个:

class C {

public delegate void MyEventHandler();

公共事件MyEventHandler MyEvent;


public void foo(){

MyEvent(); // NullReferenceException?

}

}


在什么情况下会尝试在foo中引发事件

方法生成NullReferenceException,为什么?
Suppose I have this:

class C {
public delegate void MyEventHandler();
public event MyEventHandler MyEvent;

public void foo() {
MyEvent(); // NullReferenceException?
}
}

Under what circumstances will trying to raise the event in the foo
method generate a NullReferenceException, and why?



如果没有人订阅了

事件,你将得到NullReferenceException。

By声明类似场的事件你得到一个事件(基本上是一个添加/删除对的
)和一个委托类型的字段。如果没有人订阅,那么该字段的值

为空。


参见 http://www.pobox.com/~skeet/csharp/events.html 更为丰富< br $>
描述。


Jon

You''ll get a NullReferenceException if no-one has subscribed to the
event.
By declaring a "field-like event" you get an event (which is basically
an add/remove pair) and a field of the delegate type. The field''s value
is null if no-one has subscribed to it.

See http://www.pobox.com/~skeet/csharp/events.html for a rather fuller
description.

Jon


你总是需要检查

如果(MyEvent!= null)


因为没有人订阅时事件为空。


问候,


Bela Istok

< dv ***** @ gmail.com写信息

新闻:11 ***** *****************@m7g2000cwm.googlegro ups.com ...
You always need to check
If(MyEvent != null)

Because an event is null when no one is subscribed.

Regards,

Bela Istok
<dv*****@gmail.comwrote in message
news:11**********************@m7g2000cwm.googlegro ups.com...

假设我有:


class C {

public delegate void MyEventHandler();

公共事件MyEventHandler MyEvent;


public void foo(){

MyEvent(); // NullReferenceException?

}

}


在什么情况下会尝试在foo中引发事件

方法生成NullReferenceException,为什么?
Suppose I have this:

class C {
public delegate void MyEventHandler();
public event MyEventHandler MyEvent;

public void foo() {
MyEvent(); // NullReferenceException?
}
}

Under what circumstances will trying to raise the event in the foo
method generate a NullReferenceException, and why?



假设我有这个:
Suppose I have this:

>

C级{

public delegate void MyEventHandler();

公共事件MyEventHandler MyEvent;

public void foo(){

MyEvent(); // NullReferenceException?

}

}

在什么情况下会尝试在foo中引发事件

方法生成NullReferenceException,为什么?
>
class C {
public delegate void MyEventHandler();
public event MyEventHandler MyEvent;
public void foo() {
MyEvent(); // NullReferenceException?
}
}
Under what circumstances will trying to raise the event in the foo
method generate a NullReferenceException, and why?



在事件处理程序实际添加到事件处理程序之前,事件将为null。并且,

在从中删除最后一个事件处理程序后它将为null。原因

是你发布的代码真的编译成了看起来很像这样的东西:


class C {

{

public delegate void MyEventHandler();

private MyEventHandler _MyEvent;

public void add_MyEvent(MyEventHandler价值)

{

_MyEvent =(MyEventHandler)Delegate.Combine(_MyEvent,value);

}

public void remove_MyEvent(MyEventHandler value)

{

_MyEvent =(MyEventHandler)Delegate.Remove(_MyEvent,value);

}

public void foo(){

MyEvent();

}

}


事件编译为包含您的委托实例的私有字段和

两种方法,用于在委托中添加和删除处理程序。当创建

类时,该字段为空。当一个处理程序添加到事件中时,

将调用Delegate.Combine()并创建您的委托或将处理程序

添加到您的委托(如果已创建)。当一个处理程序从

中删除时,调用Delegate.Remove()并从

中删除处理程序,如果没有,则委托将其设置为null再好的处理程序。


最好的问候,

Dustin Campbell

Developer Express Inc.

The event will be null until an event handler is actually added to it. And,
it will be null after the last event handler is removed from it. The reason
is that the code that you posted really compiles to something that looks
a little more like this:

class C {
{
public delegate void MyEventHandler();
private MyEventHandler _MyEvent;
public void add_MyEvent(MyEventHandler value)
{
_MyEvent = (MyEventHandler)Delegate.Combine(_MyEvent, value);
}
public void remove_MyEvent(MyEventHandler value)
{
_MyEvent = (MyEventHandler)Delegate.Remove(_MyEvent, value);
}
public void foo() {
MyEvent();
}
}

An event compiles to a private field that holds your delegate instance and
two methods that add and remove handlers to and from the delegate. When the
class is created, the field is null. When a handler is added to the event,
Delegate.Combine() is called and that creates your delegate or adds the handler
to your delegate if it is already created. When a handler is removed from
the event, Delegate.Remove() is called and that removes the handler from
your delegate sets it to null if there aren''t anymore handlers.

Best Regards,
Dustin Campbell
Developer Express Inc.


这篇关于什么时候事件为空?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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