Context:
背景:
I have a dependency injection container - www.typhoonframework.org
我有一个依赖注入容器——www.typhoonframework.org
It allows using the interface for defining components to resolve them at runtime - using resolveInstanceMethod and implentationWithBlock to trampoline the request to DI container.
它允许使用定义组件的接口在运行时解析它们——使用resolveInstanceMethod和implentationWithBlock来处理对DI容器的请求。
Users have been asking for some parameters to be provided at runtime. For example:
用户一直要求在运行时提供一些参数。例如:
[assembly dangerousEnemyWithWeapon:id<BigGun>]
. . where the enemy is assembled from collaborating classes in the DI container, but the gun is provided at runtime. . .
。。在DI容器中,敌人是通过协作类组装的,但是枪是在运行时提供的…
The Question:
一个问题:
Is it possible to use resolveInstanceMethod to define an implementation where the number of arguments is not known up front?
是否可以使用resolveInstanceMethod来定义一个在前面不知道参数数量的实现?
I would like to package up these arguments, and forward them on to another responder.
我想整理这些参数,并将它们转发给另一个应答器。
The arguments could be packed in order or added to a dictionary with the matching selector part as key.
参数可以按顺序打包,也可以将匹配选择器部分作为键添加到字典中。
1 个解决方案
#1
5
You can't use +resolveInstanceMethod:
for that, but you can use traditional forwarding. +resolveInstanceMethod:
just installs a new instance method on the class using the Objective-C runtime. You don't get to affect how it is called. It will be called just like any other method with the arguments in the registers and on the stack as the caller supplied them. You don't get an opportunity to package or marshal the arguments.
你不能使用+resolveInstanceMethod:为此,你可以使用传统的转发。+resolveInstanceMethod:只需使用Objective-C运行时在类上安装一个新的实例方法。你不会影响它的调用方式。它将被调用,就像调用者在寄存器和堆栈中提供参数的任何其他方法一样。你没有机会去打包或整理这些争论。
If you implement -forwardInvocation:
and -methodSignatureForSelector:
, then you get an NSInvocation
object. That's already a packaging of the arguments (and return value). You can use that as it is or interrogate it to unpack the arguments and repack them how you want.
如果实现- forwarding调用:和-methodSignatureForSelector:,那么您将获得一个nsin天命对象。这已经是参数(和返回值)的打包。您可以按原样使用它,或者查询它来解压缩参数并按自己的方式重新打包它们。
#1
5
You can't use +resolveInstanceMethod:
for that, but you can use traditional forwarding. +resolveInstanceMethod:
just installs a new instance method on the class using the Objective-C runtime. You don't get to affect how it is called. It will be called just like any other method with the arguments in the registers and on the stack as the caller supplied them. You don't get an opportunity to package or marshal the arguments.
你不能使用+resolveInstanceMethod:为此,你可以使用传统的转发。+resolveInstanceMethod:只需使用Objective-C运行时在类上安装一个新的实例方法。你不会影响它的调用方式。它将被调用,就像调用者在寄存器和堆栈中提供参数的任何其他方法一样。你没有机会去打包或整理这些争论。
If you implement -forwardInvocation:
and -methodSignatureForSelector:
, then you get an NSInvocation
object. That's already a packaging of the arguments (and return value). You can use that as it is or interrogate it to unpack the arguments and repack them how you want.
如果实现- forwarding调用:和-methodSignatureForSelector:,那么您将获得一个nsin天命对象。这已经是参数(和返回值)的打包。您可以按原样使用它,或者查询它来解压缩参数并按自己的方式重新打包它们。