In C #, the metadata can be analyzed through reflection. Example: Code As follows:
Using system; using system. reflection; namespace Hello {class program {static void main (string [] ARGs) {If (ismethoddefined (typeof (utils), "helloworld") {console. writeline ("methods in the utils class helloworld");} else {console. writeline ("there is no way to helloworld in utils class");} console. readkey ();} /// <summary> /// determines whether a class has a "specified name" method. /// </Summary> /// <Param name = "type"> </ param> /// <Param name = "methodname"> </param> /// <Returns> </returns> static bool ismethoddefined (type, string methodname) {bool result = false; foreach (memberinfo m in type. getmembers () {If (M. name = methodname) {result = true; break ;}} return result ;}} public static class utils {public static void helloworld () {console. writeline ("Hello world! ");}}}
In obj-C, it is determined by selector.
SAMPE. h
# Import <Foundation/Foundation. h> @ interface sample: nsobject {}-(void) print :( nsstring *) MSG; @ end
Sample. m
# Import "sample. H" @ implementation sample-(void) print :( nsstring *) MSG {nslog (@ "% @", MSG);} @ end
Main function:
# Import <Foundation/Foundation. h> # import "sample. H "int main (INT argc, const char * argv []) {ngutoreleasepool * Pool = [[ngutoreleasepool alloc] init]; sample * s = [sample new]; if ([s respondstoselector: @ selector (print :)]) // This row is used to determine whether the print {[S print: @ "Hello World"];} else {nslog (@ "% @", @ "print" is not defined in the sample class);} [S release]; [pool drain]; return 0 ;}