Click here to Skip to main content
15,890,336 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have list of function to execute and I want to use loop to do it,
I dont how to this , can you avise me?
I thoght to insert the names of mt functions to arr and than run in loop
something like this
string[] s1 = new string[3] {"func1", "func2", "func3"};
for(int i=0;i<s1.lengh;i++)>
here I want to call the function ... how ca I do it?

Do you have better offer?
Thanks.
Posted

Hello,

Please have a look at Dynamically Invoke A Method, Given Strings with Method Name and Class Name[^] article available here on CodeProject.

Regards,
 
Share this answer
 
Comments
Member 10304617 31-Oct-13 9:18am    
What I need to send to the parameter - typeName?
Prasad Khandekar 31-Oct-13 9:36am    
Nope, Just an array of parameters in sequence as per the definition of the method being called.
Member 10304617 31-Oct-13 9:40am    
// Get the Type for the class
Type calledType = Type.GetType(typeName);

I dont know what to send him...
he is null and break the running...
Prasad Khandekar 31-Oct-13 9:49am    
typeName is the name of the class.
Member 10304617 31-Oct-13 9:52am    
Yes , I wrote this but the value of calledType is null...
This seems like a really bad use of reflection, its important to note that reflection is slow and if you spell a name wrong or change a function name you will get errors.

A better idea is to use a delegate, here is an example (this is not tested, but you get the idea):

C#
class TestClass
{
    delegate void FunctionTemplate();

    void Function1()
    {
        //Do whatever here
    }

    void Function2()
    {
        //Do whatever here
    }

    void Function3()
    {
        //Do whatever here
    }

    public void LoopFunctions(int times)
    {
        FunctionTemplate[] array = new FunctionTemplate[] { Function1, Function2, Function3 };
    
        for(int i = 0; i < times; i++)
            array[i]();
    }
}


This is very simple, not over-engineered with anonymous methods or using slow reflection, plus if you rename Function1 to BlowUpMars it will rename it in the array and wherever else you are using it.
 
Share this answer
 
Comments
johannesnestler 31-Oct-13 10:26am    
This is what I would have done... so I like it ;-)
Member 10304617 31-Oct-13 11:44am    
Thanks
Example invocation static methods:

C#
void Main()
{
  var methods = typeof(MyFuncClass)
    .GetMethods(BindingFlags.Static | BindingFlags.Public)
    .Select(x => new 
	  { 
	    Name = x.Name, 
	    Action = new Action(() => x.Invoke(null, null))
	  })
    .ToList();
    
  foreach (var method in methods)
  {
    Console.WriteLine("Executing {0}:", method.Name);
    method.Action.Invoke();
  }
}

class MyFuncClass
{
  public static void DoStuff()
  {
    Console.WriteLine("DoStuff result");
  }
  public static void DoAnotherStuff()
  {
    Console.WriteLine("DoAnotherStuff result");
  }
}
 
Share this answer
 
v3
Comments
Member 10304617 31-Oct-13 9:32am    
But my functions are public and I can not convert them to static method
Member 10304617 31-Oct-13 11:44am    
Thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900