10 February 2021

NuGet packages inside dotnet interactive

by Shaun Lawrence

Following on from a previous post on using dotnet interactive inside vs code we can dig a little deeper in to some more useful functionality. We all need to consume NuGet packages right? I know I certainly do.

It is entirely possible to pull a package down and starting coding against that package. I use this feature a lot for diagnosing or testing things out with my own OSS library hosted via NuGet.

All you need to do is declare the following inside your C# code block:


So shameless plug time :), here is how I do it with my own library:

 #r "nuget: ExpressiveParser, 2.4.0" 

Then you are free to start using that package:

var expression = new Expressive.Expression("Pow(2, WeekDay(#today#))");  
expression.RegisterFunction("WeekDay", (p, a) => ((DateTime)p[0].Evaluate(a)).DayOfWeek);  

Here you can see it in action, simply hit Ctrl+Return and watch the tool pull the package the first time and then execute the code.


tags: C# - dotnet - dotnet-interactive - nuget - visual studio code