49

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

RoflcoptrException
  • 51,941
  • 35
  • 152
  • 200
San
  • 1,797
  • 7
  • 32
  • 56

3 Answers3

78
MethodInfo[] methodInfos = Type.GetType(selectedObjcClass) 
                           .GetMethods(BindingFlags.Public | BindingFlags.Instance);
Sachin Joseph
  • 18,928
  • 4
  • 42
  • 62
13
// 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)

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
3

Type.GetMethods Method

archil
  • 39,013
  • 7
  • 65
  • 82