简体   繁体   English

使用反射获取类方法

[英]Get class methods using reflection

How can I get all the public methods of class using reflection when class name is passed as a string as shown in the below method. 当类名作为字符串传递时,如何使用反射获取类的所有公共方法,如下面的方法所示。 ?

 private  MethodInfo[] GetObjectMethods(string selectedObjClass)
 {
   MethodInfo[] methodInfos;
   Assembly assembly = Assembly.GetAssembly(typeof(sampleAdapater));
   Type _type = assembly.GetType("SampleSolution.Data.MyData." + selectedObjClass);

  ///get all the methods for the classname passed as string

   return methodInfos;

 }

Please help. 请帮忙。 Thanks 谢谢

MethodInfo[] methodInfos = Type.GetType(selectedObjcClass) 
                           .GetMethods(BindingFlags.Public | BindingFlags.Instance);
// get all public static methods of given type(public would suffer in your case, only to show how you could other BindingFlags)
MethodInfo[] methodInfos = _type.GetMethods(BindingFlags.Public | BindingFlags.Static);

Type.GetMethods Method (BindingFlags) Type.GetMethods方法(BindingFlags)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM