I would like to know how to find which of these methods are constructors as I am writing an IOC container. I would like to determine the constructors and their parameter types.
I would like to know how to find which of these methods are
constructors as I am writing an IOC container. I would like to
determine the constructors and their parameter types.
In short, you can't. Or, at the least, you'll find that down this path lies madness.
First, Objective-C does not have constructors. It has initializers, sometimes many, and -- for a properly written class -- only one of which is the designated initializer. There is no way to identify the designated initializer at compile time or run time.
How do I use this with a Method * and no instantiated member of the
class?
You don't. First you allocate an instance of the class, then you initialize the instance.
Overall, this level of abstraction just isn't done in Objective-C outside of academic investigations. It can be done, but it is generally avoided because of the fragility of the resulting solution and the hairball of code-hell that is trying to dynamically support the underlying C ABI (go look at the source to libffi).
If you want to go down this path, then you are far better off either defining a custom abstract class that all of your containers will subclass that can provide the binding logic to the class behind it.
Or use protocols; i.e. a class could implement an IOCBean protocol and one method would be initIOCGoop that is the designated initializer goo.
Doing this generically for all classes is going to be rife with fragility, special cases, and will require a gigantic mess of code that will be difficult to maintain over time.
NSMethodSignature 对象记录方法的参数和返回值的类型信息。它用于转发接收对象不响应的消息——尤其是在分布式对象的情况下。您通常使用 NSObject 的 methodSignatureForSelector: 实例方法创建 NSMethodSignature 对象(在 Mac OS X v10.5 及更高版本上,您还可以使用签名WithObjCTypes:)。然后它被用来创建一个 NSInitation 对象,该对象作为参数传递给一个forwardInitation:消息,以将调用发送到任何其他可以处理该消息的对象。在默认情况下,NSObject 调用 doesNotRecognizeSelector:,这会引发异常。对于分布式对象,使用 NSMethodSignature 对象中的信息对 NSInitation 对象进行编码,并将其发送到由消息接收者表示的真实对象。
You can get the method signature by using the following method:
methodSignatureForSelector:
From the documentation:
An NSMethodSignature object records type information for the arguments and return value of a method. It is used to forward messages that the receiving object does not respond to—most notably in the case of distributed objects. You typically create an NSMethodSignature object using NSObject’s methodSignatureForSelector: instance method (on Mac OS X v10.5 and later you can also use signatureWithObjCTypes:). It is then used to create an NSInvocation object, which is passed as the argument to a forwardInvocation: message to send the invocation on to whatever other object can handle the message. In the default case, NSObject invokes doesNotRecognizeSelector:, which raises an exception. For distributed objects, the NSInvocation object is encoded using the information in the NSMethodSignature object and sent to the real object represented by the receiver of the message.
发布评论
评论(2)
简而言之,你不能。或者,至少,你会发现这条路充满了疯狂。
首先,Objective-C 没有构造函数。它有初始化器,有时有很多,而且——对于一个正确编写的类——只有其中一个是指定的初始化器。无法在编译时或运行时识别指定的初始值设定项。
你不知道。首先分配该类的一个实例,然后初始化该实例。
总体而言,除了学术研究之外,Objective-C 中并未完成这种抽象级别。这是可以做到的,但通常会避免这样做,因为最终解决方案的脆弱性以及试图动态支持底层 C ABI 的代码地狱的毛团(去看看 libffi 的源代码)。
如果您想走这条路,那么您最好定义一个自定义抽象类,您的所有容器都将对该抽象类进行子类化,该抽象类可以为其背后的类提供绑定逻辑。
或者使用协议;即,一个类可以实现 IOCBean 协议,并且一个方法是
initIOCGoop
,它是指定的初始值设定项 goo。对所有类通用地执行此操作将充满脆弱性和特殊情况,并且需要大量混乱的代码,随着时间的推移,这些代码将很难维护。
In short, you can't. Or, at the least, you'll find that down this path lies madness.
First, Objective-C does not have constructors. It has initializers, sometimes many, and -- for a properly written class -- only one of which is the designated initializer. There is no way to identify the designated initializer at compile time or run time.
You don't. First you allocate an instance of the class, then you initialize the instance.
Overall, this level of abstraction just isn't done in Objective-C outside of academic investigations. It can be done, but it is generally avoided because of the fragility of the resulting solution and the hairball of code-hell that is trying to dynamically support the underlying C ABI (go look at the source to libffi).
If you want to go down this path, then you are far better off either defining a custom abstract class that all of your containers will subclass that can provide the binding logic to the class behind it.
Or use protocols; i.e. a class could implement an IOCBean protocol and one method would be
initIOCGoop
that is the designated initializer goo.Doing this generically for all classes is going to be rife with fragility, special cases, and will require a gigantic mess of code that will be difficult to maintain over time.
您可以使用以下方法获取方法签名:
methodSignatureForSelector:
来自文档:
NSMethodSignature 对象记录方法的参数和返回值的类型信息。它用于转发接收对象不响应的消息——尤其是在分布式对象的情况下。您通常使用 NSObject 的 methodSignatureForSelector: 实例方法创建 NSMethodSignature 对象(在 Mac OS X v10.5 及更高版本上,您还可以使用签名WithObjCTypes:)。然后它被用来创建一个 NSInitation 对象,该对象作为参数传递给一个forwardInitation:消息,以将调用发送到任何其他可以处理该消息的对象。在默认情况下,NSObject 调用 doesNotRecognizeSelector:,这会引发异常。对于分布式对象,使用 NSMethodSignature 对象中的信息对 NSInitation 对象进行编码,并将其发送到由消息接收者表示的真实对象。
You can get the method signature by using the following method:
methodSignatureForSelector:
From the documentation:
An NSMethodSignature object records type information for the arguments and return value of a method. It is used to forward messages that the receiving object does not respond to—most notably in the case of distributed objects. You typically create an NSMethodSignature object using NSObject’s methodSignatureForSelector: instance method (on Mac OS X v10.5 and later you can also use signatureWithObjCTypes:). It is then used to create an NSInvocation object, which is passed as the argument to a forwardInvocation: message to send the invocation on to whatever other object can handle the message. In the default case, NSObject invokes doesNotRecognizeSelector:, which raises an exception. For distributed objects, the NSInvocation object is encoded using the information in the NSMethodSignature object and sent to the real object represented by the receiver of the message.