C# Type GetMethod(String, BindingFlags, Binder, Type[], ParameterModifier[])

Description

Type GetMethod(String, BindingFlags, Binder, Type[], ParameterModifier[]) searches for the specified method whose parameters match the specified argument types and modifiers, using the specified binding constraints.

Syntax

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


public MethodInfo GetMethod(
  string name,//w  w w  .  j  a v a  2  s  . c  om
  BindingFlags bindingAttr,
  Binder binder,
  Type[] types,
  ParameterModifier[] modifiers
)

Returns

Type.GetMethod(String, BindingFlags, Binder, Type[], ParameterModifier[]) method returns An object representing the method that matches the specified requirements, if found; otherwise, null.

Example

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


using System;//from w  w  w .j  ava  2  s  .  co  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 j)
    mInfo = typeof(Program).GetMethod("MethodA",
        BindingFlags.Public | BindingFlags.Instance,
        null,
        new Type[] { typeof(int), typeof(int) },
        null);
    Console.WriteLine(mInfo);

    // Get MethodA(int[] i)
    mInfo = typeof(Program).GetMethod("MethodA",
        BindingFlags.Public | BindingFlags.Instance,
        null,
        new Type[] { typeof(int[]) },
        null);
    Console.WriteLine(mInfo);

    // Get MethodA(int* i)
    mInfo = typeof(Program).GetMethod("MethodA",
        BindingFlags.Public | BindingFlags.Instance,
        null,
        new Type[] { typeof(int).MakePointerType() },
        null);
    Console.WriteLine(mInfo);

    // Get MethodA(ref int r)
    mInfo = typeof(Program).GetMethod("MethodA",
        BindingFlags.Public | BindingFlags.Instance,
        null,
        new Type[] { typeof(int).MakeByRefType() },
        null);
    Console.WriteLine(mInfo);

    // Get MethodA(int i, out int o)
    mInfo = typeof(Program).GetMethod("MethodA",
        BindingFlags.Public | BindingFlags.Instance,
        null,
        new Type[] { typeof(int), typeof(int).MakeByRefType() },
        null);
    Console.WriteLine(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