使用反射获取类方法

San*_*San 37 c# reflection

当类名作为字符串传递时,如何使用反射获取类的所有公共方法,如下面的方法所示.?

 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;

 }
Run Code Online (Sandbox Code Playgroud)

请帮忙.谢谢

小智 53

MethodInfo[] methodInfos = Type.GetType(selectedObjcClass) 
                           .GetMethods(BindingFlags.Public | BindingFlags.Instance);
Run Code Online (Sandbox Code Playgroud)


Tim*_*ter 8

// 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);
Run Code Online (Sandbox Code Playgroud)

Type.GetMethods方法(BindingFlags)