我有一些使用Tuples的C#代码:
public class Test {
static void Main() {
Tuple<int, int> t = Tuple.Create(0, 1);
}
}我尝试使用以下命令编译
mcs -debug+ -o Test.exe Test.cs但是它给出了错误
Test.cs(3,9): error CS0246: The type or namespace name `Tuple' could not be found. Are you missing a using directive or an assembly reference?
Compilation failed: 1 error(s), 0 warnings我认为它可能是在尝试针对缺少元组的旧版本的mscorlib进行编译。查看手册页,您似乎使用-sdk:4指定了版本,但这也不起作用:
$ mcs -sdk:4 Test.cs
Unhandled Exception: System.TypeLoadException: Type 'System.Dynamic.BinaryOperationBinder' not found in assembly 'System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' (后跟堆栈跟踪)。
我正在运行:
$ mcs --version
Mono C# compiler version 2.10.8.1在Ubuntu Precise上。根据文档,Mono从2.8版本开始支持.NET 4.0,特别是supports System.Tuple,所以这不应该是问题所在。
如何编译使用Tuple的代码?
发布于 2012-11-18 15:58:46
我预计它在mcs上会失败,但在dmcs上可以工作。我刚刚在Windows2.10.9上安装了Mono2.10.9,这是我对你的代码(包括顶部的using System; )的结果:
c:\Users\Jon\Test>mcs Test.cs
Test.cs(4,9): error CS0246: The type or namespace name `Tuple' could not be
found. Are you missing a using directive or an assembly reference?
Compilation failed: 1 error(s), 0 warnings
c:\Users\Jon\Test>dmcs Test.cs
Test.cs(4,25): warning CS0219: The variable `t' is assigned but its value is
never used
Compilation succeeded - 1 warning(s)不同之处在于dmcs默认使用框架v4,而mcs使用v2。您只需指定v4框架即可使其与mcs一起工作:
mcs -sdk:4 Test.cs试一试,还要仔细检查一下,当你使用dmcs时,你真的遇到了同样的问题。如果您看到它不是一个干净的编译,但没有注意到它是一个不同的消息,我不会感到惊讶。
https://stackoverflow.com/questions/13436784
复制相似问题