Calling current project methods from C# Interactive Window in Visual Studio 2015 ?

Calling current project methods from C# Interactive Window in Visual Studio 2015 ?

The C# Interactive Window in Visual Studio is a great utility to play around the code, language features, API’s and other .NET framework features. The Interactive Window also support features like IntelliSense, syntax highlighting and navigating through executed commands. It is a REPL  (Read–eval–print loop) that provides us an environment for live coding and evaluate expression or code snippet based on our inputs. 

Few months back I published several post on Use C# Interactive Window for your coding experiment in Visual Studio 2015  and also Executing C# Scripts from Command Line or C# Interactive Windows in Visual Studio. We also talked about Command Line C# Interactive Script Execution for Visual Studio .

Recently I received a query on – are there any ways I can call current project methods in interactive window   ?  Well, of course we can do it.

We can Add a metadata reference to specified assembly and all its dependencies, including an executable in Interactive Window.

Let’s take a look with an example. Consider you have an Executable with Method Add() and a Class Library that has method GetResult();

image

Now if you want to call any of these current project methods, first open the C# Interactive Window:

To launch the Interactive window navigate through  Views > Other Windows > C# Interactive

Then use #r to load the DLL or Executable in to it. for an example, #r “<dll path>”

image

Once you execute this command, you should be able to access all the public methods from the DLL.

Step 1 :

image

Step 2 :

image

Similarly for an executable (exe) you can load it using similar way.

image

The cool part of this REPL is not only you have intellisense with your code, while accessing any folders during selection of files, it detects the paths automatically.

image

Keep in mind, Interactive Window calls the build code and it does not run in debug mode. So you can’t put breakpoint and debug you code while calling them from interactive window.

Hope this help and you will start exploring this more !

Thanks !

Abhijit Jana

Abhijit runs the Daily .NET Tips. He started this site with a vision to have a single knowledge base of .NET tips and tricks and share post that can quickly help any developers . He is a Former Microsoft ASP.NET MVP, CodeProject MVP, Mentor, Speaker, Author, Technology Evangelist and presently working as a .NET Consultant. He blogs at http://abhijitjana.net , you can follow him @AbhijitJana . He is the author of book Kinect for Windows SDK Programming Guide.