Home > database >  How to call method by var value [duplicate]
How to call method by var value [duplicate]

Time:09-28

I have some int var:

int i = 5

and 3 methods:

public Method1(){}
public Method2(){}
public Method3(){}

How I can call one of this method using his name variable i? Something like this:

Method4()
{
   Method i();
}

CodePudding user response:

You could do what you asked for with reflection, but that's really ugly.

What you could do is have them all in a list or array and then access them by index:

var methods = new Action[]{ null, Method1, Method2, Method3 }; 

methods[1](); // this would call Method1, since Method1 is in position 1 of your array.

CodePudding user response:

        if (i == 3)
                Method3();
  • Related