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()
- {
-
- string name = "Inform";
-
- string[] parameters = { "Raj", "Test" };
-
- Type type = typeof(Methods);
- MethodInfo info = type.GetMethod(name);
-
- foreach (string parameter in parameters)
- {
- info.Invoke(null, new object[] { parameter });
- }
- }
- }
Output
Inform:parameter=Raj
Inform:parameter=Test