只知道类名 动态调用方法
Class cls = NSClassFromString(@"a");
[cls performSelector:NSSelectorFromString(@"show")];
Method method = class_getInstanceMethod(cls, NSSelectorFromString(@"run"));
if (method) {
IMP imp = method_getImplementation(method);
imp();
}
- (id)performSelector:(SEL)aSelector onTarget:(id)target withArgs:(NSArray *)args
{
id ret = nil;
if (aSelector == nil || target == nil || ![target respondsToSelector:aSelector]) {
return ret;
}
NSLog(@"【action】%@ %@ %@ %@",NSStringFromSelector(_cmd),NSStringFromClass([target class]),NSStringFromSelector(aSelector),args);
NSMethodSignature *signature = [target methodSignatureForSelector:aSelector];
if (args.count + 2 != signature.numberOfArguments) {
return ret;
}
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
[invocation setSelector:aSelector];
[invocation setTarget:target];
for (int i = 2, j = 0; j < args.count; i++, j++) {
id arg = [args objectAtIndex:j];
[invocation setArgument:&arg atIndex:i];
}
[invocation invoke];
NSUInteger length = [[invocation methodSignature] methodReturnLength];
// void *buffer = (void *)malloc(length);
if (length > 0) {
[invocation getReturnValue:&ret];
}
return ret;
}
- (id)performClassSelector:(SEL)aSelector onTarget:(Class)target withArgs:(NSArray *)args
{
id ret = nil;
NSLog(@"【action】%@ %@ %@ %@",NSStringFromSelector(_cmd),NSStringFromClass([target class]),NSStringFromSelector(aSelector),args);
if (aSelector == nil || target == nil) {
return ret;
}
NSMethodSignature *signature = [target methodSignatureForSelector:aSelector];
if (args.count + 2 != signature.numberOfArguments) {
return ret;
}
NSMutableString *argsInCTypes = [NSMutableString stringWithString:@"v"];
for (int i = 0; i < args.count; ++i) {
[argsInCTypes appendString:@"@:"];
}
signature = [target methodSignatureForSelector:aSelector]; // [NSMethodSignature signatureWithObjCTypes:argsInCTypes.UTF8String];
if (signature == nil) {
return ret;
}
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
[invocation setSelector:aSelector];
[invocation setTarget:target];
for (int i = 2, j = 0; j < args.count; i++, j++) {
id arg = [args objectAtIndex:j];
[invocation setArgument:&arg atIndex:i];
}
[invocation invoke];
NSUInteger length = [[invocation methodSignature] methodReturnLength];
// void *buffer = (void *)malloc(length);
if (length > 0) {
[invocation getReturnValue:&ret];
}
return ret;
}