有时我会遇到一些情况,我必须将一个方法附加到委托上,但签名不匹配,比如尝试将abc附加到somedelegate,字符串参数为“hi”。可以使用预定义参数将方法附加到委托吗?
public class test
{
//...
public void abc(int i, string x)
{
//Do Something
}
//...
}
public class test2
{
somedelegate x;
//...
public test2()
{
//Do Something
test y = new test();
x += y.abc(,"hi");
}
delegate void somedelegate(int i);
}
我可以通过正确的签名产生另一位代表则安装它周围的工作,但它似乎很不必要的复杂。你可以在C#中做这样的事吗?谢谢。
对。我猜想没有办法像我设想的那样做,但我仍然可以像x + =(int i)=> abc(i,“hi”);这样做。所以现在没问题,谢谢你们。
我想你应该重新考虑你想要做的事情。你正试图调用一个没有所有必需参数的方法?考虑所有的含义,错误的结果,错误等。而是重新设计你的类使用方法重载或实现[命令模式](http://en.wikipedia.org/wiki/Command_pattern) – shenku 2012-02-24 03:01:48
这被称为咖喱,是通常在功能性编程和模块化编程中非常有用。 http://stackoverflow.com/questions/36314/what-is-currying – irisjay 2016-10-28 18:17:35