C# Type GetMethod(String, BindingFlags, Binder, CallingConventions, Type[], ParameterModifier[])
Description
Type GetMethod(String, BindingFlags, Binder, CallingConventions,
Type[], ParameterModifier[])
searches for the specified method
whose parameters match the specified argument types and modifiers, using
the specified binding constraints and the specified calling convention.
Syntax
Type.GetMethod(String, BindingFlags, Binder, CallingConventions, Type[], ParameterModifier[])
has the following syntax.
public MethodInfo GetMethod(
string name,/*from w w w. ja v a2 s.c o m*/
BindingFlags bindingAttr,
Binder binder,
CallingConventions callConvention,
Type[] types,
ParameterModifier[] modifiers
)
Example
The following example finds specific overloads of MethodA, specifying binding constraints, calling conventions, and a variety of argument types.
using System;/*www . j a v a 2 s. c om*/
using System.Reflection;
class Program
{
// Methods to get:
public void MethodA(int i, int j) { }
public void MethodA(int[] i) { }
public unsafe void MethodA(int* i) { }
public void MethodA(ref int r) {}
// Method that takes an out parameter:
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,
CallingConventions.Any,
new Type[] { typeof(int), typeof(int) },
null);
Console.WriteLine(mInfo);
// Get MethodA(int[] i)
mInfo = typeof(Program).GetMethod("MethodA",
BindingFlags.Public | BindingFlags.Instance,
null,
CallingConventions.Any,
new Type[] { typeof(int[]) },
null);
Console.WriteLine(mInfo);
// Get MethodA(int* i)
mInfo = typeof(Program).GetMethod("MethodA",
BindingFlags.Public | BindingFlags.Instance,
null,
CallingConventions.Any,
new Type[] { typeof(int).MakePointerType() },
null);
Console.WriteLine(mInfo);
// Get MethodA(ref int r)
mInfo = typeof(Program).GetMethod("MethodA",
BindingFlags.Public | BindingFlags.Instance,
null,
CallingConventions.Any,
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,
CallingConventions.Any,
new Type[] { typeof(int), typeof(int).MakeByRefType() },
null);
Console.WriteLine(mInfo);
}
}
The code above generates the following result.