How to implement multi-targeting inside a solution?

506 Views Asked by At

We have a solution that contains both a Net 5 WPF application and a .Net Framework 4.7.1 class library that contains Com visible classes. This dll will be registered using regasm.exe to be used from an application developed in Visual Basic 6 (yes, truly).

The Net 5 application and the .Net Framework library share code in five class libraries. When I start up the Net 5 application I would like these dependent projects to be compiled against Net 5. And when I build the .Net Framework Com visible project I would like the dependent projects to be compiled against .Net Framework 4.7.1.

How can this be done? Is this a solution that will enable our team to hold on to our sanity? Or is a different approach better?

1

There are 1 best solutions below

0
On

You can specify multi targets inside the project file using TargetFrameworks element within the csproj file.

However, personally I have found that creating separate projects that act as the placeholders for the code and then Shared Projects to hold the source code works well. Essentially you create a new, empty, csproj called MyLibrary_471 and another one called MyLibrary_5 and then a third "shared project" where all of the code is. You reference the shared project in each of the other two and from then on only edit the code in the shared project. It is a little bit of mucking about to get set up but once it is working it gives you great flexibility in using either #IF.... for framework specific code (in the shared project) or specific files in each of the other two projects.

Also see this link for useful multi targeting info. https://learn.microsoft.com/en-us/dotnet/standard/frameworks