C# mono的逆变编译异常

C# mono的逆变编译异常,c#,mono,C#,Mono,我试图在MSDN()中编译协方差/反方差示例 //分配兼容性。 string str=“test”; //将派生类型较多的对象指定给派生类型较少的对象。 对象obj=str; //协方差。 IEnumerable strings=新列表(); //使用更派生的类型参数实例化的对象 //分配给使用派生类型参数较少的对象实例化。 //保留分配兼容性。 IEnumerable对象=字符串; //相反。 //假设类中存在以下方法: //静态void SetObject(对象o){} Action act

我试图在MSDN()中编译协方差/反方差示例

//分配兼容性。
string str=“test”;
//将派生类型较多的对象指定给派生类型较少的对象。
对象obj=str;
//协方差。
IEnumerable strings=新列表();
//使用更派生的类型参数实例化的对象
//分配给使用派生类型参数较少的对象实例化。
//保留分配兼容性。
IEnumerable对象=字符串;
//相反。
//假设类中存在以下方法:
//静态void SetObject(对象o){}
Action actObject=SetObject;
//用派生较少的类型参数实例化的对象
//被分配给使用更派生的类型参数实例化的对象。
//分配兼容性是反向的。
Action actString=actObject;
问题是我有两个错误:

例1.cs(22,39):

错误CS0266:无法隐式转换类型
System.Collections.Generic.IEnumerable
to
System.Collections.Generic.IEnumerable
。 存在显式转换(是否缺少强制转换?)

例1.cs(40,36):

错误CS0029:无法将类型
System.Action
隐式转换为
System.Action

我可以通过强制转换解决第一个问题:“IEnumerable objects=(IEnumerable)strings;”,但我不知道如何解决第二个问题。基本上,我不知道为什么我在编译MSDN代码时出错


我使用mono
mono C#编译器版本3.12.0.0
进行编译。可能有什么问题

问题是我使用了
gmcs
编译器,当我用
dmcs
mcs
编译代码时,代码编译得很好

从mono页面()

我认为这与和/或事实有关,
IEnumerable
不是从您的第一个示例的
IEnumerable
派生的。一个简单的
var objects=strings.Cast().Tolist()
可能会修复您的第一个示例。此功能是在C#4、.NET 4.0中添加的,在以前的版本中会产生这些错误。您的Mono版本是针对4.0功能还是更早的功能?
// Assignment compatibility.  
string str = "test";
// An object of a more derived type is assigned to an object of a less derived type.  
object obj = str;

// Covariance. 
IEnumerable<string> strings = new List<string>();
// An object that is instantiated with a more derived type argument  
// is assigned to an object instantiated with a less derived type argument.  
// Assignment compatibility is preserved. 
IEnumerable<object> objects = strings;

// Contravariance.            
// Assume that the following method is in the class:  
// static void SetObject(object o) { } 
Action<object> actObject = SetObject;
// An object that is instantiated with a less derived type argument  
// is assigned to an object instantiated with a more derived type argument.  
// Assignment compatibility is reversed. 
Action<string> actString = actObject;
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.