2015-02-06 93 views
2

我试图编译MSDN中的协变/反变化示例(https://msdn.microsoft.com/en-us/library/ee207183.aspx)。单变量编译异常

​​

的问题是,我得到了两个错误:

Example1.cs(22,39):

错误CS0266:可以输入无法隐式转换 System.Collections.Generic.IEnumerable<string>System.Collections.Generic.IEnumerable<object>。 的显式转换存在(是否缺少强制转换?)

Example1.cs(40,36):

错误CS0029:无法隐式转换类型System.Action<object>System.Action<string>

我可以通过强制转换来解决第一个问题:'IEnumerable objects =(IEnumerable)strings'',但我不确定如何解决第二个问题。基本上,我不知道为什么我从编译MSDN代码中得到错误。

我使用mono Mono C# compiler version 3.12.0.0进行编译。什么可能是错的?

+0

我觉得这与[Liskov的Substituion原理](http://en.wikipedia.org/wiki/Liskov_substitution_principle)和/或事实'IEnumerable '不是来自'IEnumerable '的第一个例子。一个简单的'var objects = strings.Cast ().Tolist()'可能会修复你的第一个例子。 – 2015-02-06 22:41:24

+1

此功能与C#4,.NET 4.0一起添加,并会在以前的版本中产生这些错误。您的Mono是否瞄准4.0或更早版本的功能? – 2015-02-06 22:43:37

回答

2

问题是我用gmcs编译器,当我用dmcsmcs编译代码时,代码编译得很好。

从单页(http://www.mono-project.com/docs/about-mono/languages/csharp/

gmcs: compiler to target the 2.0 mscorlib. 
smcs: compiler to target the 2.1 mscorlib, to build Moonlight applications. 
dmcs: compiler to target the 4.0 mscorlib. 

Starting with Mono version 2.11 a new unified compiler mcs is available. 
It replaces all previous runtime specific compilers (gmcs, dmcs, smcs). 
They still exist (as scripts only) to ease the migration path to mcs 
but we strongly recommend to use mcs.