简体   繁体   中英

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 : 在此处输入图片说明

Or when selecting the Target framework while creating a new Visual C# project (New Project > Visual C# > Console App (.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.

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


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

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). 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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