您可以编译 C# 使其在运行时不需要 .NET Framework 吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/551554/
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
Can you compile C# so it doesn't need the .NET Framework at runtime?
提问by Brian Sweeney
Is it possible to force the C# compiler to pull all the referenced calls out of the framework and pack them into dlls or even a single executable?
是否可以强制 C# 编译器将所有引用的调用从框架中提取出来并将它们打包到 dll 甚至单个可执行文件中?
I like writing quick 'one-off' applications with C#, however I don't want to have to install the whole framework on the target machine once it's ready to go.
我喜欢用 C# 编写快速的“一次性”应用程序,但是我不想在目标机器上安装整个框架,一旦它准备好了。
采纳答案by Andrew Arnott
You ask a loaded question. C# is merely a language and does not require the .NET Framework. The process of compiling it requires a compiler, which may or may not itself take a dependency on the .NET Framework (Microsoft's C# compiler does not -- it is written in native code). Your program will need to reference some assembly where types, classes, and methods can be found for your use. You canremove system.dll and mscorlib.dll from your references list and reference your own assemblies. So you can avoid dependencies on the .NET Framework if you really work at it. But in the end, unless you have a C# compiler that compiles programs to native code you still have a dependency on the CLR.
你问了一个沉重的问题。C# 只是一种语言,不需要 .NET Framework。编译它的过程需要一个编译器,它本身可能依赖于 .NET Framework(微软的 C# 编译器没有——它是用本机代码编写的)。您的程序需要引用一些程序集,其中可以找到类型、类和方法供您使用。您可以从引用列表中删除 system.dll 和 mscorlib.dll 并引用您自己的程序集。因此,如果您真的在工作,就可以避免对 .NET Framework 的依赖。但最终,除非您有将程序编译为本机代码的 C# 编译器,否则您仍然依赖于 CLR。
That's a very technical way of saying... almost nothing. But it answers your question. :) More practically useful however is how to get your C# programs to run with a minimum of dependencies. mkbundle from mono will actually let you compile it all into an .exe with virtually no dependencies.
这是一种非常技术性的说法……几乎没有。但它回答了你的问题。:) 然而,更实用的是如何让您的 C# 程序以最少的依赖项运行。来自 mono 的 mkbundle 实际上会让你把它全部编译成一个几乎没有依赖关系的 .exe。
But if you want to stick with the Microsoft .NET Framework, you can achieve a much lighter footprint and faster install of the dependencies you commonly need by using the Client profile of .NET 3.5 SP1. You can read about it here: http://msdn.microsoft.com/en-us/library/cc656912.aspx
但是,如果您想坚持使用 Microsoft .NET Framework,则可以通过使用 .NET 3.5 SP1 的客户端配置文件来减少占用空间并更快地安装通常需要的依赖项。你可以在这里阅读:http: //msdn.microsoft.com/en-us/library/cc656912.aspx
回答by Spence
Not possible. Your "compiled" C# application is a language which the .Net CLR interprets (should have said JITed, Reads the IL, compiles to native code, and then invokes the compiled native code) at runtime.
不可能。您“已编译”的 C# 应用程序是 .Net CLR 在运行时解释的语言(应该说 JITed、读取 IL、编译为本机代码,然后调用已编译的本机代码)。
FYI .net 2.0 is a standard install on xp SP2 and vista, so you won't be paying that much of a penalty.
仅供参考 .net 2.0 是 xp SP2 和 vista 上的标准安装,因此您不会付出太多代价。
You could look into mono, but this still involves running some kind of framework on your target machine.
您可以研究单声道,但这仍然涉及在目标机器上运行某种框架。
回答by markt
Take a look at the .NET client profile. This will allow you to package a minimum install on the client machine.. which will later be updated by windows update to the full framework.
查看 .NET 客户端配置文件。这将允许您在客户端机器上打包最小安装......稍后将通过 Windows 更新更新到完整框架。
This depends, of course, on your app only using libraries that are contained in the client profile ...
当然,这取决于您的应用程序仅使用客户端配置文件中包含的库...
Some info here: http://blogs.windowsclient.net/trickster92/archive/2008/05/21/introducing-the-net-framework-client-profile.aspx
这里的一些信息:http: //blogs.windowsclient.net/trickster92/archive/2008/05/21/introducing-the-net-framework-client-profile.aspx
回答by ChrisW
It's said it is possible, using 3rd-party tools such as http://www.remotesoft.com/linker/
据说可以,使用 3rd-party 工具,例如http://www.remotesoft.com/linker/
回答by Rauhotz
Some C# features are bound to interfaces of the .NET framework.
某些 C# 功能绑定到 .NET 框架的接口。
For example:
例如:
yield return
requires the IEnumerable interface
yield return
需要 IEnumerable 接口
using (x) {}
requires the IDisposable interface
using (x) {}
需要 IDisposable 接口
回答by Dave
This dependency which unfortunately frequently breaks or is missing in the real world is a big reason why C# has not had a wider adoption. On the flip side most development does have dependencies.. look at C++ & Java for example.
不幸的是,这种依赖在现实世界中经常被破坏或缺失,这是 C# 没有得到更广泛采用的一个重要原因。另一方面,大多数开发确实有依赖关系。例如,看看 C++ 和 Java。
I don't think we will really get away from these dependency issues anytime soon, so I recommend that if you want to use C#, that you make a wrapper for installation which checks for the .net framework version dependency you need, and if missing notify the user that they need this to run your app.
我认为我们不会很快摆脱这些依赖问题,所以我建议如果你想使用 C#,你为安装制作一个包装器,检查你需要的 .net 框架版本依赖,如果缺少通知用户他们需要它来运行您的应用程序。
回答by Amir Abrams
It is now possible to compile C# to native code using Microsoft .NET Native: https://msdn.microsoft.com/en-us/library/dn584397(v=vs.110).aspx
现在可以使用 Microsoft .NET Native 将 C# 编译为本机代码:https: //msdn.microsoft.com/en-us/library/dn584397(v=vs.110).aspx
It automatically compiles the release version of apps that are written in managed code (C# or Visual Basic) and that target the .NET Framework and Windows 10 to native code.
它会自动将使用托管代码(C# 或 Visual Basic)编写且面向 .NET Framework 和 Windows 10 的应用程序的发布版本编译为本机代码。
...
...
For users of your apps, .NET Native offers these advantages:
?Fast execution times
?Consistently speedy startup times
?Low deployment and update costs
?Optimized app memory usage
对于您的应用程序用户,.NET Native 提供以下优势:
?快速的执行时间
?始终如一的快速启动时间
?低部署和更新成本
?优化应用内存使用
This only works with Visual Studio .NET 2015.
这仅适用于 Visual Studio .NET 2015。