代表混淆 [英] delegate confusion

查看:65
本文介绍了代表混淆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好世界。


我想用一个计时器实现一个班级,巫婆告诉我每一个关于它的每一个b
。代码已经有效,但我想改变一件事(或更多)。


< code>

//目前我用构造函数初始化对象:

public myClass(System.Timers.ElapsedEventHandler CallMeBack)

{

系统。 Timers.Timer myTimer = new System.Timers.Timer(1000);

myTimer.Elapsed + = CallMeBack;

myTimer.Enabled = True;

}


//来自外部(其他类)的调用如下:

myClass myObject = new myClasst(new

System.Timers.ElapsedEventHandler(this.AnsweringMa chine));


// with ...

private void AnsweringMachine(object sender,

System.Timers.ElapsedEventArgs e)

{

//这就是我想做的事:

// someString =((myClass)sender).getValue();

}

< / code>


now我的问题(我在代码中已经评论过):

发送者ret urns计时器和elapsedeventargs返回时间。我需要

访问调用myObject,这将非常巧妙地更改

发送者和/或参数。有一个很好的解决方案吗?我已经尝试过从计时器继承
但是失败了,因为代表和事件对我来说真的很好,所以我很困惑``o?o''


i几乎忘记了:我使用了这个实现,因为来自

myClass的轮询将完全独立地工作。但是在外太空中设置一个计时器会更明智吗?而不是myClass?


谈得够多。


感谢您加入思维上限...
$ b $brené

hello world.

i would like to implement a class with a timer, witch informs me every
second about it''s tick. the code works already, but i would like to
change a thing (or more).

<code>
//at the moment i initialize the object with the constructor:
public myClass(System.Timers.ElapsedEventHandler CallMeBack)
{
System.Timers.Timer myTimer = new System.Timers.Timer(1000);
myTimer.Elapsed += CallMeBack;
myTimer.Enabled = True;
}

//the call from outside (other class) looks like:
myClass myObject = new myClasst(new
System.Timers.ElapsedEventHandler(this.AnsweringMa chine));

//with...
private void AnsweringMachine(object sender,
System.Timers.ElapsedEventArgs e)
{
//THAT''S WHAT I WOULD LIKE TO DO:
//someString = ((myClass)sender).getValue();
}
</code>

now to my problem (i commented it already in the code):
sender returns the timer and elapsedeventargs returns the time. i need
access to the calling myObject and it would be very neat to change the
sender and/or the arguments. is there a good solution? i''ve tried to
inherit from timer but failed, because delegates and events are really
virgin soil for me, so i get confused ` ` o?o ′ ′

i nearly forgot: i used this implementation, because the polling from
myClass would work completly independent. but would it be wiser to set
a timer in the "outer space" instead of "myClass"?

talked enough.

thanks for putting on the thinking caps...
rené

推荐答案

" ohmmega" < sh **** @ gmx.atwrote in message

news:11 ********************** @ k79g2000hse.googlegr oups.com ...
"ohmmega" <sh****@gmx.atwrote in message
news:11**********************@k79g2000hse.googlegr oups.com...

现在我的问题(我已经在代码中注释了它):

发送者返回计时器和elapsedeventargs返回时间。我需要

访问调用myObject,这将非常巧妙地更改

发送者和/或参数。有一个很好的解决方案吗?
now to my problem (i commented it already in the code):
sender returns the timer and elapsedeventargs returns the time. i need
access to the calling myObject and it would be very neat to change the
sender and/or the arguments. is there a good solution?



一种方法是将计时器回调重定向到你自己的例程,

这个例程可以依次调用原始回调使用您选择的参数

,例如this对于发件人:

class myClass

{

private System.Timers.ElapsedEventHandler theCallBack;


public myClass(System.Timers.ElapsedEventHandler CallMeBack)

{

this.theCallBack = CallMeBack;

System.Timers.Timer myTimer = new System.Timers.Timer(1000);

myTimer.Elapsed + = MyCallback;

myTimer.Enabled = True;

}


private void MyCallback(对象发送者,

System.Timers.ElapsedEventArgs e)

{

theCallBack(this,e);

}


//(这是其余的类,例如Timer的清理代码)

}

One way to do it is to redirect the timer callback into your own routine,
and this routine can in turn call the original callback with the arguments
of your choice, such as "this" for the "sender":
class myClass
{
private System.Timers.ElapsedEventHandler theCallBack;

public myClass(System.Timers.ElapsedEventHandler CallMeBack)
{
this.theCallBack = CallMeBack;
System.Timers.Timer myTimer = new System.Timers.Timer(1000);
myTimer.Elapsed += MyCallback;
myTimer.Enabled = True;
}

private void MyCallback(object sender,
System.Timers.ElapsedEventArgs e)
{
theCallBack(this, e);
}

//(Here goes the rest of class, such as clean-up code for the Timer)
}


ohmmega,


我没有编译器,但我认为

的东西,这应该做你想要的。我们的匿名方法将处理

事件,然后使用myClass作为发送者来激活事件处理程序

而不是myTimer。


public myClass(System.Timers.ElapsedEventHandler CallMeBack)

{

System.Timers.Timer myTimer = new System.Timers.Timer(1000);

myTimer.Elapsed + = delegate(对象发送者,

System.Timers.ElapsedEventArgs e)

{

CallMeBack.Invoke(this ,e);

};

myTimer.Enabled = True;

}


BTW :你需要让myTimer成为一个成员变量,否则它会收集垃圾

收集。


祝你好运,

James

ohmmega,

I don''t have a compiler on me but I think something along the lines of
this should do what you want. Our anonymous method will handle the
event, and then fire the event handler with myClass as the sender
instead of myTimer.

public myClass(System.Timers.ElapsedEventHandler CallMeBack)
{
System.Timers.Timer myTimer = new System.Timers.Timer(1000);
myTimer.Elapsed += delegate(object sender,
System.Timers.ElapsedEventArgs e)
{
CallMeBack.Invoke(this, e);
};
myTimer.Enabled = True;
}

BTW: You need to make myTimer a member variable or it will get garbage
collected.

Good Luck,
James


On Thu,2007年7月26日07:40:19 -0700,ohmmega< sh **** @ gmx.atwrote:
On Thu, 26 Jul 2007 07:40:19 -0700, ohmmega <sh****@gmx.atwrote:

现在我的问题(我已经在代码中注释了它):

发送者返回计时器,而elapsedeventargs返回时间。我需要

访问调用myObject,这将非常巧妙地更改

发送者和/或参数。有一个很好的解决方案吗?我已经尝试从计时器继承
但是失败了,因为代表和事件真的对我来说是个b $ b处女土,所以我感到困惑``o ?? o? '?'
now to my problem (i commented it already in the code):
sender returns the timer and elapsedeventargs returns the time. i need
access to the calling myObject and it would be very neat to change the
sender and/or the arguments. is there a good solution? i''ve tried to
inherit from timer but failed, because delegates and events are really
virgin soil for me, so i get confused ` ` o??o ?′ ?′



Alberto发布了一个很好的解决方案。在他的方法中,初始化计时器的类

基本上使它看起来好像是实现计时器的类

。它在内部封装了定时器本身;在

除了解决你的问题的明显优势之外,它还具有

的优势,即myClass类可以改变

的实现计时器更容易(如果你将处理程序委托更改为
类型为myClass定义的东西,而不是使用

Timers命名空间中的那个:),则更容易。) br />

至少还有其他两种方法可以解决这个问题,

根据你的方式可能适合或不适合你的需求br />
希望它能够工作。


第一个涉及简单地保存myClass实例引用并使用

。例如:


public ClientClass

{

myClass myObject;


void SomeMethod()

{

myObject = new myClass(AnsweringMachine);

}


void AnsweringMachine(对象发送者,ElapsedEventArgs e)

{

someString = myObject.getValue();

}

由于委托包含对用于创建代理的类实例的引用,因此可以引用实例成员,例如

" myObject"字段,来自委托方法。


上面的一个变体是创建一个简单的类,你在其中使用委托方法,以及存储myObject实例。那个

方式你可以为你创建的每个myClass实例创建一个新实例,如果由于某种原因这是可取的。当然,你可以在这个简单的类中包含一些方法来回到ClientClass,如果

你希望能够从那个特定于该实例的东西在

内也是回调。正如您所看到的,如果您愿意,这种技术可以随意获得。 :)


另一种解决问题的方法是使用匿名代表:


public ClientClass

{

void SomeMethod()

{

myClass myObject;

ElapsedEventArgs callback = delegate(object sender,

ElapsedEventArgs e)

{someString = myObject.getValue(); }


myObject = new myClass(回调);

}

}


这利用了局部变量myObject的事实。

已捕获由匿名代表。因此,你可以使用委托代码中的变量

,它将保留你在SomeMethod()方法中设置的值。如果存在

回调方法的唯一目的是处理那个

的情况,这可能是特别合适的,事实上这种情况通常就是这种情况。 br />

Alberto has posted one good solution. In his method, the class
initializing the timer essentially makes it look as though it is the class
implementing the timer. It encapsulates the timer itself internally; in
addition to the obvious advantage of addressing your question, it also has
the advantage that the myClass class can change the implementation of the
timer more easily (and even more easily if you change the handler delegate
type to something that myClass defines rather than using the one in the
Timers namespace :) ).

There are at least two other ways I can think of to address the issue,
which may or may not be appropriate for your needs depending on how you
want it to work.

The first involves simply saving the myClass instance reference and using
that. For example:

public ClientClass
{
myClass myObject;

void SomeMethod()
{
myObject = new myClass(AnsweringMachine);
}

void AnsweringMachine(object sender, ElapsedEventArgs e)
{
someString = myObject.getValue();
}
}

Because the delegate includes a reference to the class instance used to
create the delegate, you can then refer to instance members, such as the
"myObject" field, from within the delegate method.

A variation on the above would be to create a simple class in which you
put the delegate method, and which stores the "myObject" instance. That
way you could create a new instance for each instance of myClass that you
create, if that was desirable for some reason. Of course, you would have
to include in that simple class some way to get back to the ClientClass if
you wanted to be able to do things specific to that instance from within
the callback as well. As you can see, this technique can get arbitrarily
complicated if you want. :)

Another way to address the issue would be to use an anonymous delegate:

public ClientClass
{
void SomeMethod()
{
myClass myObject;
ElapsedEventArgs callback = delegate(object sender,
ElapsedEventArgs e)
{ someString = myObject.getValue(); }

myObject = new myClass(callback);
}
}

This takes advantage of the fact that the local variable "myObject" is
"captured" by the anonymous delegate. So you can use the variable within
the code of the delegate, and it will retain the value you set it to in
the SomeMethod() method. This might be especially appropriate if the
callback method exists for the sole purpose of handling that one
situation, which is in fact often the case for this sort of thing.


i几乎忘了:我使用了这个实现,因为来自

myClass的轮询将完全独立地工作。但是在外太空中设置一个计时器会更明智吗?而不是myClass?
i nearly forgot: i used this implementation, because the polling from
myClass would work completly independent. but would it be wiser to set
a timer in the "outer space" instead of "myClass"?



我认为不重要。如果你不需要从myClass外面的

访问计时器,我会说你不应该把它存放在

myClass之外。如果myClass实例本身也不需要访问计时器

,那么我会说即使在myClass中也没有必要存储它。


Pete

I don''t think it matters. If you have no need to access the timer from
outside myClass, I would say that you should not store it outside
myClass. If the myClass instance itself has no need to access the timer
either, then I would say there''s no need to store it even within myClass.

Pete


这篇关于代表混淆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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