【发布时间】:2013-03-16 08:30:04
【问题描述】:
public static string GetFoo() {
string source = GameInfoUtil.GetSource(repairRequest, () => {
return "0"; // this line gives error
});
.
.
MORE WORK, BUT WANT TO SKIP IT
}
public static string GetSource(WebRequest request, Action failureCallback) {
// DOING WORK HERE WITH REQUEST
if(WORK IS SUCCESSFULL) RETURN CORRECT STRING ELSE CALL ->
failureCallback();
return "";
}
我想做这样的事情,但它给了我错误:
Error 2 Cannot convert lambda expression to delegate type 'System.Action' because some of the return types in the block are not implicitly convertible to the delegate return type.
Error 1 Since 'System.Action' returns void, a return keyword must not be followed by an object expression C:\Users\Jaanus\Documents\Visual Studio 2012\Projects\Bot\Bot\Utils\GameInfoUtil.cs 58 5 Bot
我想要做的是,当GameInfoUtil.GetSource 中发生某些事情时,它会调用我的委托,并且GetFoo 方法将返回并且不会继续工作。
【问题讨论】:
-
您是否有任何理由使用回调而不是正常的异常处理?这一切看起来都相当复杂——如果你对回调不完全满意,这尤其麻烦。
标签: c# .net delegates callback