如何在 C# .NET 中编写程序,在 Linux/Wine/Mono 上运行它们?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13103360/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to write programs in C# .NET, to run them on Linux/Wine/Mono?
提问by Kamil
In this particular case I need to run .complicated NET application for Linux.By complicated i mean - that project was developed for 3 years and i don't want to write it again in java or something else and develop and support both, .NET and Java version later.
在这种特殊情况下,我需要为 Linux 运行 .complicated NET 应用程序。我的意思是复杂 - 该项目已经开发了 3 年,我不想再用 java 或其他东西编写它,然后再开发和支持 .NET 和 Java 版本。
Application is generating mouse and keyboard events (by winapi/dll import) and uses serial port. I have also few timers for delays - for serial port communication (10-20ms, i dont need big precision here).
应用程序正在生成鼠标和键盘事件(通过 winapi/dll 导入)并使用串行端口。我也有几个用于延迟的计时器 - 用于串行端口通信(10-20 毫秒,我在这里不需要很高的精度)。
The rest is just a lot of simple code, nothing special, no weird controls, no directx etc.
剩下的只是很多简单的代码,没有什么特别的,没有奇怪的控件,没有 Directx 等。
What should i expect? Will this work?
我应该期待什么?这会起作用吗?
If some part of code will fail - i can change it a little, make network connection between .net app and mini-application on linux that i can write for sending mouse and keyboard events, or RS232 communication.
如果代码的某些部分会失败 - 我可以稍微更改它,在 .net 应用程序和 linux 上的迷你应用程序之间建立网络连接,我可以编写用于发送鼠标和键盘事件或 RS232 通信。
Additionally i want to ask about Wine and .NET in general:
另外我想问一下一般的Wine和.NET:
How to write .NET applications that should run on Linux/Wine/Mono?
如何编写应该在 Linux/Wine/Mono 上运行的 .NET 应用程序?
- which version of framework? (1.x, 2.0 or can i use 3.5?)
- what should i avoid (imports from windows dll? timers?)
- 哪个版本的框架?(1.x、2.0 还是我可以使用 3.5?)
- 我应该避免什么(从 Windows dll 导入?计时器?)
Edit/moved from comment:
编辑/从评论中移出:
I saw mono few years ago, but it was terrible. Now i see it growed up, supports LINQ, Threading and other complicated features. In addition now help looks really serious. Im not accepting answer yet, because i see that people still posting very useful links. If this question gets many +1 i will rewrite it and maybe this will help others.
几年前我看过mono,但它很糟糕。现在我看到它长大了,支持 LINQ、Threading 和其他复杂的功能。另外现在帮助看起来真的很严重。我还没有接受答案,因为我看到人们仍在发布非常有用的链接。如果这个问题得到很多 +1,我会重写它,也许这会帮助其他人。
I hope someone here have some practical experience with .NET on linux here...
我希望这里的人有一些在 linux 上使用 .NET 的实践经验......
采纳答案by Freeman
Have you read about Mono.NET on http://www.mono-project.com?
您在http://www.mono-project.com上阅读过 Mono.NET吗?
It allows you to write .NET apps for Linux with minor differences over the Microsoft implementation. I don't think wine will cope with any of the .NET components.
它允许您为 Linux 编写 .NET 应用程序,与 Microsoft 实现略有不同。我认为wine 不会处理任何.NET 组件。
Just read the documentation before and you have a go.
只需阅读之前的文档即可。
PS: This also gives you the compatibility list between mono and .NET. http://www.mono-project.com/Compatibilityand MoMA(mono migration analyzer)is a tool that will scan any .NET app already created to see if its compatible to deploy on linux.
PS:这也为您提供了单声道和 .NET 之间的兼容性列表。http://www.mono-project.com/Compatibility和MoMA(单声道迁移分析器)是一种工具,可以扫描任何已经创建的 .NET 应用程序,以查看其是否与在 linux 上部署兼容。
回答by Kamil
In this post i shortened and combined all information i learned from others in this thread.
在这篇文章中,我缩短并合并了我在此线程中从其他人那里学到的所有信息。
Open source impementation of .NET for Linux is called Mono.
Compatibility is well documented (see links below). Mono supports LINQ, threading and some other complicated features.
It even supports InteropServices. Its possible to use DllImport on linux library(!), like
libc.so
for example.Mono can support Android and iOS(they are Linux based, right?)
There is a tool for Mono compatibility analysiscalled MoMA (Mono Migration Analyzer)
.NET for Linux 的开源实现称为Mono。
兼容性有据可查(请参阅下面的链接)。Mono 支持 LINQ、线程和其他一些复杂的功能。
它甚至支持互操作服务。可以在 linux 库(!)上使用DllImport
libc.so
,例如。Mono 可以支持Android 和 iOS(它们是基于 Linux 的,对吧?)
有一个叫做 MoMA(Mono Migration Analyzer)的Mono 兼容性分析工具
The Mono Migration Analyzer (MoMA) tool helps you identify issues you may have when porting your .Net application to Mono. It helps pinpoint platform specific calls (P/Invoke) and areas that are not yet supported by the Mono project.
Mono 迁移分析器 (MoMA) 工具可帮助您识别在将 .Net 应用程序移植到 Mono 时可能遇到的问题。它有助于查明特定于平台的调用 (P/Invoke) 和 Mono 项目尚不支持的区域。
Useful links:
有用的链接:
Mono - page with documentation
回答by JM Catena
We have a couple of .net applications that run on Windows or Linux with Mono.
我们有几个 .net 应用程序可以在 Windows 或 Linux 上使用 Mono 运行。
You can develop in VisualStudio if you want, it is not necessary to use the Mono development environment. Only the runtime mono libraries are required in order to run .net applications on Linux.
如果需要,您可以在 VisualStudio 中进行开发,不必使用 Mono 开发环境。为了在 Linux 上运行 .net 应用程序,只需要运行时单声道库。
We use net 2.0 target to compile these apps in VisualStudio, because mono is nearly 100% compatible with .net 2.0. Mono also implements APIS from later .net versions, but not all and not as complete. If you use features of newer versions than 2.0, you should check if these are supported or use the Mono compatibility analysis tool. You can also use Mono develop instead of Visual Studio, my predecessor was doing that; the result is pretty much the same, and I prefer to use Visual Studio instead.
我们使用 net 2.0 目标在 VisualStudio 中编译这些应用程序,因为 Mono 与 .net 2.0 几乎 100% 兼容。Mono 还实现了更高版本的 .net 版本的 APIS,但不是全部,也不完整。如果您使用比 2.0 新版本的功能,您应该检查这些功能是否受支持或使用 Mono 兼容性分析工具。您也可以使用 Mono 开发而不是 Visual Studio,我的前任就是这样做的;结果几乎相同,我更喜欢使用 Visual Studio。
We didn't find any major trouble, and the apps are relatively complex, heavily multithreaded, async I/O, WinForms, sockets, serial ports, etc.
我们没有发现任何大问题,应用程序相对复杂,多线程,异步I/O,WinForms,套接字,串口等。
Some app used .net libraries that are not available in MS .net, but all these can be installed on Windows as well so that there is no problem to run these apps in Windows.
一些应用程序使用了 MS .net 中不可用的 .net 库,但所有这些都可以安装在 Windows 上,因此在 Windows 中运行这些应用程序没有问题。
Hope that helps,
希望有帮助,
回答by Tadej
You can develop C# applications on linux with .NET Core: https://www.microsoft.com/net/core
您可以使用 .NET Core 在 linux 上开发 C# 应用程序:https: //www.microsoft.com/net/core
After you install it, type dotnet new
in your terminal to get a list of application templates which can be generated for you to start.
安装后,dotnet new
在您的终端中键入以获取可以生成的应用程序模板列表以供您启动。
As of .NET Core 2.0 today, this is the list:
从今天的 .NET Core 2.0 开始,这是列表:
Templates Short Name Language Tags
--------------------------------------------------------------------------------------------------------
Console Application console [C#], F#, VB Common/Console
Class library classlib [C#], F#, VB Common/Library
Unit Test Project mstest [C#], F#, VB Test/MSTest
xUnit Test Project xunit [C#], F#, VB Test/xUnit
ASP.NET Core Empty web [C#], F# Web/Empty
ASP.NET Core Web App (Model-View-Controller) mvc [C#], F# Web/MVC
ASP.NET Core Web App razor [C#] Web/MVC/Razor Pages
ASP.NET Core with Angular angular [C#] Web/MVC/SPA
ASP.NET Core with React.js react [C#] Web/MVC/SPA
ASP.NET Core with React.js and Redux reactredux [C#] Web/MVC/SPA
ASP.NET Core Web API webapi [C#], F# Web/WebAPI
global.json file globaljson Config
Nuget Config nugetconfig Config
Web Config webconfig Config
Solution File sln Solution
Razor Page page Web/ASP.NET
MVC ViewImports viewimports Web/ASP.NET
MVC ViewStart viewstart Web/ASP.NET
The current latest version is 2.0.
当前最新版本是2.0。