如何在派生类中隔离事件生成代码? [英] How do I isolate event-generating code in a derived class?

查看:63
本文介绍了如何在派生类中隔离事件生成代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿所有,


当我的缓冲区不是空的时候,我的一类需要告诉外面的世界。问题是C#似乎迫使你把

事件提升代码放在基类中。为了说明,考虑一下我用Java做什么



公共接口DataAvailabilityListener扩展java.util.EventListener {

void dataArrived(DataAvailabilityEvent event);

}


然后,在我的基类中,我可以这样做:


公共抽象类SmartQueue {

addDataAvailabilityListener(DataAvailabilityListen er listener);

}


然后在我的实现类中,说一个内存支持的智能队列,我可以这样做:
这样做:

公共类MemorySmartQueue实现SmartQueue {

public void poolForData (){

//数据可用!

while(iter.hasNext()){

DataAvailabilityListener lis =

(DataAvailabilityListener)iter.next();

lis.dataArrived(someEvent);

}

}

}


简单而直接。但是,考虑一下C#实现:在

界面中,我可能有这样的东西:


公共抽象类SmartQueue {

公共事件DataAvailabilityEventHandler DataArrived;

}


现在,考虑一下我在MemorySmartQueue中要做的事情:


公共类MemorySmartQueue:SmartQueue {

public override void poolForData(){

//数据可用!

if(DataArrived == null){

// BZZZZTTT !!!只能在SmartQueue中执行此操作!

DataArrived(this,args);

}

}

}


我不能相信这一点。要么我错过了一个非常明显的事情,要么我必须处理这个......这......尴尬的机制。为什么地狱

难道它不允许我在派生类中举起一个事件?我不希望

将任何行为放在我的抽象类中,我想只放一个界面!


有什么方法可以解决这个问题吗? (另外我希望惠德贝会给我们一套

的收藏,该死的)。


TIA!

长老

解决方案

糟糕,请替换implements与延伸一起:)


海德长老写道:

嘿所有,

我的一类需要告诉外面世界的时候,它的缓冲区不是空的。问题是C#似乎迫使你把
事件提升代码放在基类中。为了说明,请考虑我将用Java做什么:

公共接口DataAvailabilityListener扩展java.util.EventListener {
void dataArrived(DataAvailabilityEvent event);
那么,在我的基类中,我可以这样做:

公共抽象类SmartQueue {
addDataAvailabilityListener(DataAvailabilityListen er listener);
}

然后在我的实现类中,说一个内存支持的智能队列,我可以这样做:

公共类MemorySmartQueue实现SmartQueue {
public void poolForData(){
//数据可用!
while(iter.hasNext()){
DataAvailabilityListener lis =
(DataAvailabilityListener)iter.next();
lis.dataArrived(someEvent);
}
}
}

简单而直接。但是,考虑一下C#实现:在
界面中,我可能会有这样的事情:

公共抽象类SmartQueue {
公共事件DataAvailabilityEventHandler DataArrived;
}

现在,考虑一下我在MemorySmartQueue中要做的事情:

公共类MemorySmartQueue:SmartQueue {
public override void poolForData(){
// data可用!
if(DataArrived == null){
// BZZZZTTT !!!只能在SmartQueue中执行此操作!
DataArrived(this,args);
}
}
}

我不能相信这一点。要么我错过了一个非常明显的事情,要么我必须处理这个...这......尴尬的机制。为什么地狱
不允许我在派生类中举起一个事件?我不想在我的抽象类中放置任何行为,我只想放一个界面!

有什么方法可以解决这个问题吗? (另外我希望Whidbey会给我们一个Set
集合,该死的)。

TIA!
Elder






实现触发事件的方法是标准的,这个方法是以''On'为前缀的

事件名称。所以在你的情况下,基类将有一个

OnDataArrived,可以从任何地方调用。这样做的好处是

无需检查你的代码,如果事件在发射前有任何代表

,也可以为派生实现提供替代方案和

通过覆盖On ...

方法更好地处理事件的方法。


公共抽象类SmartQueue

{

公共事件DataAvailabilityEventHandler DataArrived;


//调用此事件来激活事件

public void OnDataArrived(YourEventArgs e )

{

if(DataArrived!= null)

DataArrived(this,e);

} < br $>
}


公共类MemorySmartQueue:SmartQueue

{

public override void poolForData()

{

//可用数据!

OnDataArrived(args); //开火活动

}

}

希望这会有所帮助

-

Chris Taylor
http://dotnetjunkies.com/WebLog/chris .taylor /

海德长老 < no_way>在消息中写道

新闻:%2 **************** @ TK2MSFTNGP12.phx.gbl ...

嘿所有,

当我的缓冲区没有空时,我的一类需要告诉外面的世界。问题是C#似乎迫使你把
事件提升代码放在基类中。为了说明,请考虑我将用Java做什么:

公共接口DataAvailabilityListener扩展java.util.EventListener
{void dataArrived(DataAvailabilityEvent event);
那么,在我的基类中,我可以这样做:

公共抽象类SmartQueue {
addDataAvailabilityListener(DataAvailabilityListen er listener);
}

然后在我的实现类中,说一个内存支持的智能队列,我可以这样做:

公共类MemorySmartQueue实现SmartQueue {
public void poolForData(){
//数据可用!
while(iter.hasNext()){
DataAvailabilityListener lis =
(DataAvailabilityListener)iter.next();
lis.dataArrived(someEvent);
}
}
}

简单而直接。但是,考虑一下C#实现:在
界面中,我可能会有这样的事情:

公共抽象类SmartQueue {
公共事件DataAvailabilityEventHandler DataArrived;
}

现在,考虑一下我在MemorySmartQueue中要做的事情:

公共类MemorySmartQueue:SmartQueue {
public override void poolForData(){
// data可用!
if(DataArrived == null){
// BZZZZTTT !!!只能在SmartQueue中执行此操作!
DataArrived(this,args);
}
}
}

我不能相信这一点。要么我错过了一个非常明显的事情,要么我必须处理这个...这......尴尬的机制。为什么地狱
不允许我在派生类中举起一个事件?我不想在我的抽象类中放置任何行为,我想只放一个
的界面!
有什么方法可以解决这个问题吗? (另外我希望Whidbey会给我们一个Set
集合,该死的)。

TIA!
Elder



我们如何在C#中执行Async事件?


事件通常是同步的。我们只是打电话给BeginInvoke吗,如果是这样的话,我们会怎么做?b $ b" Chris Taylor" < CH ************* @ hotmail.com>在消息中写道

新闻:#G ************** @ TK2MSFTNGP10.phx.gbl ...



实现触发事件的方法是标准的,此方法是以On为前缀的事件名称。所以在你的情况下,基类会有一个OnDataArrived,可以从任何地方调用。这样做的优点是
不需要检查你的代码,如果事件在发布之前有任何代表,并且还为派生的实现提供了一种替代的,并且通过覆盖更好地处理事件的方法On ...
方法。

公共抽象类SmartQueue
公共事件DataAvailabilityEventHandler DataArrived;

//这叫做触发事件
public void OnDataArrived(YourEventArgs e)
{
if(DataArrived!= null)
DataArrived(this,e);
}
}

公共类MemorySmartQueue:SmartQueue
{
公共覆盖void poolForData()
//数据可用!
OnDataArrived( args); //解雇活动
}
}

希望这会有所帮助
-
Chris Taylor
http://dotnetjunkies.com/WebLog/chris.taylor/
海德长老 < no_way>在消息中写道
新闻:%2 **************** @ TK2MSFTNGP12.phx.gbl ...

嘿所有,
当我的缓冲区没有空时,我的一类需要告诉外面的世界。问题是C#似乎迫使你把
事件提升代码放在基类中。为了说明,请考虑我将用Java做什么:

公共接口DataAvailabilityListener extends


java.util.EventListener {

void dataArrived(DataAvailabilityEvent event);
}
然后,在我的基类中,我可以这样做:

公共抽象类SmartQueue {
addDataAvailabilityListener(DataAvailabilityListen er listener);
}
然后在我的实现类中,说一个内存支持的智能队列,我可以这样做:
公共类MemorySmartQueue实现SmartQueue {
public void poolForData(){
//数据可用!
while(iter.hasNext()){
DataAvailabilityListener lis =
(DataAvailabilityListener)iter.next();
lis.dataArrived(someEvent);
}
}
}

简单而直接。但是,考虑一下C#实现:在
界面中,我可能会有这样的事情:

公共抽象类SmartQueue {
公共事件DataAvailabilityEventHandler DataArrived;
}

现在,考虑一下我在MemorySmartQueue中要做的事情:

公共类MemorySmartQueue:SmartQueue {
public override void poolForData(){
// data可用!
if(DataArrived == null){
// BZZZZTTT !!!只能在SmartQueue中执行此操作!
DataArrived(this,args);
}
}
}

我不能相信这一点。要么我错过了一个非常明显的事情,要么我必须处理这个...这......尴尬的机制。为什么地狱
不允许我在派生类中举起一个事件?我不想在我的抽象类中放置任何行为,我只想放一个


接口!


有没有绕过这个? (另外,我希望惠德贝会给我们一个集合,这个集合,该死的)。

TIA!
Elder




Hey all,

A class of mine needs to tell the outside world when its buffer is not
empty. The problem is that C# seems to force you to put the
event-raising code in the base class. To illustrate, consider what I''ll
do in Java:

public interface DataAvailabilityListener extends java.util.EventListener {
void dataArrived(DataAvailabilityEvent event);
}

then, in my base class, I can do this:

public abstract class SmartQueue {
addDataAvailabilityListener(DataAvailabilityListen er listener);
}

then in my implementation class, say a memory-backed smart queue, I can
do this:

public class MemorySmartQueue implements SmartQueue {
public void poolForData() {
// data available!
while(iter.hasNext()) {
DataAvailabilityListener lis =
(DataAvailabilityListener)iter.next();
lis.dataArrived(someEvent);
}
}
}

Simple and straighforward. However, consider a C# implementation: in the
interface, I may have something like this:

public abstract class SmartQueue {
public event DataAvailabilityEventHandler DataArrived;
}

Now, consider what I have to do in MemorySmartQueue:

public class MemorySmartQueue : SmartQueue {
public override void poolForData() {
// data available!
if(DataArrived == null) {
// BZZZZTTT!!! Can only do this in SmartQueue!
DataArrived(this, args);
}
}
}

I can''t believe this. Either I''m missing a really obvious thing, or I
have to deal with this... this... awkward mechanism. Why the hell
doesn''t it allow me to raise an event in the derived class? I don''t want
to put any behaviour in my abstract class, I want to put just an interface!

Is there any way around this? (Plus I hope Whidbey will give us a Set
collection, dammit).

TIA!
Elder

解决方案

Oops, please replace "implements" with "extends" :)

Elder Hyde wrote:

Hey all,

A class of mine needs to tell the outside world when its buffer is not
empty. The problem is that C# seems to force you to put the
event-raising code in the base class. To illustrate, consider what I''ll
do in Java:

public interface DataAvailabilityListener extends java.util.EventListener {
void dataArrived(DataAvailabilityEvent event);
}

then, in my base class, I can do this:

public abstract class SmartQueue {
addDataAvailabilityListener(DataAvailabilityListen er listener);
}

then in my implementation class, say a memory-backed smart queue, I can
do this:

public class MemorySmartQueue implements SmartQueue {
public void poolForData() {
// data available!
while(iter.hasNext()) {
DataAvailabilityListener lis =
(DataAvailabilityListener)iter.next();
lis.dataArrived(someEvent);
}
}
}

Simple and straighforward. However, consider a C# implementation: in the
interface, I may have something like this:

public abstract class SmartQueue {
public event DataAvailabilityEventHandler DataArrived;
}

Now, consider what I have to do in MemorySmartQueue:

public class MemorySmartQueue : SmartQueue {
public override void poolForData() {
// data available!
if(DataArrived == null) {
// BZZZZTTT!!! Can only do this in SmartQueue!
DataArrived(this, args);
}
}
}

I can''t believe this. Either I''m missing a really obvious thing, or I
have to deal with this... this... awkward mechanism. Why the hell
doesn''t it allow me to raise an event in the derived class? I don''t want
to put any behaviour in my abstract class, I want to put just an interface!

Is there any way around this? (Plus I hope Whidbey will give us a Set
collection, dammit).

TIA!
Elder



Hi,

It is standard to implement a method to fire the event, this method is the
event name prefixed with ''On''. So in your case the base class would have a
OnDataArrived, which can be called from anywhere. This has the advantage of
not having to cluter your code checking if the event has any delegates
before firing and also gives the derived implementation an alternative and
better performing method of handling the event by overriding the On...
method.

public abstract class SmartQueue
{
public event DataAvailabilityEventHandler DataArrived;

// This is called to fire the event
public void OnDataArrived( YourEventArgs e )
{
if ( DataArrived != null )
DataArrived( this, e );
}
}

public class MemorySmartQueue : SmartQueue
{
public override void poolForData()
{
// data available!
OnDataArrived( args ); // Fire the event
}
}
Hope this helps
--
Chris Taylor
http://dotnetjunkies.com/WebLog/chris.taylor/
"Elder Hyde" <no_way> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...

Hey all,

A class of mine needs to tell the outside world when its buffer is not
empty. The problem is that C# seems to force you to put the
event-raising code in the base class. To illustrate, consider what I''ll
do in Java:

public interface DataAvailabilityListener extends java.util.EventListener { void dataArrived(DataAvailabilityEvent event);
}

then, in my base class, I can do this:

public abstract class SmartQueue {
addDataAvailabilityListener(DataAvailabilityListen er listener);
}

then in my implementation class, say a memory-backed smart queue, I can
do this:

public class MemorySmartQueue implements SmartQueue {
public void poolForData() {
// data available!
while(iter.hasNext()) {
DataAvailabilityListener lis =
(DataAvailabilityListener)iter.next();
lis.dataArrived(someEvent);
}
}
}

Simple and straighforward. However, consider a C# implementation: in the
interface, I may have something like this:

public abstract class SmartQueue {
public event DataAvailabilityEventHandler DataArrived;
}

Now, consider what I have to do in MemorySmartQueue:

public class MemorySmartQueue : SmartQueue {
public override void poolForData() {
// data available!
if(DataArrived == null) {
// BZZZZTTT!!! Can only do this in SmartQueue!
DataArrived(this, args);
}
}
}

I can''t believe this. Either I''m missing a really obvious thing, or I
have to deal with this... this... awkward mechanism. Why the hell
doesn''t it allow me to raise an event in the derived class? I don''t want
to put any behaviour in my abstract class, I want to put just an interface!
Is there any way around this? (Plus I hope Whidbey will give us a Set
collection, dammit).

TIA!
Elder



How can we perform an Async event in C#?

Events are Synchronous usually. Do we just call BeginInvoke, if so, how
would we do this?

"Chris Taylor" <ch*************@hotmail.com> wrote in message
news:#G**************@TK2MSFTNGP10.phx.gbl...

Hi,

It is standard to implement a method to fire the event, this method is the
event name prefixed with ''On''. So in your case the base class would have a
OnDataArrived, which can be called from anywhere. This has the advantage of not having to cluter your code checking if the event has any delegates
before firing and also gives the derived implementation an alternative and
better performing method of handling the event by overriding the On...
method.

public abstract class SmartQueue
{
public event DataAvailabilityEventHandler DataArrived;

// This is called to fire the event
public void OnDataArrived( YourEventArgs e )
{
if ( DataArrived != null )
DataArrived( this, e );
}
}

public class MemorySmartQueue : SmartQueue
{
public override void poolForData()
{
// data available!
OnDataArrived( args ); // Fire the event
}
}
Hope this helps
--
Chris Taylor
http://dotnetjunkies.com/WebLog/chris.taylor/
"Elder Hyde" <no_way> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...

Hey all,

A class of mine needs to tell the outside world when its buffer is not
empty. The problem is that C# seems to force you to put the
event-raising code in the base class. To illustrate, consider what I''ll
do in Java:

public interface DataAvailabilityListener extends

java.util.EventListener {

void dataArrived(DataAvailabilityEvent event);
}

then, in my base class, I can do this:

public abstract class SmartQueue {
addDataAvailabilityListener(DataAvailabilityListen er listener);
}

then in my implementation class, say a memory-backed smart queue, I can
do this:

public class MemorySmartQueue implements SmartQueue {
public void poolForData() {
// data available!
while(iter.hasNext()) {
DataAvailabilityListener lis =
(DataAvailabilityListener)iter.next();
lis.dataArrived(someEvent);
}
}
}

Simple and straighforward. However, consider a C# implementation: in the
interface, I may have something like this:

public abstract class SmartQueue {
public event DataAvailabilityEventHandler DataArrived;
}

Now, consider what I have to do in MemorySmartQueue:

public class MemorySmartQueue : SmartQueue {
public override void poolForData() {
// data available!
if(DataArrived == null) {
// BZZZZTTT!!! Can only do this in SmartQueue!
DataArrived(this, args);
}
}
}

I can''t believe this. Either I''m missing a really obvious thing, or I
have to deal with this... this... awkward mechanism. Why the hell
doesn''t it allow me to raise an event in the derived class? I don''t want
to put any behaviour in my abstract class, I want to put just an


interface!


Is there any way around this? (Plus I hope Whidbey will give us a Set
collection, dammit).

TIA!
Elder




这篇关于如何在派生类中隔离事件生成代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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