We want to call the Inform static method by using the string "Inform" and we want to call it twice with different parameters.
using System;
using System.Reflection;
static class Methods
{
public static void Inform(string parameter)
{
Console.WriteLine("Inform:parameter={0}", parameter);
}
}
class Program
{
static void Main()
{
// Name of the method we want to call.
string name = "Inform";
// Call it with each of these parameters.
string[] parameters = { "Raj", "Test" };
// Get MethodInfo.
Type type = typeof(Methods);
MethodInfo info = type.GetMethod(name);
// Loop over parameters.
foreach (string parameter in parameters)
{
info.Invoke(null, new object[] { parameter });
}
}
}Output
Inform:parameter=Raj
Inform:parameter=Test
