Mega Code Archive

 
Categories / C# Tutorial / Reflection
 

A second form of GetMethods() lets you specify various flags that filter the methods that are retrieved

It has this general form: MethodInfo[ ] GetMethods(BindingFlags criteria) This version obtains only those methods that match the criteria. BindingFlags is an enumeration. Its most commonly used values are shown here: ValueMeaning DeclaredOnlyRetrieves only those methods defined by the specified class. Inherited methods are not included. InstanceRetrieves instance methods. NonPublicRetrieves nonpublic methods. PublicRetrieves public methods. StaticRetrieves static methods. You can OR together two or more flags. Minimally you must include either Instance or Static with Public or NonPublic. Failure to do so will result in no methods being retrieved. One of the main uses of the BindingFlags form of GetMethods() is to obtain a list of the methods defined by a class without also retrieving the inherited methods. For example, try substituting this call to GetMethods() into the preceding program: MethodInfo[] mi = t.GetMethods(BindingFlags.DeclaredOnly |                                BindingFlags.Instance |                                BindingFlags.Public) ;