1

This question is not answered by the proposed duplicate. There is no "Initialize Interactive with Project" option when I right click my project.

I want to test out some code without creating another project and writing a console application. My project includes a reference to System.Windows.Forms and I can use the Forms namespace within my project, however, I cannot use it in C# interactive due to the following error:

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

I've done a bit of sleuthing but I can't find too much information on this feature of Visual Studio. I thought to try loading the assembly with reflection but while loading the assembly did work, C# interactive still cannot find the System.Windows.Forms namespace:

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

Do note that I made sure to run #reset core to switch to .NET Core to match the version of the assembly I tried to load.


Whether it's using reflection to load the assembly or a more graceful method, how do I load a non-default assembly in a usable fashion within the C# Interactive pane of Visual Studio 2019?

codewario
  • 19,553
  • 20
  • 90
  • 159
  • See the following [blog entry](https://learn.microsoft.com/en-us/archive/msdn-magazine/2016/january/essential-net-csharp-scripting) – Karen Payne Dec 23 '21 at 15:46
  • 1
    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) – gunr2171 Dec 23 '21 at 15:46
  • @gunr2171 Unfortunately not. The option to "Reset C# interactive from project" does not exist when I right click either the solution or project as detailed in that thread. – codewario Dec 23 '21 at 15:59
  • @BendertheGreatest order the answers by Active, try the option from Mahmoud Hanafy – gunr2171 Dec 23 '21 at 16:04
  • @HansPassant Even if I run `#reset core`? It's showing I'm running as .NET Core in the pane tab. – codewario Dec 23 '21 at 16:07
  • @hanspassant Either way that worked. If you post as an answer I'll accept it. – codewario Dec 23 '21 at 16:09

1 Answers1

3

For C# Interactive, you can load a DLL directly using the #r directive.

simply execute the following:

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

or more generic:

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

Afterward, you should be able to access the namespace from the assembly.

You can also run your project with C# interactive. Just open the C# interactive window, then Right-Click the Project in your solution explorer, and select "Initialize Interactive with Project", which will also load all of your project assemblies in C# interactive.

enter image description here

Good luck!

FisherL42
  • 46
  • 2
  • I've marked this as the solution but note that the "Initialize Interactive with Project" option is not showing for me, so I've had to resort to the `#r` method. – codewario Dec 23 '21 at 16:13
  • I'm glad you at least could get it working! The "Initialize Interactive with Project" really just automatically pipes the `#r` command into the console, so you could likely make your own tool to do that as well. – FisherL42 Dec 23 '21 at 16:17
  • 1
    Doing some research in a blog post from another comment there is a way to script this I guess. But it's a pain that the menu option isn't working for me on a .NET Core project in VS 2019. [I counter your screenshot with a screenshot](https://imgur.com/a/sneQduD). But either way, the `#r` directive is working for me. – codewario Dec 23 '21 at 16:18
  • 1
    [This post](https://stackoverflow.com/a/57861538/1043380) explains why the "Initialize Interactive with Project" option doesn't exist. – gunr2171 Dec 23 '21 at 16:40