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.
In console application doesn't work but with window application if you remove from reference: the Microsoft.CSharp and System.Net.http
Related
I created a .Net 6 class library and now I am trying to create a test project.
a .net 6 test project is not available. Online I found the information to use .Net Framework or .Net core in order to test.
I created a .Net Framework 4.8 test project an referenced my class library.
I receive the Compiler error:
Project '..\CircularList\CircularList.csproj' targets 'net6.0'. It cannot be referenced by a project that targets '.NETFramework,Version=v4.8'. UnitTests
How do I do Unit tests then? Is there any way to target .Net 6.0 from .Net Framework 4.8?
None of the previous answers worked for me. I was trying to add a Test project for my Windows Forms (.NET 6.0) project. Tried all of the available project templates, and none worked.
Just needed to modify the test project .csproj (by double clicking the Test Project), and change TargetFramework from net6.0 to net6.0-windows.
I guess that must have been the answer. Don't use the project template "Unit Test Project (.NET Framework)" if you want to use test a .net6 library.
Use a more up-to-date template in the project creation wizard. There are newer MSTest ones, but one could also take advantage of better templates like the xUnit one.
I created a NET 6.0 class library and received the same message.
I went into properties, just to double check that my project and my test project were set the same and noticed that the Target OS was not set in my library.
It was in my project, but not in my class library.
Once I changed my class library "Target OS" - to be windows, the same as my project, the error messages went away.
This is A solution, not likely THE only solution:
I also have a .NET 6.0 project that I would like to test.
With the project template picker, I picked the C# NUnit Test for .NET Core. When advancing to next screen, there was a dropdown that allowed me to pick a Target framework. .NET 6.0 was the default option.
None of the previous answers worked for me. In the beginning, I think .Net framework missing, so I install the installer and install, but it doesn't work.
I also try to change the project "Target OS" to "Windows", it doesn't work. I find the Test Project property target is still .Net 4.7.2, the UI is a old version, not like the new version.
Then I remove the Test Project created from the template, and right-click the method to test and click "Create Unit Tests", then create the Test Project by the pop-up window. It works. And the new Test Project works successfully, its property target is .Net 6, and the UI is the new version.
Disclaimer: I have only two day's experience trying to learn modern MSBuild techniques, but have used C# and VS for many years largely avoiding project file details outside of what the UI provides.
I am creating experimental DLLs that intentionally name-match and somewhat interface-match several of those found in the .NET Standard. For instance a custom System.IO.dll may partially match the public interface of .NET's System.IO.dll, same with mscorlib.dll, etc.
The goal is to be able to create a new C# project (File->New->Project->...) and have it link to these alternatives DLLs and not the official DLLs. This has been done successfully from the command-line using csc.exe, but not yet from within VS.
Using detailed build output verbosity in VS (Tools->Options->Projects and Solutions->Build and Run) shows that the numerous DLLs within .NET are added to the csc.exe command-line. Perhaps if this could be prevented all would be well. One failed attempt to do so involved removing the <TargetFramework> altogether.
Maybe registering a "custom framework" with Visual Studio would allow a custom value to be used within csproj, e.g.<TargetFramework>mydotnet</TargetFramework>?
All just guesses.
It seems MSBuild is very rich and would allow for multiple ways to achieve such a thing. I am completely receptive to learning one or more of these techniques, since one may provide advantages for future build customization plans.
How can a Custom Framework be used instead of .NET in Visual Studio for C# projects?
To use a Custom Framework instead of .NET in Visual Studio for C# projects, you have to do a lot of things, Registering Framework, Create a Template for Project and so on. So, Are you really writing your own implementation of the framework? If you just want to add your own libraries to an existing Framework, you can refer to following thread:
Registering Extensions of the .NET Framework
Besides, there is a document about How to: Add a Custom ASP.NET MVC Test Framework in Visual Studio, you can check it for details.
I've added a new Class Library (Package) project to my solution. It's my first experience with a .NET Core (or whatever I'm using, still confused)
My class library contains two references: .NET Framework 4.5.1 and .NET Platform 5.4
I'm trying to import some code from a sample project that uses IPrincipal. For some reason it's saying that it doesn't exist on namespace "System.Security" altohugh I can get it trough intellisense.
What's wrong with my project settings?
The new feature of .NET Core and Class Library (Package) is that it targets multiple platform and will compile into multiple assemblies which get automatically packaged into a nuget package.
When your class library targets multiple targets, it will compile to all of them. So if a certain library is only available on full .NET framework but not on .NET Core or other target framework, then you may receive intellisense if your editor is set to .NET 4.5. More information can be found in my other recent answer.
You can switch back and forth with the pull down menu on top left of the coding window, show in the screenshot below.
If you do not want to target a certain framework, you have to remove it's moniker from the project.json file or use preprocessor directives to write platform specific code or libraries/replacements.
.NET Core is heavily modularized and most of only the core modules are referenced in the default project and if you need additional one you need to reference them within the dotnet5.x section.
Basically you have multiple places with "dependencies" in your project.json, a global one where you can add dependencies which are available on all targeted frameworks and one within each "frameworks" section for each of the targets only.
Though the other answer covers some basic concepts, it would require some attention on which classes are available and which are not.
Microsoft temporarily host a web site at http://packagesearch.azurewebsites.net to assist.
If you can find a suitable package for RC1 from there, then you can add it to your project.json file. If not, you will have to conditional compile it to a desktop profile or use other alternatives.
I am trying to target the full .NET 4.0, as described here, because the default seems to target the client profile:
http://blogs.msdn.com/b/jgoldb/archive/2009/10/19/what-s-new-in-net-framework-4-client-profile-beta-2.aspx
I also see instructions here:
http://msdn.microsoft.com/en-us/library/bb398202.aspx
But I am wondering whether there's a flag or combination of flags I can pass to csc.exe to target the full framework without using Visual Studio. More specific, I'd like to avoid solutions that require creating a solution or project file.
As far as I know, target framework just specifies where to look the types in and what types to look for. So, as long as you use only the types defined in the .NET version you want, it will be compatible with that version's runtime compiled without any additional options.
I am coding in VS2008 with Resharper 4.5.1, but the projects are set to target .NET Framework 2.0.
Still, Resharper is making suggestions that are relevant to the .NET 3.5 framework. For instance, it tells me that I should be using collection initializers, etc...
I've looked through the settings and can't seem to find the checkbox to tell it to give 2.0 specific advice.
Select your project in the Solution Explorer and open the Properties tool window (F4 in the standard keyboard layout or View > Properties Window after selecting the project). In the ReSharper section, there is a Language Level property that you can set to C# 2.0. Note that there are two separate project properties windows that manage different properties, if you see tabs for "Application", "Build" and "Debug" you are in the wrong window.
As others have said, this affects the version of C#, not the version of the framework (since most of the C# 3.0 changes can be compiled to an assembly that targets .NET 2.0).
Those features are not .NET 3.5 framework features, but merely features of the 3.5 compiler. And since in VS2008 this is the compiler invoked for .NET 2 targets, it does handle these syntax extensions correctly.