这是事件的错误并添加/删除? [英] Is this a bug with events and add/remove?

查看:54
本文介绍了这是事件的错误并添加/删除?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天早上我遇到了一个问题,因为出现了事件访问器方法

是C#中的一个错误,我想知道这是否是一个已知问题。


公共事件SomeDelegateSignature fooEvent;

公共事件SomeDelegateSignature foo2Event;

{

add

{// some这里的代码}

删除

{//这里有些代码}

}


public void DoSomething()

{

fooEvent.GetInvocationList(); //这一行编译

foo2Event.GetInvocationList(); //这行不编译

}


fooEvent编译得很好,但fooEvent2生成编译器错误

错误CS0079:事件''foo2Event''只能出现在

+ =或 - =


的左侧。这两个事件的唯一区别在于foo2我手动

实现添加/删除访问器。我相信这曾经工作过v1.1。是否

在2.0下不再支持?

I ran into a problem this morning with event accessor methods that appears
to be a bug in C# and I am wondering if this a known issue.

public event SomeDelegateSignature fooEvent;
public event SomeDelegateSignature foo2Event;
{
add
{ // some code here }
remove
{ // some code here }
}

public void DoSomething ()
{
fooEvent.GetInvocationList(); // this line compiles
foo2Event.GetInvocationList(); // this line does not compile
}

The fooEvent compiles just fine but fooEvent2 generates compiler errors
error CS0079: The event ''foo2Event'' can only appear on the left hand side of
+= or -=

The only difference between the two events is that with foo2 I manually
implement the add/remove accessors. I believe this used to work v1.1. Is
this no longer supported under 2.0?

推荐答案

" David Levine" <锡********** @ wi.rr.com>在消息中写道

新闻:Ox **************** @ TK2MSFTNGP02.phx.gbl ...
"David Levine" <Sn*************************@wi.rr.com> wrote in message
news:Ox****************@TK2MSFTNGP02.phx.gbl...
我遇到了今天早上有一个问题,事件访问器方法似乎是C#中的一个错误,我想知道这是否是一个已知问题。

公共事件SomeDelegateSignature fooEvent;
公共事件SomeDelegateSignature foo2Event;


我假设;在上一行的末尾只是一个错字?

{
添加
{//这里的一些代码}
删除
{//一些代码在这里}

公共无效DoSomething()
{f /> fooEvent.GetInvocationList(); //此行编译
foo2Event.GetInvocationList(); //此行无法编译


fooEvent编译得很好,但fooEvent2生成编译错误
错误CS0079:事件''foo2Event''只能出现在左侧手边
+ =或 - =

这两个事件的唯一区别是用foo2我手动实现添加/删除访问器。我相信这曾经工作过v1.1。是不是在2.0以下不支持这个?
I ran into a problem this morning with event accessor methods that appears
to be a bug in C# and I am wondering if this a known issue.

public event SomeDelegateSignature fooEvent;
public event SomeDelegateSignature foo2Event;
I assume the ";" at the end of the previous line is just a typo?
{
add
{ // some code here }
remove
{ // some code here }
}

public void DoSomething ()
{
fooEvent.GetInvocationList(); // this line compiles
foo2Event.GetInvocationList(); // this line does not compile
}

The fooEvent compiles just fine but fooEvent2 generates compiler errors
error CS0079: The event ''foo2Event'' can only appear on the left hand side
of += or -=

The only difference between the two events is that with foo2 I manually
implement the add/remove accessors. I believe this used to work v1.1. Is
this no longer supported under 2.0?




我不知道v1.1,但肯定关于添加/删除访问器

是你自己管理调用列表,因此没有任何方式GetInvocationList实际上可以返回任何有用的东西。

因此它似乎明智不允许它被召唤。


Chris Jobson



I don''t know about v1.1, but surely the point about the add/remove accessors
is that you are managing the invocation list yourself and therefore there''s
no way that GetInvocationList could actually return anything useful.
Therefore it seems sensible not to allow it to be called.

Chris Jobson


David Levine< Sn ****** *******************@wi.rr.com>写道:
David Levine <Sn*************************@wi.rr.com> wrote:
我今天早上遇到了一个问题,因为事件访问器方法似乎是C#中的一个错误,我想知道这是否是一个已知问题。


这不是C#中的错误。你对事件的理解是错误的

声明。

公共事件SomeDelegateSignature fooEvent;


这是一个类似字段的字段。事件声明。它有效地宣布

a公共活动和私人代表字段来支持活动。

公共活动SomeDelegateSignature foo2Event;
{
添加 {//这里有一些代码}
删除
{//这里有些代码}
}


这只* *声明事件。

public void DoSomething()
{
fooEvent.GetInvocationList(); //此行编译


访问委托 - 因为GetInvocationList()是委托

方法。

foo2Event.GetInvocationList (); //这行不编译


的确,因为你不能在实际事件上调用GetInvocationList()。

你只能调用add /删除,基本上(至少来自C#)。

}

fooEvent编译得很好,但fooEvent2生成编译错误
错误CS0079:事件''foo2Event' '只能出现在
的左侧+ =或 - =

这两个事件的唯一区别是用foo2我手动实现添加/删除访问器。我相信这曾经工作过v1.1。是不是在2.0以下不支持这个?
I ran into a problem this morning with event accessor methods that appears
to be a bug in C# and I am wondering if this a known issue.
It''s not a bug in C#. It''s a fault in your understanding of event
declaration.
public event SomeDelegateSignature fooEvent;
This is a "field-like" event declaration. It effectively declares both
a public event and a private delegate field to back the event.
public event SomeDelegateSignature foo2Event;
{
add
{ // some code here }
remove
{ // some code here }
}
This *only* declares the event.
public void DoSomething ()
{
fooEvent.GetInvocationList(); // this line compiles
That accesses the delegate - because GetInvocationList() is a delegate
method.
foo2Event.GetInvocationList(); // this line does not compile
Indeed, because you can''t call GetInvocationList() on an actual event.
You can only call add/remove, basically (from C# at least).
}

The fooEvent compiles just fine but fooEvent2 generates compiler errors
error CS0079: The event ''foo2Event'' can only appear on the left hand side of
+= or -=

The only difference between the two events is that with foo2 I manually
implement the add/remove accessors. I believe this used to work v1.1. Is
this no longer supported under 2.0?




不,它也不会在1.1中工作。


-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet 博客: http://www.msmvps.com/jon.skeet

如果回复小组,请不要给我发邮件



No, it wouldn''t have worked in 1.1 either.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too




" Jon Skeet [C#MVP]" < SK *** @ pobox.com>在消息中写道

新闻:MP ************************ @ msnews.microsoft.c om ...

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
David Levine< Sn ************************* @ wi.rr.com>写道:
David Levine <Sn*************************@wi.rr.com> wrote:
今天早上我遇到了一个问题,其中的事件访问器方法似乎是C#中的一个错误,我想知道这是否是一个已知问题。
这不是C#中的错误。你对事件
声明的理解是错误的。
I ran into a problem this morning with event accessor methods that
appears
to be a bug in C# and I am wondering if this a known issue.
It''s not a bug in C#. It''s a fault in your understanding of event
declaration.




现在太明显了: - )......那个''为什么我在

主题行中加上一个问号。我从来没有真正仔细看过events.before只要

就做了我想做的一切。



That''s now all too obvious :-)...and that''s why I put a question mark in the
subject line. I never really took a close look at events.before so long as
it did everything I wanted it to do.

公共事件SomeDelegateSignature fooEvent;
public event SomeDelegateSignature fooEvent;



这是一个类似字段的字段。事件声明。它有效地声明了一个公共事件和一个私有委托字段来支持事件。



This is a "field-like" event declaration. It effectively declares both
a public event and a private delegate field to back the event.





public事件SomeDelegateSignature foo2Event;
{
添加
{//这里的一些代码}
删除
{//这里的一些代码}
}
public event SomeDelegateSignature foo2Event;
{
add
{ // some code here }
remove
{ // some code here }
}



这* * *声明事件。



This *only* declares the event.




有两种语法吗?两者都声明了

事件的字段委托和带有添加/删除访问器的公共事件?或者我强行要做

就像将存取方法的值保存到明确声明的

私有委托字段中一样,这样......


private SomeDelegateSignature _theDelegate;

公共事件SomeDelegateSignature foo2Event;

{

add

{//其他东西在这里

_theDelegate = value;

}

}


TIA



Is there a syntax for doing both? Both declare the field delegate for the
event and the public event with add/remove accessors? Or am I force to do
something like save the accessor method''s value into an explicitly declared
private delegate field, like this...

private SomeDelegateSignature _theDelegate;
public event SomeDelegateSignature foo2Event;
{
add
{ // other stuff here
_theDelegate = value;
}
}

TIA


这篇关于这是事件的错误并添加/删除?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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