class ReflectMethod {
public static class Methods {
public static void Bye(string parameter) => Console.WriteLine("Bye:parameter={0}", parameter);
public static void Hello(string parameter) => Console.WriteLine("Hello:parameter={0}", parameter);
public static void Welcome(string parameter) => Console.WriteLine("Welcome:parameter={0}", parameter);
}
public static void InvokeMethod(string[] array) {
Type instance = typeof(Methods);
MethodInfo[] methods = instance.GetMethods();
for (int counter = 0; counter < 1; counter++) {
foreach (MethodInfo method in methods) {
method.Invoke(instance, null);
}
}
}
}
class ReflectMethod
{
public static class Methods
{
public static void Bye(string parameter) => Console.WriteLine("Bye:parameter={0}", parameter);
public static void Hello(string parameter) => Console.WriteLine("Hello:parameter={0}", parameter);
public static void Welcome(string parameter) => Console.WriteLine("Welcome:parameter={0}", parameter);
}
public static void InvokeMethod(string[] array)
{
Type instance = typeof(Methods);
MethodInfo[] methods = instance.GetMethods(BindingFlags.Static | BindingFlags.Public);
var currentArgNumber = 0;
foreach (MethodInfo method in methods)
{
method.Invoke(instance, new[] { array[currentArgNumber++] });
}
}
}
public interface IMessage
{
void SendMessage(string msg);
}
public abstract class MessageBase : IMessage
{
public virtual void SendMessage(string msg) => Console.WriteLine($@"{Name}:parameter={msg}");
public abstract string Name { get; }
}
public class Bye : MessageBase {public override string Name => "Bye"; }
public class Hello : MessageBase {public override string Name => "Hello"; }
public class Welcome : MessageBase { public override string Name => "Welcome";}
class ReflectMethod
{
public static void InvokeMethod(string[] array)
{
List<IMessage> messages = new List<IMessage>()
{
new Bye(),
new Hello(),
new Welcome()
};
for (int counter = 0; counter < array.Length; counter++)
{
messages[counter].SendMessage(array[counter]);
}
}
}