如何通过 var 值调用方法
How to call method by var value
我有一些 int 变量:
int i = 5
和 3 种方法:
public Method1(){}
public Method2(){}
public Method3(){}
如何使用他的名字 + 变量 i 调用此方法之一?
像这样:
Method4()
{
Method+i();
}
你可以做你要求的事情,但那真的很难看。
你可以做的是将它们全部放在一个列表或数组中,然后通过索引访问它们:
var methods = new Action[]{ null, Method1, Method2, Method3 };
methods[1](); // this would call Method1, since Method1 is in position 1 of your array.
if (i == 3)
Method3();
我有一些 int 变量:
int i = 5
和 3 种方法:
public Method1(){}
public Method2(){}
public Method3(){}
如何使用他的名字 + 变量 i 调用此方法之一? 像这样:
Method4()
{
Method+i();
}
你可以做你要求的事情,但那真的很难看。
你可以做的是将它们全部放在一个列表或数组中,然后通过索引访问它们:
var methods = new Action[]{ null, Method1, Method2, Method3 };
methods[1](); // this would call Method1, since Method1 is in position 1 of your array.
if (i == 3)
Method3();