在C#中可以将函数作为参数传递吗?我可以使用Func或Action类来实现,但这迫使我一次声明整个函数签名。当我尝试使用Delegate时,我得到一个编译错误,说它不能将一个方法组转换成一个Delegate。
我正在开发Axial,并尝试允许用户调用web服务。我想要的是能够创建Visual Studio代理类,然后传入生成的函数。函数签名并不重要,因为生成的代码只使用函数名。但是,出于两个原因,我希望传递函数而不是名称:能够使用代理的Url属性;如果web服务不存在或在Visual Studio中更新,则会出现编译器错误。
public void AlertIt(object o) {
Axial.DOM.Window.Alert(o.ToString());
}
public void CallAddService() {
object[] param = new object[] { int.Parse(txtA.Text), int.Parse(txtB.Text) };
Axial.ServerScript.CallWebService(new WSProxy.WS().Add, param, AlertIt, AlertIt);
}
class Axial.ServerScript {
public void CallWebService(Delegate method, object[] param, Action<object> successCallback, Action<object> failureCallback) {
// translate to javascript (already working)
}
}
https://stackoverflow.com/questions/380198
复制相似问题