C# Type GetMethod(String, Type[])

Description

Type GetMethod(String, Type[]) searches for the specified public method whose parameters match the specified argument types.

Syntax

Type.GetMethod(String, Type[]) has the following syntax.


public MethodInfo GetMethod(
  string name,
  Type[] types
)

Parameters

Type.GetMethod(String, Type[]) has the following parameters.

  • name - The string containing the name of the public method to get.
  • types - An array of Type objects representing the number, order, and type of the parameters for the method to get.
  • types - -or-
  • types - An empty array of Type objects (as provided by the EmptyTypes field) to get a method that takes no parameters.

Returns

Type.GetMethod(String, Type[]) method returns An object representing the public method whose parameters match the specified argument types, if found; otherwise, null.

Example

The following example finds specific overloads of MethodA, specifying a variety of argument types.


using System;/* ww w . j av a 2s  .c  o m*/
using System.Reflection;

class Program
{
    public void MethodA(int i, int j) { }

    public void MethodA(int[] i) { }

    public unsafe void MethodA(int* i) { }

    public void MethodA(ref int r) {}

    public void MethodA(int i, out int o) { o = 100;}


  static void Main(string[] args)
  {
    MethodInfo mInfo;

    // Get MethodA(int i, int i)
    mInfo = typeof(Program).GetMethod("MethodA",new Type[] { typeof(int), typeof(int) });
    Console.WriteLine("Found method: {0}", mInfo);

    // Get MethodA(int[] i)
    mInfo = typeof(Program).GetMethod("MethodA",new Type[] { typeof(int[]) });
    Console.WriteLine("Found method: {0}", mInfo);

    // Get MethodA(int* i)
    mInfo = typeof(Program).GetMethod("MethodA",new Type[] { typeof(int).MakePointerType() });
    Console.WriteLine("Found method: {0}", mInfo);

    // Get MethodA(ref int r)
    mInfo = typeof(Program).GetMethod("MethodA",new Type[] { typeof(int).MakeByRefType() });
    Console.WriteLine("Found method: {0}", mInfo);

    // Get MethodA(int i, out int o)
    mInfo = typeof(Program).GetMethod("MethodA",new Type[] { typeof(int), typeof(int).MakeByRefType() });
    Console.WriteLine("Found method: {0}", mInfo);

  }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System »




Array
BitConverter
Boolean
Byte
Char
Console
ConsoleKeyInfo
Convert
DateTime
DateTimeOffset
Decimal
Double
Enum
Environment
Exception
Guid
Int16
Int32
Int64
Math
OperatingSystem
Random
SByte
Single
String
StringComparer
TimeSpan
TimeZone
TimeZoneInfo
Tuple
Tuple
Tuple
Type
UInt16
UInt32
UInt64
Uri
Version