Type.GetMethod Method (String)

Searches for the public method with the specified name.

Namespace: System
Assembly: mscorlib (in mscorlib.dll)

'Declaration
Public Function GetMethod ( _
	name As String _
) As MethodInfo
'Usage
Dim instance As Type
Dim name As String
Dim returnValue As MethodInfo

returnValue = instance.GetMethod(name)
public final MethodInfo GetMethod (
	String name
)
public final function GetMethod (
	name : String
) : MethodInfo

Parameters

name

The String containing the name of the public method to get.

Return Value

A MethodInfo object representing the public method with the specified name, if found; otherwise, a null reference (Nothing in Visual Basic).

Exception typeCondition

AmbiguousMatchException

More than one method is found with the specified name.

ArgumentNullException

name is a null reference (Nothing in Visual Basic).

The search for name is case-sensitive. The search includes public static and public instance methods.

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).

NoteNote

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

If the current T:System.Type represents a constructed generic type, this method returns the MethodInfo 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.

NoteNote

For generic methods, do not include the type arguments in name. For example, the C# code GetMember("MyMethod<int>") searches for a member with the text name "MyMethod<int>", rather than for a method named MyMethod that has one generic argument of type int.

The following example gets the parameters of MyFunction in MyClass and displays their position and data type.


Imports System
Imports System.Reflection
Imports System.Security
Imports Microsoft.VisualBasic

Class [MyClass]
    Public myInt As Integer = 0
    Public myString As String = Nothing
    Public Sub New()
    End Sub 'New
    Public Sub Myfunction(ByVal i As Integer)
    End Sub 'Myfunction
End Class '[MyClass]

Class Type_GetMethod
    Public Shared Sub Main()
        Try
            Dim MyObject As New [MyClass]()
            Dim myMethodInfo As MethodInfo

            ' Get the type of MyClass.
            Dim myType As Type = MyObject.GetType()

            ' Get the method information for MyFunction. 
            myMethodInfo = myType.GetMethod("Myfunction")

            ' Get the parameters for Myfunction.
            Dim myParameters As ParameterInfo() = myMethodInfo.GetParameters()

            Console.WriteLine(ControlChars.Cr + "The parameters of the method Myfunction of class MyClass are:" + ControlChars.Cr)

            ' Display the position and type of the parameters.
            Dim i As Integer
            For i = 0 To myParameters.Length - 1
                Console.WriteLine("The data type of parameter {0} is {1}.", myParameters(i).Position + 1, myParameters(i).ParameterType)
            Next i
        Catch e As SecurityException
            Console.WriteLine(("SecurityException: " + e.Message.ToString()))
        Catch e As Exception
            Console.WriteLine(("Exception: " + e.Message.ToString()))
        End Try
    End Sub 'Main 
End Class 'Type_GetMethod


import System.*;
import System.Reflection.*;
import System.Security.*;

class MyClass
{
    public int myInt = 0;
    public String myString = null;

    public MyClass()
    {
    } //MyClass

    public void Myfunction(int i)
    {
    } //Myfunction
} //MyClass

class Type_GetMethod
{
    public static void main(String[] args)
    {
        try {
            MyClass myObject = new MyClass();
            MethodInfo myMethodInfo;
            // Get the type of MyClass.
            Type myType = myObject.GetType();
            // Get the method information for MyFunction. 
            myMethodInfo = myType.GetMethod("Myfunction");
            // Get the parameters for Myfunction.
            ParameterInfo myParameters[] = myMethodInfo.GetParameters();

            Console.WriteLine("\nThe parameters of the method Myfunction of"
                + " class MyClass are :\n");
            // Display the position and type of the parameters.
            for (int i = 0; i < myParameters.length; i++) {
                Console.WriteLine("The data type of parameter {0} is {1}.",
                    (Int32)(myParameters[i].get_Position() + 1),
                    myParameters[i].get_ParameterType());
            }
        }
        catch (SecurityException e) {
            Console.WriteLine("SecurityException: " + e.get_Message());
        }
        catch (System.Exception e) {
            Console.WriteLine("Exception: " + e.get_Message());
        }
    } //main
} //Type_GetMethod

  • ReflectionPermission  for reflecting non-public objects. Associated enumeration: ReflectionPermissionFlag.TypeInformation

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions

ADD
Show: