简体   繁体   English

安装.NET可能无法进行C#编译吗?

[英]Is it possible that C# compilation is unavailable with .NET installed?

This answer shows how to use CSharpCodeProvider class to compile a C# code snippet during runtime. 此答案显示了如何在运行时使用CSharpCodeProvider类编译C#代码段。 Since that's a rather specific task I'm not sure that every system with .NET Framework installed will have this capability - perhaps something extra has to be installed. 由于这是一项非常具体的任务,因此我不确定每个安装了.NET Framework的系统都将具有此功能-也许需要安装一些其他功能。

Is CSharpCodeProvider with its C# code compilation capability available on any system with .NET Framework installed? 具有C#代码编译功能的CSharpCodeProvider在安装了.NET Framework的任何系统上是否可用?

According to the MSDN Documentation for the CSharpeCodeProvider Class it is available in all of the frameworks 根据CSharpeCodeProvider类的MSDN文档,它在所有框架中都可用

Provides access to instances of the C# code generator and code compiler. 提供对C#代码生成器和代码编译器实例的访问。

.NET Framework .NET Framework
Supported in: 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0 受以下版本支持:4.5、4、3.5、3.0、2.0、1.1、1.0

.NET Framework Client Profile .NET Framework客户端配置文件
Supported in: 4, 3.5 SP1 受以下版本支持:4、3.5 SP1

All previous answers are correct, but I've made a test on a VM without .NET SDK/Visual Studio Installed. 以前所有的答案都是正确的,但是我已经在未安装.NET SDK / Visual Studio的VM上进行了测试。 It is a plain 2003 server with .NET 4 Client Profile + .NET 4 Extended Framework. 它是带有.NET 4客户端配置文件+ .NET 4扩展框架的普通2003服务器。

And result is that in this setup CSharpCodeProvider works just fine! 结果是,在此设置中,CSharpCodeProvider工作正常!

If you want to protect yourself even more, you can check for specific code provider availability with this call: 如果您想进一步保护自己,则可以通过以下调用检查特定的代码提供程序可用性:

   CodeDomProvider.IsDefinedLanguage("CSharp")

And documentation for this method says: 该方法的文档说:

The Element in the machine configuration file (Machine.config) contains the language provider and compiler configuration settings for each CodeDomProvider implementation on the computer. 机器配置文件(Machine.config)中的Element包含计算机上每个CodeDomProvider实现的语言提供程序和编译器配置设置。 The IsDefinedLanguage method searches the provider configuration elements for the specified language name. IsDefinedLanguage方法在提供程序配置元素中搜索指定的语言名称。

Therefore it is possible to run your program on computer where CodeProvider WILL NOT work (may be some paranoid system administrator wanted to disable dynamic code compilation (although it is meaningless because using CodeDomProvider requires full trust anyway)). 因此,可以在CodeProvider无法正常工作的计算机上运行程序(可能是有些偏执的系统管理员想要禁用动态代码编译(尽管这毫无意义,因为使用CodeDomProvider仍然需要完全信任))。

From CSharpCodeProvider documentation : CSharpCodeProvider文档

Version Information .NET Framework Supported in: 版本信息.NET Framework受以下版本支持:

4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0 4.5、4、3.5、3.0、2.0、1.1、1.0

.NET Framework Client Profile Supported in: .NET Framework客户端配置文件受以下版本支持:

4, 3.5 SP1 4,3.5 SP1

AFAIK, CSharpCodeProvider只是csc.exe的包装,而csc.exe反过来在每个.NET 桌面版本上都可用(例如,在Silverlight上不可用)。

http://msdn.microsoft.com/en-us/library/microsoft.csharp.csharpcodeprovider%28v=vs.71%29.aspx http://msdn.microsoft.com/zh-cn/library/microsoft.csharp.csharpcodeprovider%28v=vs.71%29.aspx

first hit on Google. 首次在Google上投放。 Click "Other versions" 点击“其他版本”

It gives a list of versions where this class (and therefore this feature) was available. 它提供了可以使用该类(以及该功能)的版本列表。

It seems this class has been available from version 1.1, I'd say that's probably every version as even the Object class goes back to that version. 似乎该类已从1.1版开始提供,我想说这可能是每个版本,甚至Object类都可以回到该版本。

In addition to the other answers, it would appear that the language specification is actually tied in to this assembly, so I believe the answer would be "yes". 除了其他答案,似乎语言规范实际上与该程序集绑定在一起,因此我相信答案将是“是”。

See: dynamic (C# Reference) (answer based on https://stackoverflow.com/a/2460934/211627 ) 请参阅: 动态(C#参考) (基于https://stackoverflow.com/a/2460934/211627的答案)

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

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