Type.GetMethods Method

Definition

Gets the methods of the current Type.

Overloads

GetMethods(BindingFlags)

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

GetMethods()

Returns all the public methods of the current Type.

GetMethods(BindingFlags)

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

public:
 abstract cli::array <System::Reflection::MethodInfo ^> ^ GetMethods(System::Reflection::BindingFlags bindingAttr);
public abstract System.Reflection.MethodInfo[] GetMethods (System.Reflection.BindingFlags bindingAttr);
abstract member GetMethods : System.Reflection.BindingFlags -> System.Reflection.MethodInfo[]
Public MustOverride Function GetMethods (bindingAttr As BindingFlags) As MethodInfo()

Parameters

bindingAttr
BindingFlags

A bitwise combination of the enumeration values that specify how the search is conducted.

-or-

Default to return an empty array.

Returns

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

Examples

The following example creates a class with two public methods and one protected method, creates a Type object corresponding to MyTypeClass, gets all public and non-public methods, and displays their names.

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

// Create a class having two public methods and one protected method.
public ref class MyTypeClass
{
public:
   void MyMethods(){}

   int MyMethods1()
   {
      return 3;
   }


protected:
   String^ MyMethods2()
   {
      return "hello";
   }
};

void DisplayMethodInfo( array<MethodInfo^>^myArrayMethodInfo )
{
   // Display information for all methods.
   for ( int i = 0; i < myArrayMethodInfo->Length; i++ )
   {
      MethodInfo^ myMethodInfo = dynamic_cast<MethodInfo^>(myArrayMethodInfo[ i ]);
      Console::WriteLine( "\nThe name of the method is {0}.", myMethodInfo->Name );
   }
}

int main()
{
   Type^ myType = MyTypeClass::typeid;
   
   // Get the public methods.
   array<MethodInfo^>^myArrayMethodInfo = myType->GetMethods( static_cast<BindingFlags>(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.
   array<MethodInfo^>^myArrayMethodInfo1 = myType->GetMethods( static_cast<BindingFlags>(BindingFlags::NonPublic | BindingFlags::Instance | BindingFlags::DeclaredOnly) );
   Console::WriteLine( "\nThe number of protected methods is {0}->", myArrayMethodInfo1->Length );
   
   // Display information for all methods.
   DisplayMethodInfo( myArrayMethodInfo1 );
}
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);
        }
    }
}

Imports System.Reflection
Imports System.Reflection.Emit

' Create a class having two public methods and one protected method.
Public Class MyTypeClass
    Public Sub MyMethods()
    End Sub
    Public Function MyMethods1() As Integer
        Return 3
    End Function 'MyMethods1
    Protected Function MyMethods2() As [String]
        Return "hello"
    End Function 'MyMethods2
End Class
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

    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
End Class

Remarks

For the GetMethods(BindingFlags) overload to successfully retrieve method information, the bindingAttr argument must include at least one of BindingFlags.Instance and BindingFlags.Static, along with at least one of BindingFlags.NonPublic and BindingFlags.Public.

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

  • Specify BindingFlags.Instance to include instance methods.

  • Specify BindingFlags.Static to include static methods.

  • Specify BindingFlags.Public to include public methods in the search.

  • Specify BindingFlags.NonPublic to include non-public methods (that is, private, internal, and protected methods) in the search. Only protected and internal methods on base classes are returned; private methods on base classes are not returned.

  • Specify BindingFlags.FlattenHierarchy to include public and protected static members up the hierarchy; private static members in inherited classes are not included.

  • Specify BindingFlags.Default alone to return an empty MethodInfo array.

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.

In .NET 6 and earlier versions, the GetMethods method does not return methods in a particular order, such as alphabetical or declaration order. Your code must not depend on the order in which methods are returned, because that order varies. However, starting with .NET 7, the ordering is deterministic based upon the metadata ordering in the assembly.

If the current Type represents a constructed generic type, this method returns the MethodInfo objects with the type parameters replaced by the appropriate type arguments.

If the current Type represents a type parameter in the definition of a generic type or generic method, this method searches the methods of the class constraint, or the methods of Object if there is no class constraint.

See also

Applies to

GetMethods()

Returns all the public methods of the current Type.

public:
 cli::array <System::Reflection::MethodInfo ^> ^ GetMethods();
public:
 virtual cli::array <System::Reflection::MethodInfo ^> ^ GetMethods();
public System.Reflection.MethodInfo[] GetMethods ();
member this.GetMethods : unit -> System.Reflection.MethodInfo[]
abstract member GetMethods : unit -> System.Reflection.MethodInfo[]
override this.GetMethods : unit -> System.Reflection.MethodInfo[]
Public Function GetMethods () As MethodInfo()

Returns

An array of MethodInfo objects representing all the public methods defined for the current Type.

-or-

An empty array of type MethodInfo, if no public methods are defined for the current Type.

Implements

Remarks

In .NET 6 and earlier versions, the GetMethods method does not return methods in a particular order, such as alphabetical or declaration order. Your code must not depend on the order in which methods are returned, because that order varies. However, starting with .NET 7, the ordering is deterministic based upon the metadata ordering in the assembly.

Constructors are not included in the array of methods returned by this call. Make a separate call to GetConstructors() to get the constructor methods.

The following table shows what members of a base class are returned by the Get methods when reflecting on a type.

Member Type Static Non-Static
Constructor No No
Field No Yes. A field is always hide-by-name-and-signature.
Event Not applicable The common type system rule is that the inheritance is the same as that of the methods that implement the property. Reflection treats properties as hide-by-name-and-signature. See note 2 below.
Method No Yes. A method (both virtual and non-virtual) can be hide-by-name or hide-by-name-and-signature.
Nested Type No No
Property Not applicable The common type system rule is that the inheritance is the same as that of the methods that implement the property. Reflection treats properties as hide-by-name-and-signature. See note 2 below.
  1. Hide-by-name-and-signature considers all of the parts of the signature, including custom modifiers, return types, parameter types, sentinels, and unmanaged calling conventions. This is a binary comparison.

  2. For reflection, properties and events are hide-by-name-and-signature. If you have a property with both a get and a set accessor in the base class, but the derived class has only a get accessor, the derived class property hides the base class property, and you will not be able to access the setter on the base class.

  3. Custom attributes are not part of the common type system.

Note

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

If the current Type represents a constructed generic type, this method returns the MethodInfo objects with the type parameters replaced by the appropriate type arguments.

If the current Type represents a type parameter in the definition of a generic type or generic method, this method searches the methods of the class constraint, or the methods of Object if there is no class constraint.

See also

Applies to