Export (0) Print
Expand All

Type.GetMethod Method (String)

.NET Framework 1.1

Searches for the public method with the specified name.

[Visual Basic]
Overloads Public Function GetMethod( _
   ByVal name As String _
) As MethodInfo
[C#]
public MethodInfo GetMethod(
 string name
);
[C++]
public: MethodInfo* GetMethod(
 String* name
);
[JScript]
public 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).

Exceptions

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

Remarks

The search for name is case-sensitive.

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 parameters of MyFunction in MyClass and displays their position and data type.

[Visual Basic] 

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

[C#] 

using System;
using System.Reflection;
using System.Security; 

class MyClass
{
    public int myInt = 0;
    public string myString = null;
    public MyClass()
    {
    }
    public void Myfunction(int i)
    {
    } 
}
class Type_GetMethod
{
    public static void Main()
    {
        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}.", 
                    myParameters[i].Position + 1, myParameters[i].ParameterType);
        }
        catch (SecurityException e)
        {
            Console.WriteLine("SecurityException: " + e.Message ); 
        }
        catch (Exception e)
        {
            Console.WriteLine("Exception: " + e.Message ); 
        }      
    }
}

[C++] 

#using <mscorlib.dll>

using namespace System;
using namespace System::Reflection;
using namespace System::Security;

__gc class MyClass {
public:
   int*  myInt;
   String* myString;
   MyClass() {
   }
   void Myfunction(int i) {
      myString = __box(i)->ToString();
   }
};

int main() {
   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(S"Myfunction");

      // Get the parameters for Myfunction.
      ParameterInfo*  myParameters[] = myMethodInfo->GetParameters();

      Console::WriteLine(S"\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(S"The data type of parameter {0} is {1}.",
         __box(myParameters[i]->Position + 1), myParameters[i]->ParameterType);
   } catch (SecurityException* e) {
      Console::WriteLine(S"SecurityException: {0}", e->Message);
   } 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 | DefaultBinder | ReflectionPermission | GetMethodImpl | GetMethods

Show:
© 2014 Microsoft