我有一个基于输入参数的方法。现在,我想通过在委托的调用列表中添加不同的参数来调用该方法。
在不多次调用委托的情况下可以做到吗?
private delegate void myDel(int a);
private myDel del;
public MainWindow()
{
InitializeComponent();
del = delmethod;
}
private void delmethod(int a)
{
//Do something
}
private void call_methods()
{
del(1);
del(2);
del(3);
}这是正确的方式还是我还有其他选择?请注意,我可能希望使用循环传递许多这样的参数。
虽然我提到了here,但它们只为调用不同的方法提供了解决方案,但我想要的是相同的方法。
编辑:
我希望通过根据参数调用方法将数据从一个数据库传输到另一个数据库
环境: Windows,.Net 3.5
发布于 2011-05-27 08:56:00
将
添加到委托的调用列表中,但使用不同的参数。
可能但很丑。你最后的下场是:
del += (x) => delmethod(1);
del += (x) => delmethod(2);
del += (x) => delmethod(3);
del(-1); // Note the -1 is not used发布于 2011-05-27 08:46:52
我会去
private void delmethod(params int[] abc)
{
//Do something for each item in abc
}
private void call_methods()
{
del(1,2,3); //you can call with any number of parameters
} https://stackoverflow.com/questions/6149775
复制相似问题