I'm building a class library and I will deploy it a NuGet package, which lets me choose different assemblies to be added as references based on the .NET framework version of the project it's added to. This is a very nice feature, but what I'm wondering is whether it is possible to have a single class library project, and build it against mulitple versions of the .NET framework?
I'd rather avoid having:
MyLibrary40.dll
and MyLibrary45.dll
if possible, because the two projects would have to share a lot of code. The 4.5 version will be offering async
functions, which is a 4.5 feature.
Does anyone know what the best approach for this is? Can I use multiple build configurations? Or must I go down the separate project route?
If I was working in C++ I'd probably use multiple configurations and #if
blocks around the functions that are only supported in one configuration, but I worry this would lead to me having two assemblies with the same name that do different things.
Thanks in advance!
It is safe to install multiple versions of the . NET Framework on your computer. Developers may want to check out .
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 .
I would recommend moving to the . Net 4.0 Client Profile. Although it doesn't have a large install base yet, it's a small download that your users can easily install. If you don't want your users to need to download the framework, you should target 3.5, which most people already have.
This multi-targeting pack installs new reference assemblies, IntelliSense files, and other supporting files. The target frameworks added by this update to Microsoft Visual Studio for projects to use are ". NET Framework 4.0. 3" and ".
You will at least need one VisualStudio Solution with 2 projects (one for .net 4 and one for .net 4.5).
Add all codefiles to the .net 4-project and in the other project you add the code files as link (use "Add Existing Item..."
-Dialog and chose Add as link
)
Now you add all codes and classes for .NET 4.5 to your 4.5-project.
Additionally you should define your own compiler switches (conditional compilation symbols) to your projects. Like NET4 for your .net 4-project and NET4.5 to your .net 4.5-project)
You set the switches in the project settings under Build->General->Conditional Compilation Switches
In your code you can use the switches as follows to generate code for .NET 4 or .NET 4.5
#if NET4 // code only for .NET 4 #endif // code for all framework versions. #if NET45 // code only for .NET 4.5 #endif
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With