MethodInfo
Объект берут из
GetMethods
или GetMethod.
Вызов -
Invoke,
параметры -
GetParameters.
Синтаксис
MethodInfo method = type.GetMethod("Run");
Пример
Давайте прочитать имя метода
Run:
using System;
using System.Reflection;
class Gate
{
public int Run(int value)
{
return value;
}
}
class Program
{
static void Main()
{
MethodInfo method = typeof(Gate).GetMethod("Run");
string res = method.Name;
Console.WriteLine(res);
}
}
Результат выполнения кода:
"Run"
Пример
Давайте вызвать метод через
Invoke:
using System;
using System.Reflection;
class Gate
{
public int Run(int value)
{
return value;
}
}
class Program
{
static void Main()
{
Gate gate = new Gate();
MethodInfo method = typeof(Gate).GetMethod("Run");
int res = (int)method.Invoke(gate, new object[] { 12345 });
Console.WriteLine(res);
}
}
Результат выполнения кода:
12345
Пример
Давайте узнать тип результата метода:
using System;
using System.Reflection;
class Gate
{
public int Run(int value)
{
return value;
}
}
class Program
{
static void Main()
{
MethodInfo method = typeof(Gate).GetMethod("Run");
bool res = method.ReturnType == typeof(int);
Console.WriteLine(res);
}
}
Результат выполнения кода:
True
Смотрите также
-
Invoke,
вызов -
GetParameters,
параметры -
ConstructorInfo,
конструктор -
GetMethods,
список методов