0

I am the author of a number of open source of Nuget packages and I want my Nuget packages to be convenient to use for everyone, but it seems to be unreasonably difficult to configure Visual Studio to produce multiple assemblies targeting different platforms and architectures from one set of source code.

One of the major difficulties is that Microsoft does not do this for it's own Nuget packages, and therefore my project has to reference different versions of Microsoft's packages for each framework I want to target. This in turn means that I need different projects for each platform and this is awkward for projects with a large number of source files.

I can't help feeling that I missed something because many people must be suffering the same issue and it's hard to imagine that after so many years of development of Visual Studio this is still so difficult.

Can anyone recommend a pattern of organizing solutions, projects, source files etc that makes it easy to write code once and have it compile to multiple DLLs that target .Net 4.0, .Net 4.5, .Net Standard 1.0 etc.

bikeman868
  • 1,346
  • 14
  • 24
  • You can use conditional references. The answer [here](https://stackoverflow.com/questions/42747977/how-do-you-multi-target-a-net-core-class-library-with-csproj) has a good example with a .NET Core project. – Herohtar Nov 11 '19 at 00:44
  • This is rather broad, but the individual 'bits' to make such a solution are already covered very well on this site e.g. for the package and DLL references https://stackoverflow.com/a/58192509/397817 ; for conditional compilation https://stackoverflow.com/a/57031308/397817 - and that's just 2 I've plucked from my own answers :) – Stephen Kennedy Nov 11 '19 at 02:08
  • On the other hand, it really can be as simple as [this](https://github.com/kingboyk/Serilog.NodaTime/blob/master/Serilog.NodaTime/Serilog.NodaTime.csproj). So, it does depend very much on the code and other details we don't have information on. – Stephen Kennedy Nov 11 '19 at 02:17
  • I experimented with both shared projects and the element. On the whole I think is the way to go, but it involves a lot of manual editing on the project file which I was always uncomfortable with. I guess now that Visual Studio has better support for editing the project file maybe this is more acceptable. – bikeman868 Nov 11 '19 at 06:47

1 Answers1

0

Below is the way to set multiple target frameworks which results into multiple output folders to be produced.

<TargetFrameworks>netstandard1.0;net45;net40</TargetFrameworks>

I don't think there is other way to handle mapping between output platforms and versions of dependencies other than below:

<ItemGroup Condition=" '$(TargetFramework)' == '<version>'">
    <PackageReference Include="<dependency_name>" Version="<version>" />
</ItemGroup>

A detailed description of the migration process from an older project is available here

bikeman868
  • 1,346
  • 14
  • 24
  • That's only one part of it. The question specifically asks about how to handle references that are only needed for a certain framework. – Herohtar Nov 11 '19 at 00:51
  • @Herohtar, versions could be handled like this. This seem not straitforward but it does not seem there is a more convinient way. – Artyom Ignatovich Nov 11 '19 at 00:59
  • I think this is the right approach but your answer is incomplete. If you care to improve it I will mark it as the correct answer. – bikeman868 Nov 11 '19 at 18:27