简体   繁体   English

在C#中包含文件,Mono(gmcs)

[英]Include file in C#, Mono (gmcs)

I'm writing a really small application in C# and I need to include an .cs file with some class. 我正在用C#语言编写一个非常小的应用程序,并且需要在某个类中包含一个.cs文件。 How do I do that? 我怎么做? I'm looking for some equivalent to PHP's "include file" or Python's "from file import something". 我正在寻找一些等同于PHP的“包含文件”或Python的“从文件导入某物”。 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. 而且我真的不想使用MonoDevelop或VisualStudio开始一些新的超大型完全无用的项目,我想保留简单的gmcs和命令行。

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. 您只需在命令行上同时包含两个文件名,并确保名称空间相同,或确保通过using语句或完全限定的引用导入所包含文件的名称空间。 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. 请注意,mono命令行旨在支持与Microsoft编译器完全相同的语法(有一些附加功能),因此这两种语言都是正确的。

Fundamentally this is what the project files and visual studio are doing (albeit going through an in memory msbuild equivalent) 从根本上讲,这就是项目文件和Visual Studio正在执行的操作(尽管要通过等效于内存中的msbuild进行操作)

There are two ways to "include" a file in .NET (and Mono) 在.NET(和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 将includeFile编译为单独的程序集,并从主程序集中引用该文件

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

    this way you get two assemblies 这样,您将获得两个程序集

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM