简体   繁体   English

在Smalltalk中获取消息的发件人

[英]Get sender of a message in Smalltalk

Is there a practical way to get the sender of a message in Smalltalk without manually passing self as a Parameter? 有没有一种实用的方法可以在Smalltalk中获取消息的发件人, 而无需手动将self作为参数传递

To be more concrete: I want to add a class specific prefix to an ID that gets passed to my class, so if ClassA sends (on class side) 更具体地讲:我想在传递给我的类的ID上添加特定于类的前缀,因此,如果ClassA发送(在类端)

ClassB doSomethingWith: 'myId'.

ClassB should internally treat 'myId' as 'ClassB-myId' or something similar. ClassB应该在内部将“ myId”视为“ ClassB-myId”或类似名称。

I have implemented this with an additional parameter which has to be self 我已经用一个必须是self的附加参数实现了它

ClassB doSomethingWith: 'myId' for: self.

but I would be very glad if there is a solution without this explicit send of self. 但是如果没有这种明确的自我解决方案,我会很高兴。

You can use the reflective facilities on the execution stack: 您可以在执行堆栈上使用反射功能:

thisContext sender receiver

  1. thisContext answers the current stack frame thisContext回答当前的堆栈帧
  2. sender answers the parent stack frame sender回答父堆栈帧
  3. receiver answers the receiver of the stack frame receiver回答堆栈帧的接收方

This should work in Pharo, VisualWorks and GemStone. 这应该可以在Pharo,VisualWorks和GemStone中使用。 Other Smalltalk might use different method names. 其他Smalltalk可能使用不同的方法名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM