简体   繁体   English

为什么未设置.NET目标框架定义?

[英]Why are .NET target framework defines not set?

The Target Frameworks documentation page cites that certain defines are automatically set based on the project's target framework, which I am assuming would change based on the Target framework field in Properties > Application : 目标框架”文档页面引用了某些定义是根据项目的目标框架自动设置的,我假设会根据“ Properties > Application的“ Target framework字段进行更改: 在此处输入图片说明

Or when selecting the Target framework while creating a new Visual C# project (New Project > Visual C# > Console App (.NET Framework)) 或者在创建新的Visual C#项目时选择目标框架(“新建项目”>“ Visual C#”>“控制台应用程序(.NET Framework)”)

However as far as I've tested, none of the defines specified in the documentation are actually defined. 但是,据我测试,文档中指定的定义均未定义。

For example, If I create a .NET Framework 3.5 project or manually set an existing project to target .NET Framework 3.5 in properties, NET35 is not defined. 例如,如果我创建一个.NET Framework 3.5项目,或者在属性中手动将现有项目设置为目标.NET Framework 3.5 ,则未定义NET35

I've taken every single define listed on the documentation page and added debug code that prints if any or all are defined, and none appear to be defined: 我已经采用了文档页面上列出的每个定义,并添加了调试代码,如果定义了任何一个或全部,则将进行打印,但似乎没有一个被定义: 在此处输入图片说明 Sourcecode: https://gist.github.com/JohannesMP/ece8987fa18b2eaf830d7426c2256d6b 源代码: https : //gist.github.com/JohannesMP/ece8987fa18b2eaf830d7426c2256d6b


Am I making an incorrect assumption about when or how these defines should be set? 我是否应该对何时以及如何设置这些定义做出错误的假设? What other portable way would I to conditionally compile code based on the target framework on a per-line basis? 我还可以采用哪种其他可移植的方式在每行的基础上有条件地基于目标框架来编译代码?

I am running Visual Studio Community 2017 Version 15.8.4 with the default Microsoft .NET Framework Version 4.7.03056 我正在使用默认的Microsoft .NET Framework版本4.7.03056运行Visual Studio社区2017版本15.8.4

No. Such predefined constants would only work if you work on the new SDK project format, and with multiple target frameworks ( net45 and netstandard1.3 for example). netstandard1.3 。只有当您使用新的SDK项目格式并使用多个目标框架(例如net45netstandard1.3 )时,此类预定义常量才有效。 Their existence is to help you perform conditional compilation so as to support multiple frameworks. 它们的存在是为了帮助您执行条件编译,以支持多个框架。 My blog post shows a concrete example. 我的博客文章显示了一个具体示例。

So you should create a new project that uses the new format, while .NET Framework Console Application template is not. 因此,您应该创建一个使用新格式的新项目,而不使用.NET Framework Console应用程序模板。

如果您从引用中删除,则在控制台中的应用程序将无法运行,但会与窗口应用程序一起运行:Microsoft.CSharp和System.Net.http

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

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