In C #, you can resolve this problem by reflection parsing the metadata, as shown in the following example code:
| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
usingSystem;usingSystem.Reflection;namespaceHello{ class Program { staticvoidMain(string[] args) { if(IsMethodDefined(typeof(Utils), "HelloWorld")) { Console.WriteLine("Utils类中有方法HelloWorld"); } else { Console.WriteLine("Utils类中没有方法HelloWorld"); } Console.ReadKey(); } /// <summary> /// 判断一个类中有无"指定名称"的方法 /// </summary> /// <param name="type"></param> /// <param name="methodName"></param> /// <returns></returns> staticboolIsMethodDefined(Type type,stringmethodName) { boolresult = false; foreach(MemberInfo m intype.GetMembers()) { if(m.Name == methodName) { result = true; break; } } returnresult; } } publicstaticclassUtils { publicstaticvoid HelloWorld() { Console.WriteLine("Hello World!"); } }} |
C # Determines whether a class has a "specified name" method