0

In my WPF MVVM project I have to execute some 10 methods in one go without passing individual name one by one. Lets say I can do this for a single method call:

CallStaticMethod("MainApplication.Test", "Test1");

its body:

public static void CallStaticMethod(string typeName, string methodName)
{
    var type = Type.GetType(typeName);

    if (type != null)
    {
        var method = type.GetMethod(methodName);

        if (method != null)
        {
            method.Invoke(null, null);
        }
    }
}

public static class Test
{
    public static void Test1()
    {
        Console.WriteLine("Test invoked");
    }
}

But I have the requirement:

public static class Test
{
    public static void Test1()
    {
        Console.WriteLine("Test invoked");
    }

    public static void Test2()
    {
        Console.WriteLine("Test invoked");
    }

    public static void Test3()
    {
        Console.WriteLine("Test invoked");
    }

    public static void CallAllTestMethod()
   {
       Test1();
       Test2();
       Test3();
   }
}

Now I want to call this CallAllTestMethod() and also want to know which method (Test1,Test2 or Test3) is currently being processed. Any thought on this?

Tobias
  • 232
  • 1
  • 16
user2519971
  • 345
  • 3
  • 12
  • 27

2 Answers2

0

You kind of try to do this. As stated in that thread it's pretty difficult to do so.

Why don't you create an event which gets fired by each of the test methods? You can then register to those events using reflection.

Community
  • 1
  • 1
Jan Rothkegel
  • 737
  • 3
  • 21
  • you can use a list of delegates: var actions = new List>(); actions.Add(this.GetReports); actions.Add(this.GetUsers); actions.Add(this.GetEmployees); And calling them is easy: foreach (var action in actions) { action(new[] { "All" }); } var actions = new List(); actions.Add(() => this.GetReports(new[] { "Foo" })); actions.Add(() => this.GetUsers(new[] { "Bar" })); // etc foreach (var action in actions) { action(); } – user2519971 Sep 05 '13 at 09:00
0

you can use a list of delegates: var actions = new List>(); actions.Add(this.GetReports); actions.Add(this.GetUsers); actions.Add(this.GetEmployees); And calling them is easy: foreach (var action in actions) { action(new[] { "All" }); } var actions = new List(); actions.Add(() => this.GetReports(new[] { "Foo" })); actions.Add(() => this.GetUsers(new[] { "Bar" })); // etc foreach (var action in actions) { action(); }

user2519971
  • 345
  • 3
  • 12
  • 27