11

In Visual Studio 2017 I am trying to run some code in the c# Interactive Window. This works for very simple cases (e.g. using System) However, when I try and reference slightly more obscure libraries it fails

using Microsoft.AspNetCore.Cryptography.KeyDerivation;

(1,17): error CS0234: The type or namespace name 'AspNetCore' does not exist in the namespace 'Microsoft' (are you missing an assembly reference?)

This is a dot net core 2.0 project, and it has been suggested that I can right click on the project and select "Initialize Interactive with Project", however this option does not appear for me on either the project or solution.

I need to know how to add references to code libraries for the interactive window.

jazza1000
  • 4,099
  • 3
  • 44
  • 65
  • Does this answer your question? [Can the C# interactive window interact with my code?](https://stackoverflow.com/questions/11135571/can-the-c-sharp-interactive-window-interact-with-my-code) – StayOnTarget Jul 11 '22 at 20:04

3 Answers3

13
  1. Open Interactive window by navigating to "Views > Other Windows > C# Interactive"

  2. Right click your project and select "Initialize Interactive with Project"

enter image description here

techExplorer
  • 810
  • 7
  • 16
  • 2
    this option is missing for core and even .net standard assemblies, the workaround from this answer helped me https://stackoverflow.com/a/56051613 – gordy Jan 08 '22 at 01:09
11

You need to add a reference to any 'more obscure' libraries that you plan to use.

#r is used to reference assemblies. Below are some examples:

#r "path/MyAssembly.dll" #r "MicrosoftLibrary", e.g., #r "System.Collections.Generic" Note: The Interactive Window doesn't currently support #r'ing NuGet packages. As a temporary workaround, reference the NuGet DLL.

mjwills
  • 23,389
  • 6
  • 40
  • 63
4

You can write this

#r "Microsoft.AspNetCore.Cryptography.KeyDerivation"

VS will be registered it, if it will found it in GAC. If you have DLL somewhere on your disk. pass the path instead of string and it will registered it.

If you have the reference registered in your project you can just right click and there is a option to use c# interactive with current project.

Anirudha Gupta
  • 9,073
  • 9
  • 54
  • 79
  • Yes for me when I right click I don't get the option to use c# interactive. I'm not sure if that is because I am using .net core though – jazza1000 Sep 13 '17 at 13:59