Export (0) Print
Expand All

Type.GetMethods Method

Gets the methods of the current Type.

Overload List

Returns all the public methods of the current Type.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Function GetMethods() As MethodInfo()
[C#] public MethodInfo[] GetMethods();
[C++] public: MethodInfo* GetMethods() [];
[JScript] public function GetMethods() : MethodInfo[];

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

Supported by the .NET Compact Framework.

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

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, C#, C++] Note   This example shows how to use one of the overloaded versions of GetMethods. For other examples that might be available, see the individual overload topics.
[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.

See Also

Type Class | Type Members | System Namespace

Show:
© 2014 Microsoft