如何能的C#代码编译提出,要需要一个给定的语言和编译器的版本? [英] How can compilation of C# code be made to require a given language or compiler version?

查看:99
本文介绍了如何能的C#代码编译提出,要需要一个给定的语言和编译器的版本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何C#程序检测到它正在一个版本的C#不包含在该程序中使用的语言功能的支持下汇编?



C#编译器将拒绝该程序,并产生一些错误消息,在遇到它不支持的语言的功能。这不解决这个问题,这是说明该方案是在太旧版本的C#编译器或C#编译器不支持C#所需版本


$ B编译$ b

在理想情况下,这将是为

 #如果CS_VERSION<一样简单; 3 
#ERROR CSHARP 3或更高版本
#END


解决方案

我不相信你可以做到这一点与一个C#文件,但如果你使用的MSBuild那么项目/解决方案的工具,版本号可以被使用旧版本的MSBuild内置停止。



你能给这个确切的背景下?一个人的解决方案,而不是技术可能会尝试编译所有的旧版本的代码,并创建一个文档:如果你遇到一个错误的这个的这可能意味着你'重新使用错误的版本......



您可能要考虑做,即使是简单的是有一个单一的另一种选择所需的功能文件。这将是未使用由您的主要应用程序,但要求用户编译第一。如果一切正常,你构建的其余部分应该工作。如果不是这样,这是由于使用了错误的版本。这可能会产生一个较小的范围内,从不同版本的错误(特别是它不具有编译器可以列出以随机的顺序从不同的文件中的错误的问题)的


How can a C# program detect it is being compiled under a version of C# that does not contain support for the language features used in that program?

The C# compiler will reject the program, and produce some error message, on encountering the features of the language it does not support. This does not address the issue, which is to state that the program is being compiled with too old a version of the C# compiler, or a C# compiler that does not support the required version of C#

Ideally, it would be as simple as

#if CS_VERSION < 3
#error CSharp 3 or later is required
#end

解决方案

I don't believe you can do that with a C# file, but if you're using MSBuild then the project/solution tools version number can stop it from being built with an older version of MSBuild.

Can you give the exact context of this? One "human" solution rather than a technical one might be to try compiling the code with all the "old" versions, and create a document with: "If you get an error like this it probably means you're using the wrong version..."

Another option you might want to consider to make that even simpler is to have a single "required features" file. This would be unused by your main app, but ask users to compile that first. If it works, the rest of your build should work. If it doesn't, it's due to using the wrong version. That's likely to produce a smaller range of errors from different versions (in particular it doesn't have the problem that the compiler could list errors from different files in a random order).

这篇关于如何能的C#代码编译提出,要需要一个给定的语言和编译器的版本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆