Can the C# interactive window interact with my cod

2019-01-12 21:51发布

I installed Roslyn. Now, if I know where to look in Visual Studio, I can open the 'C# interactive window', and run code:

> 5 + 3
8

That's cute. Now how can I interact my code—my classes? Assume I have a project open.

> new Cog()
(1,5): error CS0246: The type or namespace name 'Cog' could not be found (are you missing a using directive or an assembly reference?)

5条回答
\"骚年 ilove
2楼-- · 2019-01-12 22:37

Totally agree "Initializa Interactive with Project" is cool.

My approach is to push classes into a library and use //css_reference in C# script or #r in C# Interactive window

For example:

#r "D:\\dev\\DbHMonData\\LoadH2Stats\\bin\\Debug\\DbHMonStats.dll"
using DbHMonStats;
查看更多
爷、活的狠高调
3楼-- · 2019-01-12 22:43

You can use classes from your own project.
Just right click on your solution and select "Reset Interactive from Project".

If you need more information, here is the source:
Using the C# Interactive Window that comes with Roslyn – Part 2

查看更多
放我归山
4楼-- · 2019-01-12 22:43

Just an update from the @Botz3000 answer.

The command you want to find is now called "Initialize Interactive with Project"

enter image description here

Also it is worth noting i could not find this command if my C# interactive window was not viewable.

查看更多
放荡不羁爱自由
5楼-- · 2019-01-12 22:47

When using Visual Studio 2015:

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

查看更多
Viruses.
6楼-- · 2019-01-12 22:51

Don't forget to change class name range, to public class name.
:)

查看更多
登录 后发表回答