Get class methods using reflection

2020-01-28 05:42发布

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

标签: c# reflection
3条回答
Luminary・发光体
2楼-- · 2020-01-28 06:21
MethodInfo[] methodInfos = Type.GetType(selectedObjcClass) 
                           .GetMethods(BindingFlags.Public | BindingFlags.Instance);
查看更多
ら.Afraid
3楼-- · 2020-01-28 06:22
// 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)

查看更多
登录 后发表回答