相关疑难解决方法(0)

如何在while循环中添加中断来解决过载歧义?

考虑一下这个Reactive Extensions片段(忽略它的实用性):

return Observable.Create<string>(async observable =>
{
    while (true)
    {
    }
});
Run Code Online (Sandbox Code Playgroud)

这不能使用Reactive Extensions 2.2.5(使用NuGet Rx-Main包)进行编译.它失败了:

错误1以下方法或属性之间的调用不明确:'System.Reactive.Linq.Observable.Create <string>(System.Func <System.IObserver <string>,System.Threading.Tasks.Task <System.Action> >)'和'System.Reactive.Linq.Observable.Create <string>(System.Func <System.IObserver <string>,System.Threading.Tasks.Task>)'

但是,break在while循环中添加任何位置可以修复编译错误:

return Observable.Create<string>(async observable =>
{
    while (true)
    {
        break;
    }
});
Run Code Online (Sandbox Code Playgroud)

这个问题可以在没有Reactive Extensions的情况下重现(如果你想在没有摆弄Rx的情况下尝试它,会更容易):

class Program
{
    static void Main(string[] args)
    {
        Observable.Create<string>(async blah =>
        {
            while (true)
            {
                Console.WriteLine("foo.");
                break; //Remove this and the compiler will break
            }
        });
    }
}

public class Observable
{
    public static IObservable<TResult> Create<TResult>(Func<IObserver<TResult>, Task> subscribeAsync)
    { …
Run Code Online (Sandbox Code Playgroud)

c#

48
推荐指数
2
解决办法
2740
查看次数

标签 统计

c# ×1