Click here to Skip to main content
15,905,508 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi, For example I have 100 methods and the methods names so:
method1(), method2(), method3() ..... method100()

I want to returning my methods 1 to 100


C#
public static returningMethod()
{

   for(i=0;i<100;i++)
   {
      //How can I return the metods ??
   }

}
Posted
Updated 16-Jun-15 22:49pm
v2

Use reflection

http://www.c-sharpcorner.com/Blogs/8856/getting-all-methods-of-class-using-reflection-C-Sharp.aspx[^]

http://stackoverflow.com/questions/2202381/reflection-how-to-invoke-method-with-parameters[^]

However what you're trying to do is normally a result of bad application design that should be re-thought.
 
Share this answer
 
Comments
CPallini 17-Jun-15 4:52am    
5.
Use Reflection

XML
public void Invoke<T>(string methodName) where T : new()
{
    T instance = new T();
    MethodInfo method = typeof(T).GetMethod(methodName);
    method.Invoke(instance, null);
}
 
Share this answer
 
C#
using System.Reflection;
namespace linqpractice
{

  class MyClass
    {

        public static int method1()
        {
            return 1;
        }
        public static string method2()
        {
            return "Hello";
        }
        public static double method3()
        {
            return 1.5;
        }
    }
 class Program
    {
      
        static void Main(string[] args)
        {


            //linqpractice namespace
            //MyClass is the class where method1(),method2() and method3() are present
            Type calledType = Type.GetType("linqpractice.MyClass");
            ArrayList ob = new ArrayList();
            for (int i = 1; i <= 3; i++)
            {
                ob.Add(calledType.InvokeMember(
                             "method" + i.ToString(),
                             BindingFlags.InvokeMethod | BindingFlags.Public |
                                 BindingFlags.Static,
                             null,
                             null,
                             null));

            }
            //printing the returns value of the methods
            foreach (var item in ob)
            {
                Console.WriteLine(item); 
            }
            
           
        }
    }



}
 
Share this answer
 

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