C# 检查internet连接:IEnumerator带有lambda表达式错误
我在这段代码中得到了一个编译时错误,说明如下:C# 检查internet连接:IEnumerator带有lambda表达式错误,c#,C#,我在这段代码中得到了一个编译时错误,说明如下: Cannot convert `lambda expression' to delegate type `System.Action<bool>' because some of the return types in the block are not implicitly convertible to the delegate return type 无法将'lambda expression'转换为委托类型'System.Act
Cannot convert `lambda expression' to delegate type `System.Action<bool>' because
some of the return types in the block are not implicitly convertible to the delegate
return type
无法将'lambda expression'转换为委托类型'System.Action',因为
块中的某些返回类型不能隐式转换为委托
返回类型
以下是我的方法。我希望能够返回真或假,这取决于是否有互联网连接。我该怎么做呢
private bool IsAbleToCallMethods () {
StartCoroutine(ConnectedToInternet((isConnected)=>{
if (isConnected) {
return true;
} else {
return false;
}
}));
}
private IEnumerator ConnectedToInternet(Action<bool> action) {
// Issue a request to google and see if the request returned
// any errors or not, this will determine if you were able
// to reach google via an internet connection
WWW www = new WWW("http://google.com");
yield return www;
if (www.error != null) {
action (false);
} else {
action (true);
}
}
private bool IsAbleToCallMethods(){
启动例行程序(已连接的点网络)((未连接)=>{
如果(未连接){
返回true;
}否则{
返回false;
}
}));
}
专用IEnumerator连接点网络(操作){
//向google发出请求,看看请求是否返回
//无论是否有任何错误,这将决定您是否能够
//通过互联网连接到谷歌
WWW=新WWW(“http://google.com");
收益率;
如果(www.error!=null){
行动(假);
}否则{
行动(真实);
}
}
操作
不会返回任何内容。您需要改用Func
。第一个bool
是参数,最后一个参数是返回类型
private IEnumerator ConnectedToInternet(Func<bool,bool> action)
private IEnumerator connectedPointerNet(函数操作)
您正在测试是否(已连接)
,但是已连接
不能以这种方式进行测试,因为它不返回bool
,它返回void
,如果您想要返回除void
以外的任何内容的委托,则其类型应为Func
,在这种情况下,如果您希望一个代理接受一个bool
并返回一个bool
,那么它应该是Func
看看这个:谢谢你的链接,解释得很好。