代码之家  ›  专栏  ›  技术社区  ›  codewario

在Visual Studio中,如何将项目程序集(或任何程序集)添加到C#interactive窗格中?

  •  0
  • codewario  · 技术社区  · 3 年前

    这个问题 is not answered by the proposed duplicate 。当我右键单击我的项目时,没有“初始化与项目交互”选项。

    我想在不创建其他项目和编写控制台应用程序的情况下测试一些代码。我的项目包括对 System.Windows.Forms 我可以用 Forms 但是,由于以下错误,我无法在我的项目中使用35C interactive:

    > using System.Windows.Forms;
    (1,22): error CS0234: The type or namespace name 'Forms' does not exist in the namespace 'System.Windows' (are you missing an assembly reference?)
    

    我做了一些调查,但我找不到太多关于VisualStudio这个功能的信息。我本想尝试用反射加载程序集,但在加载程序集的过程中,C#interactive仍然无法找到 系统窗户。形式 名称空间:

    > Assembly.LoadFile("@C:\Program Files\dotnet\packs\Microsoft.WindowsDesktop.App.Ref\3.1.0\ref\netcoreapp3.1\System.Windows.Forms.dll")`
    using System.Windows.Forms;
    (1,14): error CS0234: The type or namespace name 'Windows' does not exist in the namespace 'System' (are you missing an assembly reference?)
    

    请注意,我一定要跑步 #reset core 切换到。NET核心以匹配我尝试加载的程序集的版本。


    无论是使用反射加载程序集还是使用更优雅的方法,我如何在Visual Studio 2019的C#Interactive窗格中以可用的方式加载非默认程序集?

    0 回复  |  直到 3 年前
        1
  •  1
  •   FisherL42    3 年前

    对于C#Interactive,可以使用 #r 指令。

    只需执行以下操作:

    > #r "C:\Program Files\dotnet\packs\Microsoft.WindowsDesktop.App.Ref\3.1.0\ref\netcoreapp3.1\System.Windows.Forms.dll"
    

    或者更通用:

    > #r "C:\YOUR\DLL\PATH"
    

    之后,您应该能够从程序集访问名称空间。

    你也可以用C#interactive运行你的项目。只需打开C#interactive窗口,然后在解决方案资源管理器中右键单击该项目,然后选择“ 初始化与项目的交互 “,它还将在C#interactive中加载所有项目程序集。

    enter image description here

    祝你好运