如何在Mono中使用来自NuGet的软件包构建C#程序?

10
我正在使用Ubuntu 17.10上的Mono 5.4.1.7编写C#代码。
以下是我想要做的事情,全部都需要通过命令行完成:
  1. 使用NuGet安装一个包(具体来说是MathNet.Numerics)。
  2. 编译使用该包中类的C#程序。
  3. 运行程序。
但我没有找到轻松的方法来实现这一点,所以我肯定是错过了什么。
这是我尝试过的。我为我的程序创建了一个名为'foo'的目录。在该目录中,我运行了以下命令:
$ nuget install MathNet.Numerics

我下载了MathNet.Numerics库,并将其放在名为'MathNet.Numerics.3.20.2'的子目录中。到目前为止,一切都很好。

然后,我创建了我的测试程序foo.cs,内容如下:

using MathNet.Numerics.LinearAlgebra;
using MathNet.Numerics.LinearAlgebra.Double;

class Foo {
  static void Main() {
    Vector<double> A = DenseVector.OfArray(new double[] { 1, 2, 3, 4 });
  }
}

但是现在我不能仅仅使用mcs来构建:

$ mcs Foo.cs
foo.cs(1,7): error CS0246: The type or namespace name `MathNet' could not be found. Are you missing an assembly reference?
foo.cs(2,7): error CS0246: The type or namespace name `MathNet' could not be found. Are you missing an assembly reference?

如果我明确指定已安装的程序集,它就会起作用:

$ mcs -reference:MathNet.Numerics.3.20.2/lib/net40/MathNet.Numerics.dll foo.csfoo.cs(6,20): warning CS0219: The variable `A' is assigned but its value is never used
Compilation succeeded - 1 warning(s)

现在,如果我尝试运行生成的可执行文件,它会失败: $ mono foo.exe
$ mono foo.exe
Unhandled Exception:
System.IO.FileNotFoundException: Could not load file or assembly 'MathNet.Numerics, Version=3.20.2.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies.
File name: 'MathNet.Numerics, Version=3.20.2.0, Culture=neutral, PublicKeyToken=null'
[ERROR] FATAL UNHANDLED EXCEPTION: System.IO.FileNotFoundException: Could not load file or assembly 'MathNet.Numerics, Version=3.20.2.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies.
File name: 'MathNet.Numerics, Version=3.20.2.0, Culture=neutral, PublicKeyToken=null'

只有在将库的DLL文件复制到当前目录时,它才能正常工作:

$ cp MathNet.Numerics.3.20.2/lib/net40/MathNet.Numerics.dll .
$ mono foo.exe
$

是的,我找到了一种方法使它可以工作,但这似乎很笨拙,如果我使用来自NuGet的许多不同库,它甚至会更加笨拙。

所以我肯定错过了什么。是否有某个构建系统可以使用,可以使所有这些自动化?请注意,我在使用Linux,并且如果可能的话,我宁愿留在命令行而不是使用大型IDE(如MonoDevelop)。

1个回答

0

谢谢 - 我刚刚看了一下.NET Core中的dotnet命令行构建工具,它正是我想要的。 (我还注意到Mono现在带有msbuild,这是另一个可以从命令行使用的构建工具。但是dotnet看起来更适合在没有集成开发环境的情况下使用命令行。) - Adam Dingle
10
很抱歉,这个答案没有解释如何在Monodevelop中导入Nuget包。为了保持一致性,请注意。 - fIwJlxSzApHEZIl
我同意,那你怎么回答这个问题呢? - Dale

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接