C# interactive window 不会引用我的代码

C# interactive window won't reference my code

在 Visual Studio 2017 年,我正在尝试 运行 c# Interactive Window 中的一些代码。 这适用于非常简单的情况(例如使用系统) 但是,当我尝试引用更晦涩的库时,它失败了

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?)

这是一个 dot net core 2.0 项目,有人建议我可以右键单击该项目并 select "Initialize Interactive with Project",但是我在项目或解决方案。

我需要知道如何为交互式 window 添加对代码库的引用。

您需要 add a reference 任何您打算使用的 'more obscure' 库。

#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.

你可以这样写

#r "Microsoft.AspNetCore.Cryptography.KeyDerivation"

VS 会注册它,如果它会在 GAC 中找到它。如果您的磁盘上某处有 DLL。传递路径而不是字符串,它会注册它。

如果您在您的项目中注册了引用,您只需右键单击,就会有一个选项可以使用 c# 与当前项目交互。

  1. 通过导航到 "Views > Other Windows > C# Interactive"

  2. 打开交互式 window
  3. 右键单击您的项目并 select "Initialize Interactive with Project"