Export (0) Print
Expand All
0 out of 1 rated this helpful - Rate this topic

Type.GetMethod Method (String)

Searches for the public method with the specified name.

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

public MethodInfo GetMethod (
	string 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.


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 ); 
        }      
    }
}


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

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
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.