Export (0) Print
Expand All

Type.GetMethod Method (String, BindingFlags)

.NET Framework 1.1

Searches for the specified method, using the specified binding constraints.

[Visual Basic]
Overloads Public Overridable Function GetMethod( _
   ByVal name As String, _
   ByVal bindingAttr As BindingFlags _
) As MethodInfo Implements IReflect.GetMethod
[C#]
public virtual MethodInfo GetMethod(
 string name,
 BindingFlags bindingAttr
);
[C++]
public: virtual MethodInfo* GetMethod(
 String* name,
 BindingFlags bindingAttr
);
[JScript]
public function GetMethod(
   name : String,
 bindingAttr : BindingFlags
) : MethodInfo;

Parameters

name
The String containing the name of the method to get.
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

A MethodInfo object representing the method that matches the specified requirements, if found; otherwise, a null reference (Nothing in Visual Basic).

Implements

IReflect.GetMethod

Exceptions

Exception Type Condition
AmbiguousMatchException More than one method is found with the specified name and matching the specified binding constraints.
ArgumentNullException name is a null reference (Nothing in Visual Basic).

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.IgnoreCase to ignore the case of name.
  • 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 gets the method that matches the specified binding flags and displays its signature.

[Visual Basic] 

Imports System
Imports System.Reflection
Imports Microsoft.VisualBasic

Class [MyClass]

    Public Sub MyMethod()
    End Sub 'MyMethod
End Class '[MyClass]
Public Class Type_GetMethod
    Public Shared Sub Main()
        Try
            ' Get the type of MyClass.
            Dim myType As Type = GetType([MyClass])

            ' Get the method information for MyClass.
            Dim myMethodInfo As MethodInfo = myType.GetMethod("MyMethod", BindingFlags.Public Or BindingFlags.Instance)

            ' Display the method information.
            Console.WriteLine(ControlChars.Cr + "The signature of MyMethod is: {0}", myMethodInfo.ToString())
        Catch e As Exception
            Console.WriteLine("Exception: {0}", e.Message.ToString())
        End Try
    End Sub 'Main
End Class 'Type_GetMethod

[C#] 

using System;
using System.Reflection;

class MyClass
{
    public void MyMethod()
    {    
    }
}

public class Type_GetMethod
{
    public static void Main()
    {
        try
        {
            // Get the type of MyClass.
            Type myType = typeof(MyClass);
         
            // Get the method information for MyClass.
            MethodInfo myMethodInfo = myType.GetMethod("MyMethod",
                BindingFlags.Public|BindingFlags.Instance );
         
            // Display the method information.
            Console.WriteLine("\nThe signature of MyMethod is: {0}", myMethodInfo.ToString());
        }
        catch(Exception e)
        {
            Console.WriteLine("Exception: {0}", e.Message);
        }
    }
}

[C++] 

#using <mscorlib.dll>

using namespace System;
using namespace System::Reflection;

__gc class MyClass {
public:
   void MyMethod() {
   }
};

int main() {
   try {
      // Get the type of MyClass.
      Type*  myType = __typeof(MyClass);

      // Get the method information for MyClass.
      MethodInfo*  myMethodInfo = myType->GetMethod(S"MyMethod",
         static_cast<BindingFlags>(BindingFlags::Public|BindingFlags::Instance));

      // Display the method information.
      Console::WriteLine(S"\nThe signature of MyMethod is: {0}", myMethodInfo);
   } catch (Exception* e) {
      Console::WriteLine(S"Exception: {0}", e->Message);
   }
}

[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.GetMethod Overload List | MethodInfo | String | BindingFlags | DefaultBinder | ReflectionPermission | GetMethodImpl | GetMethods

Show:
© 2014 Microsoft