使用csc.exe编译时,编译器错误等待IObservable [英] Compiler error awaiting IObservable when compiled with csc.exe

查看:96
本文介绍了使用csc.exe编译时,编译器错误等待IObservable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下程序:


使用System;

使用System.Collections.Generic;

使用System。 Linq;

使用System.Reactive.Linq;

使用System.Threading.Tasks;



命名空间ConsoleApplication1

{

    课程计划

     {

         static void Main(string [] args)

         {

             Foo()。等等();

        }
$


         static async Task Foo()

         {

             string hello = await Observable.Return(" Hello,world");

             Console.WriteLine(你好);

        }

    }

}


在Visual Studio 2012中编译并运行完美。但是如果您尝试通过以下方式编译相同的程序CSharpCodeProvider或命令行编译器:


C:\ Windows \ Microsoftoft.NET\Framework\v4.0.30319\csc /r:System.Reactive .Interfaces.dll /r:System.Reactive.Core.dll /r:System.Reactive.Linq.dll /r:System.Reactive.PlatformServices.dll /r:C:\Windows\Microsoft.NET\Framework \v4.0.30319\System.Runtime.dll
Program.cs


编译器生成以下错误:


Program.cs(18,19):错误CS4027:'System.Reactive.Subjects.AsyncSubject< string>'未实现'System.Runtime.CompilerServices.INotifyCompletion'


我在Windows 7和Windows 8计算机上都试过这个。显然,已经安装了Framework 4.5,并且已经为C#5成功修补了编译器,因为它识别了async和await关键字。 Rx组件来自Rx-Main,v2.0.20823。
在Reflector中打开System.Reactive.Linq.dll显示AsyncSubject<>类型确实实现了System.Runtime.CompilerServices.INotifyCompletion。


可能导致这种情况,是否有解决方法? FWIW,它没有在带有异步CTP的Framework 4.0中发生。


Joe


  P.S。您可以通过等待LINQPad中的observable来重现这一点(LINQPad依赖于CSharpCodeProvider,它依赖于csc.exe)。




$ b

解决方案

对于遇到此问题的其他人,添加对 System.Threading.Tasks.dll 的引用修复了它。


System.Threading.Tasks.dll 是一个未记录的程序集,由
引用 System.Reactive.Linq.dll 。它只包含类型转发属性(没有类型)。如果您没有显式引用此程序集以及System.Runtime.dll,则不会发生类型转发优点,并且您将获得无用的编译器
错误。


我不明白为什么在Visual Studio中创建的项目不需要这些引用。据我所知,VS使用的是msbuild而不是csc.exe,所以可能会有一些自动化。▼





The following program:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reactive.Linq;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main (string [] args)
        {
            Foo ().Wait();
        }

        static async Task Foo()
        {
            string hello = await Observable.Return ("Hello, world");
            Console.WriteLine (hello);
        }
    }
}

compiles and runs perfectly in Visual Studio 2012. But if you try and compile the same program via the CSharpCodeProvider or the command-line compiler:

C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc /r:System.Reactive.Interfaces.dll /r:System.Reactive.Core.dll /r:System.Reactive.Linq.dll /r:System.Reactive.PlatformServices.dll /r:C:\Windows\Microsoft.NET\Framework\v4.0.30319\System.Runtime.dll Program.cs

the compiler generates the following error:

Program.cs(18,19): error CS4027: 'System.Reactive.Subjects.AsyncSubject<string>' does not implement 'System.Runtime.CompilerServices.INotifyCompletion'

I've tried this on both Windows 7 and Windows 8 machines. Obviously, Framework 4.5 is installed, and the compiler has been successfully patched for C# 5, because it recognizes the async and await keywords. The Rx assemblies are from Rx-Main, v2.0.20823. Opening System.Reactive.Linq.dll in Reflector shows that the AsyncSubject<> type indeed implements System.Runtime.CompilerServices.INotifyCompletion.

What could cause this, and is there a workaround? FWIW, it didn't happen in Framework 4.0 with the async CTP.

Joe

 P.S. You can reproduce this by awaiting an observable in LINQPad (LINQPad relies on CSharpCodeProvider, which relies on csc.exe).



解决方案

For anyone else running into this problem, adding a reference to System.Threading.Tasks.dll fixes it.

System.Threading.Tasks.dll is an undocumented assembly that's referenced by System.Reactive.Linq.dll. It contains just type forwarding attributes (and no types). If you don't explicitly reference this assembly, along with System.Runtime.dll, the type forwarding goodness doesn't occur and you get the unhelpful compiler error.

What I don't understand is why a project created in Visual Studio doesn't require these references. As I understand, VS uses msbuild rather than csc.exe, so presumably there's some automagic going on there.



这篇关于使用csc.exe编译时,编译器错误等待IObservable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆