Type.GetMethod Method (String, Type())
Searches for the specified public method whose parameters match the specified argument types.
Assembly: mscorlib (in mscorlib.dll)
Parameters
- name
-
Type:
System.String
The string containing the name of the public method to get.
- types
-
Type:
System.Type()
An array of Type objects representing the number, order, and type of the parameters for the method to get.
-or-
An empty array of Type objects (as provided by the EmptyTypes field) to get a method that takes no parameters.
Return Value
Type: System.Reflection.MethodInfoAn object representing the public method whose parameters match the specified argument types, if found; otherwise, null.
Implements
_Type.GetMethod(String, Type())| Exception | Condition |
|---|---|
| AmbiguousMatchException | More than one method is found with the specified name and specified parameters. |
| ArgumentNullException | name is null. -or- types is null. -or- One of the elements in types is null. |
| ArgumentException | types is multidimensional. |
The search for name is case-sensitive. The search includes public static and public instance methods.
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 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.
Note |
|---|
The name parameter cannot include type arguments. For example, the C# code GetMethod("MyGenericMethod<int>") searches for a method with the text name "MyGenericMethod<int>", rather than for a method named MyGenericMethod that has one generic argument of type int. Instead, use GetMethod("MyGenericMethod") with the appropriate parameter in the types array. |
The following example finds specific overloads of MethodA, specifying a variety of argument types.
Note |
|---|
The Visual C# 2005 example requires the /unsafe compiler option. |
Imports System Imports System.Reflection Imports System.Runtime.InteropServices Class Program ' Methods to get: Public Overloads Sub MethodA(ByVal i As Integer, ByVal l As Long) End Sub Public Overloads Sub MethodA(ByVal i() As Integer) End Sub Public Overloads Sub MethodA(ByRef r As Integer) End Sub ' Method that takes an out parameter. Note that an Imports ' reference is needed to System.Runtime.InteropServices ' for the <OutAttribute>, which can be shortened to <Out>. Public Overloads Sub MethodA(ByVal i As Integer, <Out()> ByRef o As Integer) o = 100 End Sub Public Shared Sub Main(ByVal args() As String) Dim mInfo As MethodInfo ' Get MethodA(i As Integer i, l As Long) mInfo = GetType(Program).GetMethod("MethodA", New Type() {GetType(Integer), GetType(Long)}) Console.WriteLine("Found method: {0}", mInfo) ' Get MethodA(i As Integer()) mInfo = GetType(Program).GetMethod("MethodA", New Type() {GetType(Integer())}) Console.WriteLine("Found method: {0}", mInfo) ' Get MethodA(ByRef r As Integer) mInfo = GetType(Program).GetMethod("MethodA", New Type() {GetType(Integer).MakeByRefType}) Console.WriteLine("Found method: {0}", mInfo) ' Get MethodA(i As Integer, ByRef r As Integer) mInfo = GetType(Program).GetMethod("MethodA", New Type() {GetType(Integer), _ GetType(Integer).MakeByRefType}) Console.WriteLine("Found method: {0}", mInfo) End Sub End Class
The following example retrieves MethodInfo objects that represent the Add methods of a non-generic type (the ArrayList class), an open generic type (the List(Of T) class), and a closed generic type (the List(Of String) type.
Imports System.Collections Imports System.Collections.Generic Imports System.Reflection Module Example Public Sub Main() ' Get a Type object that represents a non-generic type. GetAddMethod(GetType(ArrayList)) Dim list As New List(Of String)() ' Get a Type object that represents a constructed generic type. Dim closed As Type = list.GetType() GetAddMethod(closed) ' Get a Type object that represents an open generic type. Dim open As Type = GetType(List(Of)) GetAddMethod(open) End Sub Private Sub GetAddMethod(typ As Type) Dim method As MethodInfo ' Determine if this is a generic type. If typ.IsGenericType Then ' Is it an open generic type? If typ.ContainsGenericParameters Then method = typ.GetMethod("Add", typ.GetGenericArguments()) ' Get closed generic type arguments. Else method = typ.GetMethod("Add", typ.GenericTypeArguments) End If ' This is not a generic type. Else method = typ.GetMethod("Add", { GetType(Object) } ) End If ' Test if an Add method was found. If method Is Nothing Then Console.WriteLine("No Add method found.") Exit Sub End If Dim t As Type = method.ReflectedType Console.Write("{0}.{1}.{2}(", t.Namespace, t.Name, method.Name) Dim params() As ParameterInfo = method.GetParameters() For ctr As Integer = 0 To params.Length - 1 Console.Write("{0}{1}", params(ctr).ParameterType.Name, If(ctr < params.Length - 1, ", ", "")) Next Console.WriteLine(")") End Sub End Module ' The example displays the following output: ' System.Collections.ArrayList.Add(Object) ' System.Collections.Generic.List`1.Add(String) ' System.Collections.Generic.List`1.Add(T)
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0

The example defines a GetAddMethod method that retrieves the appropriate MethodInfo object. To provide the types argument for an open generic type, it calls the Type.GetGenericArguments method. To provide the types argument for a closed generic type, it retrieves the value of the Type.GenericTypeArguments property.