Question

Context:

I have a dependency injection container - 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.

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. . .

The Question:

Is it possible to use resolveInstanceMethod to define an implementation where the number of arguments is not known up front?

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.

Was it helpful?

Solution

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.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top