Sometimes packages need references to system assemblies

When NuGet packages need to reference other NuGet packages, it’s fairly straightforward to do: add a References section in your NuSpec file pointing to the right package. However, what if your package requires one of the built-in assemblies, like System.ComponentModel.Composition (MEF)? As far as I can tell, NuGet doesn’t have a way to do this in the spec file. ReactiveUI’s WP7 version needs a reference to System.Observable – trying to install the package then building it results in build errors, and that’s no way to live.

Using a post-install script to rig it up

Add a tools folder, and in that add an “install.ps1″ – this is a PowerShell script that gets run whenever your package gets installed. Here’s the magic sauce to add the reference:

param($installPath, $toolsPath, $package, $project)

$project.Object.References.Add("System.ComponentModel.Composition")

You can do all kinds of other cool things with that $project variable, use the NuGet console to explore what’s available.