Export (0) Print
Expand All

Type.GetMethods Method (BindingFlags)

.NET Framework 1.1

When overridden in a derived class, searches for the methods defined for the current Type, using the specified binding constraints.

[Visual Basic]
Overloads Public MustOverride Function GetMethods( _
   ByVal bindingAttr As BindingFlags _
) As MethodInfo() Implements IReflect.GetMethods
[C#]
public abstract MethodInfo[] GetMethods(
 BindingFlags bindingAttr
);
[C++]
public: virtual MethodInfo* GetMethods(
 BindingFlags bindingAttr
) [] = 0;
[JScript]
public abstract function GetMethods(
   bindingAttr : BindingFlags
) : MethodInfo[];

Parameters

bindingAttr
A bitmask comprised of one or more BindingFlags that specify how the search is conducted.

-or-

Zero, to return a null reference (Nothing in Visual Basic).

Return Value

An array of MethodInfo objects representing all methods defined for the current Type that match the specified binding constraints.

-or-

An empty array of type MethodInfo, if no methods are defined for the current Type, or if none of the defined methods match the binding constraints.

Implements

IReflect.GetMethods

Remarks

The following BindingFlags filter flags can be used to define which methods to include in the search:

  • You must specify either BindingFlags.Instance or BindingFlags.Static in order to get a return.
  • Specify BindingFlags.Public to include public methods in the search.
  • Specify BindingFlags.NonPublic to include non-public methods (that is, private and protected members) in the search.
  • Specify BindingFlags.FlattenHierarchy to include static methods up the hierarchy.

The following BindingFlags modifier flags can be used to change how the search works:

  • BindingFlags.DeclaredOnly to search only the methods declared on the Type, not methods that were simply inherited.

See System.Reflection.BindingFlags for more information.

If the requested type is non-public and the caller does not have ReflectionPermission to reflect non-public objects outside the current assembly, this method returns a null reference (Nothing in Visual Basic).

Note   You cannot omit parameters when looking up constructors and methods. You can only omit parameters when invoking.

Example

[Visual Basic, C#, C++] The following example creates a class with two public methods and one protected method, creates a Type object corresponding to MyTypeClass, gets all the public and nonpublic methods, and displays their number and names.

[Visual Basic] 

Imports System
Imports System.Reflection
Imports System.Reflection.Emit
Imports Microsoft.VisualBasic

' Create a class having two public methods and one protected method.
Public Class MyTypeClass
    Public Sub MyMethods()
    End Sub 'MyMethods
    Public Function MyMethods1() As Integer
        Return 3
    End Function 'MyMethods1
    Protected Function MyMethods2() As [String]
        Return "hello"
    End Function 'MyMethods2
End Class 'MyTypeClass
Public Class TypeMain
    Public Shared Sub Main()

        Dim myType As Type = GetType(MyTypeClass)
        ' Get the public methods.
        Dim myArrayMethodInfo As MethodInfo() = myType.GetMethods((BindingFlags.Public Or BindingFlags.Instance Or BindingFlags.DeclaredOnly))
        Console.WriteLine((ControlChars.Cr + "The number of public methods is " & myArrayMethodInfo.Length.ToString() & "."))
        ' Display all the public methods.
        DisplayMethodInfo(myArrayMethodInfo)
        ' Get the nonpublic methods.
        Dim myArrayMethodInfo1 As MethodInfo() = myType.GetMethods((BindingFlags.NonPublic Or BindingFlags.Instance Or BindingFlags.DeclaredOnly))
        Console.WriteLine((ControlChars.Cr + "The number of protected methods is " & myArrayMethodInfo1.Length.ToString() & "."))
        ' Display all the nonpublic methods.
        DisplayMethodInfo(myArrayMethodInfo1)
    End Sub 'Main

    Public Shared Sub DisplayMethodInfo(ByVal myArrayMethodInfo() As MethodInfo)
        ' Display information for all methods.
        Dim i As Integer
        For i = 0 To myArrayMethodInfo.Length - 1
            Dim myMethodInfo As MethodInfo = CType(myArrayMethodInfo(i), MethodInfo)
            Console.WriteLine((ControlChars.Cr + "The name of the method is " & myMethodInfo.Name & "."))
        Next i
    End Sub 'DisplayMethodInfo
End Class 'TypeMain    

[C#] 

using System;
using System.Reflection;
using System.Reflection.Emit;

        // Create a class having two public methods and one protected method.
public class MyTypeClass
{
    public void MyMethods()
    {
    }
    public int MyMethods1() 
    {
        return 3;
    }
    protected String MyMethods2()
    {
        return "hello";
    }
}
public class TypeMain
{
    public static void Main() 
    {
        Type myType =(typeof(MyTypeClass));
        // Get the public methods.
        MethodInfo[] myArrayMethodInfo = myType.GetMethods(BindingFlags.Public|BindingFlags.Instance|BindingFlags.DeclaredOnly);
        Console.WriteLine("\nThe number of public methods is {0}.", myArrayMethodInfo.Length);
        // Display all the methods.
        DisplayMethodInfo(myArrayMethodInfo);
        // Get the nonpublic methods.
        MethodInfo[] myArrayMethodInfo1 = myType.GetMethods(BindingFlags.NonPublic|BindingFlags.Instance|BindingFlags.DeclaredOnly);
        Console.WriteLine("\nThe number of protected methods is {0}.", myArrayMethodInfo1.Length);
        // Display information for all methods.
        DisplayMethodInfo(myArrayMethodInfo1);        
    }
    public static void DisplayMethodInfo(MethodInfo[] myArrayMethodInfo)
    {
        // Display information for all methods.
        for(int i=0;i<myArrayMethodInfo.Length;i++)
        {
            MethodInfo myMethodInfo = (MethodInfo)myArrayMethodInfo[i];
            Console.WriteLine("\nThe name of the method is {0}.", myMethodInfo.Name);
        }
    }
}

[C++] 

#using <mscorlib.dll>

using namespace System;
using namespace System::Reflection;
using namespace System::Reflection::Emit;

// Create a class having two public methods and one protected method.
public __gc class MyTypeClass {
public:
   void MyMethods() {
   }
   int MyMethods1() {
      return 3;
   }
protected:
   String* MyMethods2() {
      return S"hello";
   }
};


void DisplayMethodInfo(MethodInfo* myArrayMethodInfo[]) {
   // Display information for all methods.
   for (int i=0;i<myArrayMethodInfo->Length;i++) {
      MethodInfo*  myMethodInfo = dynamic_cast<MethodInfo*>(myArrayMethodInfo[i]);
      Console::WriteLine(S"\nThe name of the method is {0}.", myMethodInfo->Name);
   }
}
int main() {
   Type* myType =(__typeof(MyTypeClass));
   // Get the public methods.
   MethodInfo*  myArrayMethodInfo[] = myType->GetMethods(static_cast<BindingFlags>(BindingFlags::Public|BindingFlags::Instance|BindingFlags::DeclaredOnly));
   Console::WriteLine(S"\nThe number of public methods is {0}->",__box( myArrayMethodInfo->Length));
   // Display all the methods.
   DisplayMethodInfo(myArrayMethodInfo);
   // Get the nonpublic methods.
   MethodInfo*  myArrayMethodInfo1[] = myType->GetMethods(static_cast<BindingFlags>(BindingFlags::NonPublic|BindingFlags::Instance|BindingFlags::DeclaredOnly));
   Console::WriteLine(S"\nThe number of protected methods is {0}->",__box( myArrayMethodInfo1->Length));
   // Display information for all methods.
   DisplayMethodInfo(myArrayMethodInfo1);
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

.NET Framework Security: 

See Also

Type Class | Type Members | System Namespace | Type.GetMethods Overload List | MethodInfo | BindingFlags | DefaultBinder | ReflectionPermission | GetMethod

Show:
© 2014 Microsoft