Can the C# Interactive Window Interact with My Code

Can the C# interactive window interact with my code?

When using Visual Studio between 2015 and 2022:

You can open the Interactive window by navigating to Views > Other Windows > C# Interactive,

Then just right click your project and run Initialize Interactive with Project from the context menu.


For older versions:

To use the C# Interactive Window with your code, ensure you first compile your code, then add a reference to the resulting assembly with the #r command like so:

C# command prompt


You can also use the Immediate Window to interact with your code as well, like so:

Immediate Window

C# interactive window won't reference my code

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.

Is it possible to use code from a Web project in C# Interactive?

Just open C# interactive window and type

#r "full path to dll\exe with your asp.net core project"

Then you can use your classes as usual.

Note that if your project targets .NET Core - it might not work as expected (or at all), because as far as I know - C# interactive currently runs under full .NET Framework. However, in your case your project is also under full .NET (<TargetFramework>net461</TargetFramework>) so you should be fine.

In Visual Studio 2019, how do I add my project assemblies (or any assemblies at all) to the C# interactive pane?

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.

Sample Image

Good luck!

Can I interact with the current debugging session variables using C# Interactive window or Immediate window?

As you expected, the answer is no, there's no way to interact with your debugee's state from the Interactive Window. This is a scenario we continue to think about, but until then you're stuck with the Interactive Window, with all it's limitations.

Test simple c# code expressions in Visual Studio

The Interactive Window (not to be confused with the immediate window) will achieve what you're looking for.

It can be accessed by View > Other Windows > C# Interactive, and is essentially an interactive compiler session that runs independently of whether the project is being executed or not, allowing you to arbitrarily execute code without having to build and run your project.

Here is an example of what can be done in this window

> Random gen = new Random();
> DateTime RandomDay()
. {
. int monthsBack = 1;
. int monthsForward = 3;
. DateTime startDate = DateTime.Now.AddMonths(-monthsBack);
. DateTime endDate = DateTime.Now.AddMonths(monthsForward);
. int range = (endDate - startDate).Days;
. return startDate.AddDays(gen.Next(range));
. }
> RandomDay()
[28/01/2020 15:11:51]

and also using external dlls

> Newtonsoft.Json.Linq.JObject.Parse("{'myArticle': { 'myDate': '2020-03-24T00:00:00'}  }")
(1,1): error CS0103: The name 'Newtonsoft' does not exist in the current context

> #r "C:\Users\MyUser\.nuget\packages\newtonsoft.json\11.0.2\lib\netstandard2.0\Newtonsoft.Json.dll"

> Newtonsoft.Json.Linq.JObject.Parse("{'myArticle': { 'myDate': '2020-03-24T00:00:00'} }")
JObject(1) { JProperty(1) { JObject(3) { JProperty(1) { [24/03/2020 00:00:00] } } } }

Execute private method in C# interactive window

Writing > MyClass.SayHello() might actually work in the interactive window, have you tried it?

If it doesn't, you can execute SayHello the same way you would do if you were trying to execute it via normal code - with reflection. You might write something that looks like this into the interactive prompt:

typeof(MyClass)
.GetMethod("SayHello", BindingFlags.NonPublic | BindingFlags.Static)
.Invoke(null, new object[0]);

You'll need to change the binding flags and Invoke method parameters depending on where the method is. (BindingFlags.Instance vs BindingFlags.Static etc)

Alternate:

If working with an instance object (not static), there exists several implementations out there which use the magic of dynamic to exposes private members. If you do a google search you can find an implementation of this (or write one yourself).

That would allow you to write code like this:

(new PrivateExposerObject(myClass)).SayHello();


Related Topics



Leave a reply



Submit