Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best practice for a multi-target .NET library

I'm planning on releasing an open source (MIT) .NET library, but also including DLLs to make it easy for people so they don't have to compile everything themselves.

My library is very simplistic in the types it references, the only real dependency seems to be .NET 3.0 or higher (since it refers to Func<T> and the like).

I want my library to be usable by multiple targets, including .NET 4.0 server, .NET 3.5 server, Windows Phone 7 Silverlight, normal Silverlight, XNA (Phone), XNA (Windows), and XNA (XBox 360).

I make sure to not use any types that are not available on the platforms I'm targeting, e.g. HashSet<T> isn't available on Windows Phone 7, so I'm not using it.

Will I need to make different projects and thus multiple DLLs for each of these targets or is there some way to produce a common DLL for them all to use?

like image 427
ckknight Avatar asked Nov 16 '10 23:11

ckknight


People also ask

Can you have multiple target frameworks?

For SDK-style projects, you can configure support for multiple targets frameworks (TFM) in your project file, then use dotnet pack or msbuild /t:pack to create the package. nuget.exe CLI does not support packing SDK-style projects, so you should only use dotnet pack or msbuild /t:pack .

What is .NET Framework multi targeting pack?

The . NET Framework 4.6 Targeting Pack is a package that enables developers to build applications targeting the . NET Framework 4.6 by using either Visual Studio 2013, Visual Studio 2012 or third-party IDEs.

What is TFM in dotnet?

Latest versions These target framework versions are the latest stable versions. Prerelease versions aren't shown. A target framework moniker (TFM) is a standardized token format for specifying the target framework of a . NET app or library.

Can you mix .NET Framework and .NET Core?

At this point, if your NuGet dependencies are compatible with both your netcore and netframework targets you may be done! The big news here is that most of your NuGet dependencies are compatible with both. All of the most downloaded NuGet packages are either multi-targeted, or have packages for each target.


2 Answers

There was a talk at PDC this year about doing this sort of stuff:

This is the video and these are the slides.

like image 102
Will Dean Avatar answered Oct 01 '22 22:10

Will Dean


Separate project files with shared, common source files is the way to go. Make sure that the projects are set to build to different output folders (e.g. not \bin\Debug, but \CF\bin\debug) to prevent annoyances when consuming the library from multiple targets (like a desktop and device project).

The OpenNETCF.IoC framework is an example of this - it supports the desktop, CF, Windows Phone and MonoTouch all with the same source files, but separate project files per platform. Trying to compile to a single binary assembly that is usable on all of them was too messy and hard to maintain.

The main pain point here is keeping all of the project files in sync when you add/change files and making sure all of them always compile. A build server can do that if you have access to automation (which Codeplex unfortunately doesn't expose).

like image 35
ctacke Avatar answered Oct 01 '22 23:10

ctacke