I have a Visual Studio 2008 project that is a Smart Device project targeting a .NET 3.5 Compact Framework Windows CE device and it currently references a 3.5 class library project.
What I would like to do is be able to add the class library into a Visual Studio 2012/2013 solution without "migrating" it.
For example the structure is currently:
VS2008 Solution
Smart Device Project
Class Library Project
I want to have is:
A VS2008 Solution
Smart Device Project
Class Library Project
A VS2013 Solution
Class Library Project
ASP.NET MVC Project
The result of this would be that I can work on the Smart device project in VS2008 and add classes to the Class Library Project, and then be able to open the VS2013 solution file and have those new classes available.
When I look at the difference between a 2008 Class Project and a 2013 Class Project csproj files, the only thing that really is different is the ToolsVersion on the Project element (http://msdn.microsoft.com/en-us/library/bcxfsh87.aspx).
In VS2008 the ToolsVersion is 3.5, but in VS2013 its 12.0.
I've attempted to make the ClassLibraryProject switch between 3.5 and 12.0 for the ToolsVersion with the following:
<Project ToolsVersion="$(ProjectToolsVersion)">
<Choose>
<When Condition="'$(VisualStudioVersion)' != ''">
<PropertyGroup>
<ProjectToolsVersion>12.0</ProjectToolsVersion>
</PropertyGroup>
</When>
<Otherwise>
<PropertyGroup>
<ProjectToolsVersion>3.5</ProjectToolsVersion>
</PropertyGroup>
</Otherwise>
</Choose>
<!-- other project stuff -->
</Project>
I can open this fine in VS2013, but when I open it in VS2008 it says:
Unable to read the project file 'ClassLibraryProject.csproj'.
C:\Test\ClassLibraryPorject.csproj: The tools version "$(ProjectToolsVersion)" is unrecognized.
Please Note: I am not able to migrate the entire project to VS2013 because VS2013 does not support Smart Device projects.
VS2008 which uses MSBuild 3.5 should be able to handle ToolsVersion 12, as in that case it automatically falls back to ToolsVersion 3.5.
Thus, you should migrate the class library project in VS2013, and it should be able to work still in the VS2008 solution without any modification.
VS2008 and MSBuild 3.5 gives you the error, because Choose is evaluated later than Project, so what you define in Choose (aka ProjectToolsVersion) cannot be used in Project.
Related
I tried creating a Windows Forms .NET core 3.1 application via the template and switching the output type to Console Application.
Here's my code:
static class Program
{
static void Main()
{
System.Console.WriteLine(0 switch { 0 => "Hello World" });
}
}
When I compile I get:
error CS8370: Feature 'recursive patterns' is not available in C# 7.3. Please use language version 8.0 or greater.
I'm targeting .NET Core 3.1. I thought that would get me C# 8.0 language features by default. Apparently I am mistaken.
What do I do?
EDIT: I'm using Visual Studio 2019 16.3.9
This is the part that confuses me the most because it says that the Language version is "Automatically selected based on framework version" (and I can't change it.) Also I don't see an adequate explanation of why I can't change language versions at Why can't I select a different C# version? That page says that if I'm using .NET Core 3.x that I should be using C# 8.0.
The .csproj file is as follows:
<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<UseWindowsForms>true</UseWindowsForms>
<ApplicationIcon />
<StartupObject>Program</StartupObject>
</PropertyGroup>
</Project>
Adding this line fixes the problem:
<LangVersion>8.0</LangVersion>
But is that really the only way to create an application? I have to manually edit my .csproj? Why can I not change the Language version and why is it not automatically selecting C# 8.0 based on me using .NET Core 3.1?
Open your csproj and see if you have a line like
<LangVersion>7.3</LangVersion>
If yes try removing it, if that doesn't work try to change it to 8.0
From https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/configure-language-version#defaults
You should remove the
<LangVersion>latest</LangVersion> from your project file when you
update the .NET SDK.
I had the same problem (c#8 unavailable on Core 3.1 project) and I fixed it that way :
Uninstall previous version of Visual studio (2015 and 2017 in my
case)
Repair Visual studio 2019 (V16.4.1 in my case) with "visual studio installer" launched with administrator right.
No need of LangVersion in csproj.
In Visual Studio 2019 if I try to create an ASP.NET Core web application project, I get to select the framework version in the following screen -
But when I try to create a .NET Core class library project I am prompted with the following screen which does not provide any option for selecting the framework version -
Clicking the Create button always creates the project right away taking the latest .NET Core version installed on my machine.
So, how can I select the framework version while creating the class library project? Or do I have to change it manually every time after creation ?
The ASP.NET project creation dialog providing a framework selection seems to be an exception in .NET Core / Standard projects to me. At least since VS2019 with the new "New Project" dialog, you have the following options after creating the project with this dialog.
"Normally" (to my experience), you right-click the project file in the Solution Explorer, choose "Edit Project File" and modify the <TargetFramework> element by naming one of the valid target framework monikers. See MSDN about them.
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
</Project>
You can also rename the element to TargetFrameworks (note the pluralized name) to build against multiple frameworks at the same time, which are ; separated:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net451;netstandard2.0;netcoreapp3.0</TargetFrameworks>
</PropertyGroup>
</Project>
Alternatively, you can also choose "Properties" from the project right-click menu and select a framework via a slightly dated UI not supporting all of the new csproj features, like said multi targeting:
If you need many new projects building against a specific framework, create a template csproj and just copy and rename it.
Also, if you want to build against preview versions of .NET Core in non-preview versions of VS, ensure you allow usage of them in Tools > Options > Environment > Preview Features.
I have one PCL project, in this project I made Xunit project, which I want to reference my PCL project to test it.
When I'm trying to add a reference, I cant check it, because of I have an error like this:
Incompatible target framework: .NETPortable,Version=v.4.5,profile=Profile111)
I'm using Visual studio Mac, I've also updated everything from Check Updates
Also updated all Nuget packages.
I am assuming this is a .NET Core xUnit project which is trying to reference the PCL project.
Currently this is prevented in Visual Studio for Mac 7.2 and earlier versions. In Visual Studio for Mac 7.3 and later, currently available on the alpha channel, it is possible to reference a PCL project from a .NET Core project.
For Visual Studio for Mac 7.2 the only workaround would be to add the project reference manually by editing the .csproj in the text editor. For example:
<ItemGroup>
<ProjectReference Include="..\PclProject\PclProject.csproj" />
</ItemGroup>
I'm using Visual Studio 2017 Community on a Windows 10 box.
When I add a .net standard library (just the default code) to my solution the solution builds just fine in Visual studio 2017.
But when I try to build the solution using
&'C:\Program Files (x86)\MSBuild\14.0\Bin\msbuild.exe' C:_Sources\MySolution\MyProject\MyProject.csproj
I get a build error:
error MSB4041: The default XML namespace of the project must be the
MSBuild XML namespace. If t he project is authored in the MSBuild 2003
format, please add
xmlns="http://schemas.microsoft.com/developer/msbuild/2003" to the
element. If the pro ject has been authored in the old 1.0 or
1.2 format, please convert it to MSBuild 2003 format
It looks similar to this bugreport but I'm on windows so this does not seem to apply to me.
the csproj file contents:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard1.4</TargetFramework>
</PropertyGroup>
</Project>
I tried upgrading it to netstandard1.6 but that didn't help either.
I found out I was using the wrong msbuild version.
The msbuild file resides in a different location now with vs2017
How do I call Visual Studio 2017 RC's version of MSBuild from a BAT file?
Today I installed the .NET Framework 4.5 on my machine expecting to be able to use it from Visual Studio 2010, since it's just a minor update that should't pose problems for Visual Studio 2010. Unfortunately I am not, even manually removing certain 4.0 and adding the corresponding 4.5 assemblies resulted in the original 4.0 assemblies still being referenced in the project.
Is it possible to target version 4.5 from Visual Studio 2010 and if yes, how? I'd really like to use the ribbons...
Each version of Visual Studio prior to Visual Studio 2010 is tied to a specific .NET framework. (VS2008 is .NET 3.5, VS2005 is .NET 2.0, VS2003 is .NET1.1) Visual Studio 2010 and beyond allow for targeting of prior framework versions but cannot be used for future releases. You must use Visual Studio 2012 in order to utilize .NET 4.5.
There are pretty limited scenarios that I can think of where this would be useful, but let's assume you can't get funds to purchase VS2012 or something to that effect. If that's the case and you have Windows 7+ and VS 2010 you may be able to use the following hack I put together which seems to work (but I haven't fully deployed an application using this method yet).
Backup your project file!!!
Download and install the Windows 8 SDK which includes the .NET 4.5 SDK.
Open your project in VS2010.
Create a text file in your project named Compile_4_5_CSharp.targets with the following contents. (Or just download it here - Make sure to remove the ".txt" extension from the file name):
<Project DefaultTargets="Build"
xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<!-- Change the target framework to 4.5 if using the ".NET 4.5" configuration -->
<PropertyGroup Condition=" '$(Platform)' == '.NET 4.5' ">
<DefineConstants Condition="'$(DefineConstants)'==''">
TARGETTING_FX_4_5
</DefineConstants>
<DefineConstants Condition="'$(DefineConstants)'!='' and '$(DefineConstants)'!='TARGETTING_FX_4_5'">
$(DefineConstants);TARGETTING_FX_4_5
</DefineConstants>
<PlatformTarget Condition="'$(PlatformTarget)'!=''"/>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
</PropertyGroup>
<!-- Import the standard C# targets -->
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<!-- Add .NET 4.5 as an available platform -->
<PropertyGroup>
<AvailablePlatforms>$(AvailablePlatforms),.NET 4.5</AvailablePlatforms>
</PropertyGroup>
</Project>
Unload your project (right click -> unload).
Edit the project file (right click -> Edit *.csproj).
Make the following changes in the project file:
a. Replace the default Microsoft.CSharp.targets with the target file created in step 4
<!-- Old Import Entry -->
<!-- <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" /> -->
<!-- New Import Entry -->
<Import Project="Compile_4_5_CSharp.targets" />
b. Change the default platform to .NET 4.5
<!-- Old default platform entry -->
<!-- <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> -->
<!-- New default platform entry -->
<Platform Condition=" '$(Platform)' == '' ">.NET 4.5</Platform>
c. Add AnyCPU platform to allow targeting other frameworks as specified in the project properties. This should be added just before the first <ItemGroup> tag in the file
<PropertyGroup Condition="'$(Platform)' == 'AnyCPU'">
<PlatformTarget>AnyCPU</PlatformTarget>
</PropertyGroup>
.
.
.
<ItemGroup>
.
.
.
Save your changes and close the *.csproj file.
Reload your project (right click -> Reload Project).
In the configuration manager (Build -> Configuration Manager) make sure the ".NET 4.5" platform is selected for your project.
Still in the configuration manager, create a new solution platform for ".NET 4.5" (you can base it off "Any CPU") and make sure ".NET 4.5" is selected for the solution.
Build your project and check for errors.
Assuming the build completed you can verify that you are indeed targeting 4.5 by adding a reference to a 4.5 specific class to your source code:
using System;
using System.Text;
namespace testing
{
using net45check = System.Reflection.ReflectionContext;
}
When you compile using the ".NET 4.5" platform the build should succeed. When you compile under the "Any CPU" platform you should get a compiler error:
Error 6: The type or namespace name 'ReflectionContext' does not exist in
the namespace 'System.Reflection' (are you missing an assembly reference?)
FYI, if you want to create an Installer package in VS2010, unfortunately it only targets .NET 4. To work around this, you have to add NET 4.5 as a launch condition.
Add the following in to the Launch Conditions of the installer (Right click, View, Launch Conditions).
In "Search Target Machine", right click and select "Add Registry Search".
Property: REGISTRYVALUE1
RegKey: Software\Microsoft\NET Framework Setup\NDP\v4\Full
Root: vsdrrHKLM
Value: Release
Add new "Launch Condition":
Condition: REGISTRYVALUE1>="#378389"
InstallUrl: http://www.microsoft.com/en-gb/download/details.aspx?id=30653
Message: Setup requires .NET Framework 4.5 to be installed.
Where:
378389 = .NET Framework 4.5
378675 = .NET Framework 4.5.1 installed with Windows 8.1
378758 = .NET Framework 4.5.1 installed on Windows 8, Windows 7 SP1, or Windows Vista SP2
379893 = .NET Framework 4.5.2
Launch condition reference: http://msdn.microsoft.com/en-us/library/vstudio/xxyh2e6a(v=vs.100).aspx
I have been struggling with VS2010/DNFW 4.5 integration and have finally got this working. Starting in VS 2008, a cache of assemblies was introduced that is used by Visual Studio called the "Referenced Assemblies". This file cache for VS 2010 is located at \Reference Assemblies\Microsoft\Framework.NetFramework\v4.0. Visual Studio loads framework assemblies from this location instead of from the framework installation directory. When Microsoft says that VS 2010 does not support DNFW 4.5 what they mean is that this directory does not get updated when DNFW 4.5 is installed. Once you have replace the files in this location with the updated DNFW 4.5 files, you will find that VS 2010 will happily function with DNFW 4.5.
From another search. Worked for me!
"You can use Visual Studio 2010 and it does support it, provided your OS supports .NET 4.5.
Right click on your solution to add a reference (as you do). When the dialog box shows, select browse, then navigate to the following folder:
C:\Program Files(x86)\Reference Assemblies\Microsoft\Framework\.Net Framework\4.5
You will find it there."