As I mentioned as a comment to the question, ignoring the question footnotes this looks like a generic .NET question and I expect google will suggest this page to people making generic searches, not limited to Unity, and it won't be obvious to other people reading the question that it's specific to Unity until they read most of the question. Therefore, I'm going to start off giving an answer for generic .NET development.
If all the projects involved use PackageReference
, then nothing special needs to be done. Simple create a .NET Standard Class Library from the new project template, add package references to the packages you want, then create project references to your class library from the apps that you want to use that set of NuGet packages.
If you have a non-SDK style project that does not have any packages, then you should add <PackageRestoreStyle>PackageReference</PackageRestoreStyle>
. The reason is that PackageReference
was added to NuGet years after NuGet started, with the original way to use packages being packages.config
. So, when a project doesn't opt-in to PackageReference
restore style, it defaults to packages.config
. See the next paragraph.
If you have a non-SDK style project that uses packages.config
(or no packages and hasn't opted into PackageReference
restore style), then this approach may not work. The build system will build the app's assembly and probably copy all references directly referenced in the csproj
, which means it will get your class library dll as well as any nuget packages directly referenced. However, since the app csproj doesn't know about your class library's NuGet packages, they won't be copied. Normally a class library references packages that it uses and the .NET build system will use something called ResolveAssemblyReferences to look at the class library dll, see what dependencies it has, then copies those dlls. However, in this case the class library project doesn't have any compiled in dependencies since it never used any of the NuGet packages the project referenced.
This is one of the reasons why the NuGet and .NET teams are encouraging customers to migrate to PackageReference
. There's a bunch of issues, like transitive dependencies, and <HintPath>
's going bad when projects are moved around, that work much better as long as the project type you're using supports it.
Now, given that the question footnote mentioned that the asker wants to do this for a Unity project, I'll give an answer I hope works. Note that while I work on the NuGet client team, I know nothing about Unity and wasted about 2 hours of my morning today installing it and playing around trying to understand its capabilities just to find that Unity doesn't appear to support either NuGet or project references at all.
So my suggestion is to either:
use dotnet publish
to have the .NET build system copy all the relevant dlls into a single folder, where you can then reference in the Unity project
or
Multi-target your class library to target both .NET Standard 2.0 and .NET Framework 4.7.1 (adjust the .NET Framework version to use the same as what your Unity project uses). Do this by changing the <TargetFramework>netstandard2.0</TargetFramework>
to <TargetFrameworks>netstandard2.0;net471</TargetFrameworks>
(note that the XML element had an s
added to the end). Now when you build the class library the .NET SDK will create two directories in the bin\
directory, one for each target framework. At the moment at least, it seems that the net471
directory gets all the dlls, similar to non-SDK style projects.