C#、Mono (gmcs) 中包含文件

发布于 2024-10-24 03:10:23 字数 199 浏览 5 评论 0原文

我正在用 C# 编写一个非常小的应用程序,我需要包含一个带有某些类的 .cs 文件。我该怎么做?我正在寻找一些相当于 PHP 的“包含文件”或 Python 的“从文件导入某些内容”的东西。显然“使用”是不够的,文件必须以某种方式链接。我真的不想使用 MonoDevelop 或 VisualStudio 启动一些新的超级巨大且完全无用的项目,我想继续使用简单的 gmcs 和命令行。

I'm writing a really small application in C# and I need to include an .cs file with some class. How do I do that? I'm looking for some equivalent to PHP's "include file" or Python's "from file import something". Obviously "using" is not enough and the file has to be somehow linked. And I really don't want to start some new super-huge-totally-useless project using MonoDevelop or VisualStudio, I would like to stay with simple gmcs and command line.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

拥有 2024-10-31 03:10:23

您只需在命令行中包含两个文件名,并确保命名空间相同,或者所包含文件的命名空间是通过 using 语句或完全限定引用导入的。然后,用于编译的命令行如下所示:

gmcs mainFile.cs includeFile.cs

请注意,mono 命令行旨在支持与 Microsoft 编译器完全相同的语法(带有一些附加内容),因此这对它们都适用。

从根本上来说,这就是项目文件和 Visual Studio 正在做的事情(尽管经历了内存中的 msbuild 等效操作)

You simply include both file names on the command line and ensure that the namespaces are the same or that the namespace of the included file is imported via a using statement or via fully qualified references. Then the command line for compilation looks like this:

gmcs mainFile.cs includeFile.cs

Note that the mono command line is designed to support the exact same syntax (with a few additions) as the Microsoft compiler so this is true for both of them.

Fundamentally this is what the project files and visual studio are doing (albeit going through an in memory msbuild equivalent)

面如桃花 2024-10-31 03:10:23

有两种方法可以在 .NET(和 Mono)中“包含”文件

  1. 将多个文件编译在一起。

    gmcs mainFile.cs includeFile.cs
    

    然后将文件一起编译为单个程序集

  2. 将 includeFile 编译为单独的程序集并从主程序集引用它

    gmcs -target:library includeFile.cs
    gmcs -r:includeFile.dll mainFile.cs
    

    这样你就得到了两个程序集

There are two ways to "include" a file in .NET (and Mono)

  1. Compile several files together.

    gmcs mainFile.cs includeFile.cs
    

    then files are then compiled together to a single assembly

  2. Compile the includeFile to a separate assembly and reference that from the main assembly

    gmcs -target:library includeFile.cs
    gmcs -r:includeFile.dll mainFile.cs
    

    this way you get two assemblies

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文